# Vector2

## Overview

Vector2 is a data type used to express vector in a two-dimensional space using X and Y axes. It is used to handle a variety of 2D data such as position, direction, speed, and strength. It is also one of the key elements used by many systems to calculate coordinates and vector within a space or simulating 2D physics.

## Constructors

### new

Creates and returns a new vector using the passed X and Y values.

#### Parameters

| `number` x | The value of X coordinate. |
| ---------- | -------------------------- |
| `number` y | The value of Y coordinate. |

#### Return

| `Vector2` | The created Vector2. |
| --------- | -------------------- |

#### Code Samples

```lua
local Vector = Vector2.new(3, 7)
print(Vector)
```

## Properties

### X

`number`&#x20;

The value of X coordinate.

#### Code Samples

```lua
local Vector = Vector2.new(5, 10)
print(Vector.X)
```

### Y

`number`&#x20;

The value of Y coordinate.

#### Code Samples

```lua
local Vector = Vector2.new(5, 10)
print(Vector.Y)
```

### zero

`Vector2`&#x20;

A vector of which axis values are zeroes (0, 0).

#### Code Samples

```lua
local ZeroVector = Vector2.zero
print(ZeroVector)
```

### one

`Vector2`&#x20;

A vector of which axis values are ones (1, 1).

#### Code Samples

```lua
local OneVector = Vector2.one
print(OneVector)
```

### xAxis

`Vector2`&#x20;

A Vector2 set to have the value of 1 for X axis and 0 for Y axis (1, 0).

#### Code Samples

```lua
local XAxis = Vector2.xAxis
print(XAxis)
```

### yAxis

`Vector2`&#x20;

A Vector2 set to have the value of 0 for X axis and 1 for Y axis (0, 1).

#### Code Samples

```lua
local YAxis = Vector2.yAxis
print(YAxis)
```

## Methods

### Lerp

Calculates the linear interpolation between the current vector and the target vector according to alpha ratio and returns it as a new vector.

#### Parameters

| `Vector2` goal | The target Vector2 to be interpolated.  |
| -------------- | --------------------------------------- |
| `number` alpha | A value indicating interpolation ratio. |

#### Return

| `Vector2` | The created Vector2. |
| --------- | -------------------- |

#### Code Samples

```lua
local a = Vector2.new(0, 0)
local b = Vector2.new(10, 10)
print("Lerp:", a:Lerp(b, 0.3)) --> Output: 3, 3
```

### Slerp

Calculates the spherical linear interpolation between the current vector and the target vector according to alpha ratio and returns it as a new vector.

While Lerp interpolates along a linear path, Slerp interpolates along a curve on a unit sphere, making its rotation axis movement smoother and more constant.

#### Parameters

| `Vector2` goal | The target Vector2 to be interpolated.  |
| -------------- | --------------------------------------- |
| `number` alpha | A value indicating interpolation ratio. |

#### Return

| `Vector2` | The created Vector2. |
| --------- | -------------------- |

#### Code Samples

```lua
local a = Vector2.new(1, 0)
local b = Vector2.new(0, 1)
print("Slerp:", a:Slerp(b, 0.5)) --> Output: 0.707107, 0.707107
```
