# base/vector3 (vanilla.js 0.4.0)

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

3D vector.

## Vector3 extends Object

3D vector.

Example: Create, cross and normalize

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

const right = Vector3.create(1, 0, 0);
const up = Vector3.up();
const forward = right.cross(up);
const offset = Vector3.create(3, 0, 4);
const direction = offset.normalize();
console.log(forward.x, forward.y, forward.z);
console.log(direction.x, direction.z, direction.length());
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `equals(other)` (method, since 0.3.0): Comparison.
  - `other` any: Value to compare.
  - returns boolean: True if it is the same object or a Vector3 with the same x, y and z.
- `clone()` (method, since 0.3.0): Clone.
  - returns this: A new vector with the same x, y and z.
- `get/set x` (property, since 0.3.0): X coordinate getter property.
  X coordinate setter property.
  - returns number: X coordinate.
- `get/set y` (property, since 0.3.0): Y coordinate getter property.
  Y coordinate setter property.
  - returns number: Y coordinate.
- `get/set z` (property, since 0.3.0): Z coordinate getter property.
  Z coordinate setter property.
  - returns number: Z coordinate.
- `set(x, y, z)` (method, since 0.3.0): Assignment.
  - `x` number: X coordinate.
  - `y` number: Y coordinate.
  - `z` number: Z coordinate.
- `getX()` (method, since 0.3.0): Returns the horizontal axis.
  - returns number: X coordinate.
- `getY()` (method, since 0.3.0): Returns the vertical axis.
  - returns number: Y coordinate.
- `getZ()` (method, since 0.3.0): Returns the depth axis.
  - returns number: Z coordinate.
- `length()` (method, since 0.3.0): Length.
  - returns number: Length of the vector.
- `lengthSquared()` (method, since 0.3.0): Squared length.
  - returns number: Squared length.
- `normalize()` (method, since 0.3.0): Normalize.
  - returns Vector3: A new vector scaled to length 1. (copied as is if the length is 0)
- `add(other)` (method, since 0.3.0): Add.
  - `other` Vector3 | number: Vector or number to add. (a number is added to all three components, other types throw an error)
  - returns Vector3: A new computed vector. (this vector is not changed)
- `subtract(other)` (method, since 0.3.0): Subtract.
  - `other` Vector3 | number: Vector or number to subtract. (a number is subtracted from all three components, other types throw an error)
  - returns Vector3: A new computed vector. (this vector is not changed)
- `multiply(other)` (method, since 0.3.0): Multiply.
  - `other` Vector3 | number: Vector or number to multiply by. (all three components are multiplied by a number, other types throw an error)
  - returns Vector3: A new computed vector. (this vector is not changed)
- `divide(other)` (method, since 0.3.0): Divide.
  - `other` Vector3 | number: Vector or number to divide by. (all three components are divided by a number, other types throw an error)
  - returns Vector3: 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 Vector3: 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 Vector3: 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 Vector3: 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 Vector3: A new computed vector.
- `dot(other)` (method, since 0.3.0): Dot product.
  - `other` Vector3: Vector for the dot product.
  - returns number: Dot product.
- `cross(other)` (method, since 0.3.0): Cross product.
  - `other` Vector3: Vector for the cross product.
  - returns Vector3: A new vector perpendicular to this and other. (this × other)
- `distance(other)` (method, since 0.3.0): Distance.
  - `other` Vector3: Vector to measure the distance to.
  - returns number: Distance between the two points.
- `static create(x, y, z)` (static, since 0.3.0): Creates. (static)
  - `x` number: X coordinate.
  - `y` number: Y coordinate.
  - `z` number: Z coordinate.
  - returns Vector3: A new vector.
- `static zero()` (static, since 0.3.0): Creates a zero vector. (static)
  - returns Vector3: A (0, 0, 0) vector.
- `static one()` (static, since 0.3.0): Creates a one vector. (static)
  - returns Vector3: A (1, 1, 1) vector.
- `static up()` (static, since 0.3.0): Creates an up unit vector. (static)
  - returns Vector3: A (0, 1, 0) vector.
- `static lerp(from, to, normalizedTime)` (static, since 0.3.0): Linear interpolation. (static)
  - `from` Vector3: Start vector.
  - `to` Vector3: End vector.
  - `normalizedTime` number: 0 gives from, 1 gives to. (values outside 0 ~ 1 are not clamped and extrapolate)
  - returns Vector3: A new interpolated vector.
