# base/vector3 (vanilla-engine 0.4.0)

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

3D vector. (the original base/vector3.js)
- The original keeps x, y, z as hidden fields with properties, but the Rust version is a Copy value with public fields like Vector2.
  The original clone is replaced by copying, and equals by PartialEq.
- The original add / subtract / multiply / divide accept both numbers and Vector3. The Rust version splits them into ones taking Vector3 and
  ...Scalar ones taking numbers. (the same shape as Vector2's multiply_scalar)
- Numbers are f64 like the original JavaScript numbers.
- lerp uses the original math.lerp formula (from * (1 - t) + to * t) as is. It may differ in the last digits from the Rust math::lerp (from + (to - from) * t),
  so it is computed separately.

## Vector3 (struct) (Clone, Copy, Debug, Default, PartialEq, Add, Sub, Mul<f64>, AddAssign, SubAssign)

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

Example: Add, take the cross product and measure the length

```rust
use vanilla::Vector3;

let position = Vector3::create(1.0, 2.0, 2.0);
assert_eq!(position.length(), 3.0);
let moved = position.add(Vector3::up());
assert_eq!(moved, Vector3::create(1.0, 3.0, 2.0));

let axis_x = Vector3::create(1.0, 0.0, 0.0);
let axis_z = Vector3::create(0.0, 0.0, 1.0);
assert_eq!(axis_x.cross(Vector3::up()), axis_z);

let mut velocity = Vector3::zero();
velocity += Vector3::one() * 2.0;
assert_eq!(velocity, Vector3::create(2.0, 2.0, 2.0));
```

- `pub x: f64` (field, since 0.3.2): x component.
  - returns f64
- `pub y: f64` (field, since 0.3.2): y component. (up is +)
  - returns f64
- `pub z: f64` (field, since 0.3.2): z component.
  - returns f64
- `pub const fn create(x: f64, y: f64, z: f64) -> Vector3` (static, since 0.3.2): Constructor.
  - `x` f64: x component.
  - `y` f64: y component.
  - `z` f64: z component.
  - returns Vector3: The vector (x, y, z).
- `pub const fn zero() -> Vector3` (static, since 0.3.2): Zero vector. (0, 0, 0)
  - returns Vector3: A vector with all three components 0.
- `pub const fn one() -> Vector3` (static, since 0.3.2): One vector. (1, 1, 1)
  - returns Vector3: A vector with all three components 1.
- `pub const fn up() -> Vector3` (static, since 0.3.2): Up unit vector. (0, 1, 0)
  - returns Vector3: A (0, 1, 0) vector.
- `pub fn set(&mut self, x: f64, y: f64, z: f64)` (method, since 0.3.2): Assignment.
  - `x` f64: x component to set.
  - `y` f64: y component to set.
  - `z` f64: z component to set.
- `pub fn get_x(&self) -> f64` (method, since 0.3.2): Returns the horizontal axis.
  - returns f64: x component.
- `pub fn get_y(&self) -> f64` (method, since 0.3.2): Returns the vertical axis.
  - returns f64: y component.
- `pub fn get_z(&self) -> f64` (method, since 0.3.2): Returns the depth axis.
  - returns f64: z component.
- `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.3.2): Squared length.
  - returns f64: x * x + y * y + z * z. (no square root is computed)
- `pub fn normalize(&self) -> Vector3` (method, since 0.3.2): Normalization. (returns a copy as is if the length is 0 or less)
  - returns Vector3: A new vector of length 1. The same value as self if the length is 0 or less.

  Example: Keep only the direction

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

- `pub fn add(&self, other: Vector3) -> Vector3` (method, since 0.3.2): Add.
  - `other` Vector3: Vector to add.
  - returns Vector3: A new vector added per component. (self is not changed)
- `pub fn add_scalar(&self, scalar: f64) -> Vector3` (method, since 0.3.2): Adds the same number to all three components. (the original add(number))
  - `scalar` f64: Number to add to all three components.
  - returns Vector3: The new added vector.
- `pub fn subtract(&self, other: Vector3) -> Vector3` (method, since 0.3.2): Subtract.
  - `other` Vector3: Vector to subtract.
  - returns Vector3: A new vector subtracted per component. (self - other)
- `pub fn subtract_scalar(&self, scalar: f64) -> Vector3` (method, since 0.3.2): Subtracts the same number from all three components. (the original subtract(number))
  - `scalar` f64: Number to subtract from all three components.
  - returns Vector3: The new subtracted vector.
- `pub fn multiply(&self, other: Vector3) -> Vector3` (method, since 0.3.2): Per-component multiplication.
  - `other` Vector3: Vector to multiply per component.
  - returns Vector3: A new vector multiplied per component.
- `pub fn multiply_scalar(&self, scalar: f64) -> Vector3` (method, since 0.3.2): Scalar multiplication. (the original multiply(number))
  - `scalar` f64: Number to multiply all three components by.
  - returns Vector3: The new multiplied vector.
- `pub fn divide(&self, other: Vector3) -> Vector3` (method, since 0.3.2): Per-component division.
  - `other` Vector3: Vector to divide by per component. (components that are 0 give infinity or NaN)
  - returns Vector3: A new vector divided per component.
- `pub fn divide_scalar(&self, scalar: f64) -> Vector3` (method, since 0.3.2): Scalar division. (the original divide(number))
  - `scalar` f64: Number to divide all three components by. (0 gives infinity or NaN)
  - returns Vector3: The new divided vector.
- `pub fn dot(&self, other: Vector3) -> f64` (method, since 0.3.2): Dot product.
  - `other` Vector3: Vector for the dot product.
  - returns f64: Dot product. (x * other.x + y * other.y + z * other.z)
- `pub fn cross(&self, other: Vector3) -> Vector3` (method, since 0.3.2): Cross product. (right-handed coordinate system: x axis cross y axis = z axis)
  - `other` Vector3: Vector placed on the right. (self cross other)
  - returns Vector3: A new vector perpendicular to both vectors. (length is the product of the two lengths x the sine of the angle between them)

  Example: Axes of the right-handed coordinate system

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

- `pub fn distance(&self, other: Vector3) -> f64` (method, since 0.3.2): Distance between two points.
  - `other` Vector3: Point to measure the distance to.
  - returns f64: Distance between the two points. (0 or more)
- `pub fn lerp(from: Vector3, to: Vector3, normalized_time: f64) -> Vector3` (static, since 0.3.2): Linear interpolation. (the original math.lerp formula: from * (1 - t) + to * t)
  - `from` Vector3: Start vector.
  - `to` Vector3: End vector.
  - `normalized_time` f64: Interpolation ratio. (0 is from, 1 is to; values outside 0 ~ 1 are not clamped and extrapolate)
  - returns Vector3: A new interpolated vector.

  Example: The quarter point

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

