# Vec

A two-dimensional vector.

Vectors can be used to represent positions, normals, tangents, offsets and translations.

## Constructors

Vec(x,y)→[Vec](https://cuttle.xyz/learn/reference/Vec)

Constructs a vector from two numbers.

```
Vec(3, 5)
// Result: Vec(3, 5)
```

`x`

`number`

optional

The `x` component

`y`

`number`

optional

The `y` component

Vec.fromAngle(angle)→[Vec](https://cuttle.xyz/learn/reference/Vec)

Constructs a unit-length vector from an `angle`.

`angle`

`number`

An angle specified in degrees

Vec.fromAngleRadians(angle)→[Vec](https://cuttle.xyz/learn/reference/Vec)

Constructs a unit-length vector from an `angle`.

`angle`

`number`

An angle specified in radians

## Properties

.xnumber

The `x` component

.ynumber

The `y` component

## Methods

.clone()→[Vec](https://cuttle.xyz/learn/reference/Vec)

Returns a copy of this vector.

.set(x,y)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Sets the the `x` and `y` components of this vector.

`x`

`number`

`y`

`number`

.copy(v)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Copies `x` and `y` components into this vector from `v`.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

.affineTransform(affineMatrix)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Transforms this vector by the affine matrix `affineMatrix`.

Use this when transforming a `Vec` that represents a point or position.

`affineMatrix`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

An affine matrix representing a transformation

.affineTransformWithoutTranslation(affineMatrix)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Transforms this vector by the affine matrix `affineMatrix`, ignoring translation.

Use this instead of `affineTransform()` when transforming a `Vec` that represents a normal or a tangent.

`affineMatrix`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

An affine matrix representing a transformation

.transform(transform)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Transforms this vector.

```
vec.transform({
  position: Vec(1, 2),
  rotation: 45,
  scale: Vec(1, 0.5), // Scale can also be a number
  skew: 10,
  origin: Vec(0, 0), // The center of rotation and scale
})
```

`transform`

`[TransformArgs](https://cuttle.xyz/learn/reference/interfaces#TransformArgs)`

An object reprenting a transform.

.add(v)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Adds the vector `v` to this vector.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

.addScalar(x)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Adds the scalar `x` to both components of this vector.

`x`

`number`

.sub(v)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Subtracts the vector `v` from this vector.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

.subScalar(x)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Subtracts the scalar `x` from both components of this vector.

`x`

`number`

.mul(v)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Multiplies this vector by the vector `v`.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

.mulScalar(x)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Multiplies both components of this vector by the scalar `x`.

`x`

`number`

.div(v)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Divides this vector by the vector `v`.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

.divScalar(x)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Divides both components of this vector by the scalar `x`;

`x`

`number`

.negate()→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Multiplies both components of this vector by -1. This is functionally the same as `.mulScalar(-1)`.

.equals(v)→boolean

Tests if this vector _exactly_ equals the vector `v`.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

Returns `true` if the vectors are _exactly_ equal, `false` otherwise.

.floor()→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Rounds the components of this vector to the next-lowest integer.

```
let v = Vec(3.141, -1.618);
v.floor();
// Result: Vec(3, -2)
```

.ceil()→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Rounds the components of this vector to the next-highest integer.

```
let v = Vec(3.141, -1.618);
v.ceil();
// Result: Vec(4, -1)
```

.round()→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Rounds the components of this vector to the closest integer.

```
let v = Vec(3.141, 3.618);
v.round();
// Result: Vec(3, 4)
```

.min(v)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Compares the components of this vector and `v` and sets this vector's to the lesser of the two.

```
let a = Vec(1, 4);
let b = Vec(2, 3);
a.min(b);
// Result: Vec(1, 3)
```

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

The vector to compare against

.minScalar(x)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Compares this vector's components to `x` and sets them to the lesser of the two.

```
let v = Vec(1, 2);
v.minScalar(3);
// Result: Vec(1, 2)
```

`x`

`number`

The value to compare against

.max(v)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Compares the components of this vector and `v` and sets this vector's to the greater of the two.

```
let a = Vec(1, 4);
let b = Vec(2, 3);
a.max(b);
// Result: Vec(2, 4)
```

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

The vector to compare against

.maxScalar(x)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Compares this vector's components to `x` and sets them to the greater of the two.

```
let v = Vec(1, 2);
v.maxScalar(3);
// Result: Vec(3, 3)
```

`x`

`number`

The value to compare against

.mix(v,t)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Linearly interpolates this vector to the vector `v` by the mixing factor `t`.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

The vector to interpolate to

`t`

`number`

The mixing factor

.dot(v)→number

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

Returns the dot product between this vector and the vector `v`.

.cross(v)→number

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

Returns the cross product between this vector and the vector `v`.

.normalize()→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Scales this vector so that its length is equal to `1`.

Note the this vector must already have a non-zero length.

.rotate(angle)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Rotates this vector clockwise by an `angle` specified in degrees.

`angle`

`number`

An angle in degrees

.rotateRadians(angleRadians)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Rotates this vector clockwise by an `angle` specified in radians.

`angleRadians`

`number`

An angle in radians

.rotate90()→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Rotates this vector clockwise by exactly 90°.

This is functionally the same as writing `vec.rotate(90)` but is computationally simpler.

.rotateNeg90()→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Rotates this vector counter-clockwise by exactly 90°.

This is functionally the same as writing `vec.rotate(-90)` but is computationally simpler.

.projectOnto(v)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

The vector projection of this vector onto a non-zero vector `v`, (also known as the vector component or vector resolution of a in the direction of b), is the orthogonal projection of this onto a straight line parallel to `v`.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

A vector representing a line to project onto

.closestPoint(v)→

A trivial closest point implementation to maintain a uniform interface with Geometry.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

Returns a ClosestPointResult with itself as the position.

.angle()→number

Returns the angle of this vector in degrees.

.angleRadians()→number

Returns the angle of this vector in radians.

.setFromAngle(angle)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Sets this vector to a unit-length vector at `angle`.

`angle`

`number`

An angle specified in degrees

.setFromAngleRadians(angle)→[Vec](https://cuttle.xyz/learn/reference/Vec)chainable

Sets this vector to a unit-length vector at `angle`.

`angle`

`number`

An angle specified in radians

.isClockwiseFrom(v)→boolean

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

The vector to compare against

Returns `true` if this vector lies in the 180° region clockwise from `v`.

.length()→number

Returns the length of this vector. The length of a vector is also sometimes referred to as its "magnitude".

.lengthSquared()→number

Returns the squared length of this vector.

This variation avoids the `sqrt()` used in `length()` and can be computationally simpler when comparing the length of two vectors.

.distance(v)→number

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

Returns the distance from this vector to `v`.

.distanceSquared(v)→number

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

Returns the squared distance from this vector to `v`.

This variation avoids the `sqrt()` used in `distance()` and can be computationally simpler when comparing distances.

.isZero()→boolean

Returns `true` if both components of this vector are 0, `false` otherwise.

.isFinite()→boolean

Returns `true` if both components of this vector are finite real numbers, meaning not `NaN` or `Infinity`.