# base/vector2 (vanilla-engine 0.4.0)

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

2D vector. (the original base/vector2.js)
- The original add / subtract / multiply / divide / modulo accept both numbers and Vector2. The Rust version splits them into ones taking Vector2 and
  ..._scalar ones taking numbers. Numbers are f64 like the original JavaScript numbers.
- lerp uses the original math.lerp formula (from * (1 - t) + to * t) as is. (same as Vector3)
- Two bugs in the original (modulo dividing instead of taking the remainder, and half returning double (dividing by 0.5) instead of half) were fixed together in both engines.
  modulo returns the remainder (%), and half returns half. (nothing in vanilla.js or the games uses either)

## Vector2 (struct) (Clone, Copy, Debug, Default, PartialEq, Add, Sub, Mul<f64>, AddAssign, SubAssign, GeometryKeyValue, ToReflectValue, FromReflectValue<'a>)

2D vector. (a Copy value with x, y as public fields; computing functions return a new vector without changing self)

Example: Create, add and measure length

```rust
use vanilla::Vector2;

let start = Vector2::create(1.0, 2.0);
let velocity = Vector2::create(3.0, 4.0);
let moved = start.add(velocity);
assert_eq!(moved, Vector2::create(4.0, 6.0));
assert_eq!(velocity.length(), 5.0);
assert_eq!(start + velocity, moved);

let mut position = Vector2::zero();
position += velocity * 2.0;
assert_eq!(position, Vector2::create(6.0, 8.0));
```

- `pub x: f64` (field, since 0.3.2): x component.
  - returns f64
- `pub y: f64` (field, since 0.3.2): y component.
  - returns f64
- `pub const fn create(x: f64, y: f64) -> Vector2` (static, since 0.3.2): Constructor.
  - `x` f64: x component.
  - `y` f64: y component.
  - returns Vector2: The vector (x, y).
- `pub const fn zero() -> Vector2` (static, since 0.3.2): (0, 0).
  - returns Vector2: A vector with both components 0.
- `pub const fn one() -> Vector2` (static, since 0.3.2): (1, 1).
  - returns Vector2: A vector with both components 1.
- `pub const fn minus_one() -> Vector2` (static, since 0.4.0): (-1, -1).
  - returns Vector2: A vector with both components -1.
- `pub const fn positive_infinity() -> Vector2` (static, since 0.4.0): (+infinity, +infinity).
  - returns Vector2: A vector with both components f64::INFINITY.
- `pub const fn negative_infinity() -> Vector2` (static, since 0.4.0): (-infinity, -infinity).
  - returns Vector2: A vector with both components f64::NEG_INFINITY.
- `pub fn set(&mut self, x: f64, y: f64)` (method, since 0.3.2): Sets the value.
  - `x` f64: x component to set.
  - `y` f64: y component to set.
- `pub fn get_x(&self) -> f64` (method, since 0.4.0): Returns x.
  - returns f64: x component.
- `pub fn get_y(&self) -> f64` (method, since 0.4.0): Returns y.
  - returns f64: y component.
- `pub fn add(&self, other: Vector2) -> Vector2` (method, since 0.3.2): Add.
  - `other` Vector2: Vector to add.
  - returns Vector2: A new vector added per component. (self is not changed)
- `pub fn add_scalar(&self, scalar: f64) -> Vector2` (method, since 0.4.0): Adds a number. (to both components)
  - `scalar` f64: Number to add to both components.
  - returns Vector2: The new added vector.
- `pub fn subtract(&self, other: Vector2) -> Vector2` (method, since 0.3.2): Subtract.
  - `other` Vector2: Vector to subtract.
  - returns Vector2: A new vector subtracted per component. (self - other)
- `pub fn subtract_scalar(&self, scalar: f64) -> Vector2` (method, since 0.4.0): Subtracts a number. (from both components)
  - `scalar` f64: Number to subtract from both components.
  - returns Vector2: The new subtracted vector.
- `pub fn multiply(&self, other: Vector2) -> Vector2` (method, since 0.3.2): Per-component multiplication.
  - `other` Vector2: Vector to multiply per component.
  - returns Vector2: A new vector multiplied per component.
- `pub fn multiply_scalar(&self, scalar: f64) -> Vector2` (method, since 0.3.2): Scalar multiplication.
  - `scalar` f64: Number to multiply both components by.
  - returns Vector2: The new multiplied vector.
- `pub fn divide(&self, other: Vector2) -> Vector2` (method, since 0.4.0): Per-component division.
  - `other` Vector2: Vector to divide by per component. (components that are 0 give infinity or NaN)
  - returns Vector2: A new vector divided per component.
- `pub fn divide_scalar(&self, scalar: f64) -> Vector2` (method, since 0.4.0): Scalar division.
  - `scalar` f64: Number to divide both components by. (0 gives infinity or NaN)
  - returns Vector2: The new divided vector.
- `pub fn modulo(&self, other: Vector2) -> Vector2` (method, since 0.4.0): Per-component remainder. (the sign follows the dividend, same as the original %)
  - `other` Vector2: Vector to divide by per component.
  - returns Vector2: A new vector holding the per-component remainders.

  Example: The remainder of a negative is negative

  ```rust
  use vanilla::Vector2;
  
  let remainder = Vector2::create(7.0, -9.0).modulo(Vector2::create(4.0, 4.0));
  assert_eq!(remainder, Vector2::create(3.0, -1.0));
  ```

- `pub fn modulo_scalar(&self, scalar: f64) -> Vector2` (method, since 0.4.0): Scalar remainder.
  - `scalar` f64: Number to divide both components by.
  - returns Vector2: A new vector holding the per-component remainders.
- `pub fn half(&self) -> Vector2` (method, since 0.4.0): Half.
  - returns Vector2: A new vector with both components multiplied by 0.5.
- `pub fn length(&self) -> f64` (method, since 0.3.2): Length.
  - returns f64: Length of the vector. (0 or more)
- `pub fn length_squared(&self) -> f64` (method, since 0.4.0): Squared length.
  - returns f64: x * x + y * y. (no square root is computed)
- `pub fn distance(&self, other: Vector2) -> f64` (method, since 0.3.2): Distance between two points.
  - `other` Vector2: Point to measure the distance to.
  - returns f64: Distance between the two points. (0 or more)
- `pub fn normalize(&self) -> Vector2` (method, since 0.3.2): Unit vector. ((0, 0) if the length is 0)
  - returns Vector2: A new vector of length 1. (0, 0) if the length is 0 or less.

  Example: Keep only the direction

  ```rust
  use vanilla::Vector2;
  
  let direction = Vector2::create(3.0, 4.0).normalize();
  assert_eq!(direction, Vector2::create(0.6, 0.8));
  assert_eq!(Vector2::zero().normalize(), Vector2::zero());
  ```

- `pub fn dot(&self, other: Vector2) -> f64` (method, since 0.3.2): Dot product.
  - `other` Vector2: Vector for the dot product.
  - returns f64: Dot product. (self.x * other.x + self.y * other.y)
- `pub fn cross(&self, other: Vector2) -> f64` (method, since 0.4.0): z of the cross product. (self.x * other.y - self.y * other.x, positive if other is counterclockwise)
  - `other` Vector2: Vector for the cross product.
  - returns f64: z value of the cross product.

  Example: Swapping the order flips the sign

  ```rust
  use vanilla::Vector2;
  
  let axis_x = Vector2::create(1.0, 0.0);
  let axis_y = Vector2::create(0.0, 1.0);
  assert_eq!(axis_x.cross(axis_y), 1.0);
  assert_eq!(axis_y.cross(axis_x), -1.0);
  ```

- `pub fn angle(&self, other: Vector2) -> f64` (method, since 0.4.0): Angle between two vectors. (radians, 0 ~ PI, NaN for a zero-length vector)
  - `other` Vector2: Vector to measure the angle with.
  - returns f64: Angle between the two vectors. (radians, 0 ~ PI, NaN if either length is 0)

  Example: A right angle and a zero-length vector

  ```rust
  use vanilla::{math, Vector2};
  
  let angle = Vector2::create(1.0, 0.0).angle(Vector2::create(0.0, 2.0));
  assert!((angle - math::PI * 0.5).abs() < 1e-6);
  assert!(Vector2::zero().angle(Vector2::one()).is_nan());
  ```

- `pub fn lerp(from: Vector2, to: Vector2, normalized_time: f64) -> Vector2` (static, since 0.4.0): Linear interpolation. (the original math.lerp formula: from * (1 - t) + to * t)
  - `from` Vector2: Start vector.
  - `to` Vector2: End vector.
  - `normalized_time` f64: Interpolation ratio. (0 is from, 1 is to; values outside 0 ~ 1 are not clamped and extrapolate)
  - returns Vector2: A new interpolated vector.

  Example: The quarter point

  ```rust
  use vanilla::Vector2;
  
  let from = Vector2::create(0.0, 10.0);
  let to = Vector2::create(8.0, -6.0);
  assert_eq!(Vector2::lerp(from, to, 0.25), Vector2::create(2.0, 6.0));
  assert_eq!(Vector2::lerp(from, to, 1.0), to);
  ```

- `pub fn clamp(value: Vector2, minimum: Vector2, maximum: Vector2) -> Vector2` (static, since 0.4.0): Clamps per component.
  - `value` Vector2: Vector to clamp.
  - `minimum` Vector2: Per-component minimum.
  - `maximum` Vector2: Per-component maximum.
  - returns Vector2: A new vector with each component clamped to minimum ~ maximum. (uses math::clamp per component)

  Example: Clamp into a range

  ```rust
  use vanilla::Vector2;
  
  let clamped = Vector2::clamp(Vector2::create(-5.0, 50.0), Vector2::zero(), Vector2::create(10.0, 10.0));
  assert_eq!(clamped, Vector2::create(0.0, 10.0));
  ```

