# base/vector2 (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/vector2/

2D vector.

## Vector2 extends Object

2D vector.

Example: Create, add and measure length

```js
import { Vector2 } from "./build/vanilla.js";

const position = Vector2.create(3, 4);
const moved = position.add(Vector2.create(1, 0));
console.log(moved.x, moved.y, position.length());
```

- `constructor()` (constructor, since 0.0.8): Constructor.
- `equals(other)` (method, since 0.0.8): Comparison.
  - `other` any: Value to compare.
  - returns boolean: True if it is the same object or a Vector2 with the same x and y.
- `clone()` (method, since 0.0.9): Clone.
  - returns this: A new vector with the same x and y.
- `get/set x` (property, since 0.0.8): X coordinate getter property.
  X coordinate setter property.
  - returns number: X coordinate.
- `get/set y` (property, since 0.0.8): Y coordinate getter property.
  Y coordinate setter property.
  - returns number: Y coordinate.
- `set(x, y)` (method, since 0.0.8): Assignment.
  - `x` number: X coordinate.
  - `y` number: Y coordinate.
- `getX()` (method, since 0.0.8): Returns the horizontal axis.
  - returns number: X coordinate.
- `getY()` (method, since 0.0.8): Returns the vertical axis.
  - returns number: Y coordinate.
- `length()` (method, since 0.0.8): Length.
  - returns number: Length of the vector.
- `normalize()` (method, since 0.0.8): Normalize.
  - returns Vector2: A new vector scaled to length 1. (copied as is if the length is 0)
- `add(other)` (method, since 0.0.8): Add.
  - `other` Vector2 | number: Vector or number to add. (a number is added to both components, other types throw an error)
  - returns Vector2: A new computed vector. (this vector is not changed)
- `subtract(other)` (method, since 0.0.8): Subtract.
  - `other` Vector2 | number: Vector or number to subtract. (a number is subtracted from both components, other types throw an error)
  - returns Vector2: A new computed vector. (this vector is not changed)
- `multiply(other)` (method, since 0.0.8): Multiply.
  - `other` Vector2 | number: Vector or number to multiply by. (both components are multiplied by a number, other types throw an error)
  - returns Vector2: A new computed vector. (this vector is not changed)
- `divide(other)` (method, since 0.0.8): Divide.
  - `other` Vector2 | number: Vector or number to divide by. (both components are divided by a number, other types throw an error)
  - returns Vector2: A new computed vector. (this vector is not changed)
- `modulo(other)` (method, since 0.0.8): Modulo.
  - `other` Vector2 | number: Vector or number to divide by. (takes the remainder, both components are divided by a number, other types throw an error)
  - returns Vector2: A new computed vector. (this vector is not changed)
- `addScalar(scalar)` (method, since 0.4.0): Adds a number. (Same number to every component, same as add(number))
  - `scalar` number: Number to add.
  - returns Vector2: A new computed vector.
- `subtractScalar(scalar)` (method, since 0.4.0): Subtracts a number. (Same number from every component, same as subtract(number))
  - `scalar` number: Number to subtract.
  - returns Vector2: A new computed vector.
- `multiplyScalar(scalar)` (method, since 0.4.0): Multiplies by a number. (Same number for every component, same as multiply(number))
  - `scalar` number: Number to multiply by.
  - returns Vector2: A new computed vector.
- `divideScalar(scalar)` (method, since 0.4.0): Divides by a number. (Same number for every component, same as divide(number))
  - `scalar` number: Number to divide by.
  - returns Vector2: A new computed vector.
- `moduloScalar(scalar)` (method, since 0.4.0): Modulo by a number. (Same number for every component, same as modulo(number))
  - `scalar` number: Number to divide by. (takes the remainder)
  - returns Vector2: A new computed vector.
- `half()` (method, since 0.0.8): Half.
  - returns Vector2: A new vector with x and y divided by 2.
- `dot(other)` (method, since 0.0.8): Dot product.
  - `other` Vector2: Vector for the dot product.
  - returns number: Dot product.
- `cross(other)` (method, since 0.0.8): Cross product (2D cross product -> returns a scalar).
  - `other` Vector2: Vector for the cross product.
  - returns number: Cross product. (this.x * other.y - this.y * other.x)
- `distance(other)` (method, since 0.0.8): Distance between two vectors.
  - `other` Vector2: Vector to measure the distance to.
  - returns number: Distance between the two points.
- `lengthSquared()` (method, since 0.0.8): Squared length (square root optimization).
  - returns number: Squared length.
- `angle(other)` (method, since 0.0.8): Returns the angle between two vectors (radians).
  - `other` Vector2: Vector to measure the angle to.
  - returns number: Angle between the two vectors. (radians, 0 ~ π, NaN if either length is 0)

  Example: View the angle between two vectors in degrees

  ```js
  import { Vector2, Math } from "./build/vanilla.js";
  
  const right = Vector2.create(1, 0);
  const up = Vector2.create(0, -1);
  const radian = right.angle(up);
  console.log(radian, Math.radianToDegree(radian));
  ```

- `static create(x, y)` (static, since 0.0.8): Creates a new vector.
  - `x` number: X coordinate.
  - `y` number: Y coordinate.
  - returns Vector2: A new vector.
- `static zero()` (static, since 0.0.8): Creates a vector with the value 0.
  - returns Vector2: A (0, 0) vector.
- `static one()` (static, since 0.0.8): Creates a vector with the value 1.
  - returns Vector2: A (1, 1) vector.
- `static minusOne()` (static, since 0.1.0): Creates a vector with the value -1.
  - returns Vector2: A (-1, -1) vector.
- `static positiveInfinity()` (static, since 0.0.8): Creates a maximum-value vector.
  - returns Vector2: An (Infinity, Infinity) vector.
- `static negativeInfinity()` (static, since 0.0.8): Creates a minimum-value vector.
  - returns Vector2: A (-Infinity, -Infinity) vector.
- `static lerp(from, to, normalizedTime)` (static, since 0.0.8): Linear interpolation.
  - `from` Vector2: Start vector.
  - `to` Vector2: End vector.
  - `normalizedTime` number: 0 gives from, 1 gives to. (values outside 0 ~ 1 are not clamped and extrapolate)
  - returns Vector2: A new interpolated vector.

  Example: Find the midpoint between two points

  ```js
  import { Vector2 } from "./build/vanilla.js";
  
  const start = Vector2.create(0, 0);
  const end = Vector2.create(100, 50);
  const middle = Vector2.lerp(start, end, 0.5);
  console.log(middle.x, middle.y);
  ```

- `static clamp(value, min, max)` (static, since 0.0.8): Clamps to a range.
  - `value` Vector2: Vector to clamp.
  - `min` Vector2: Per-component minimum.
  - `max` Vector2: Per-component maximum.
  - returns Vector2: A new vector with each component clamped to min ~ max.
