---
title: Namespace: vec2
source: https://vjmap.com/doc/Namespace_vec2.html
---

# Namespace: vec2

### Table of contents

#### Type aliases

- [valueType](#valuetype)

#### Functions

- [add](#add)
- [angle](#angle)
- [ceil](#ceil)
- [clone](#clone)
- [copy](#copy)
- [create](#create)
- [cross](#cross)
- [distance](#distance)
- [divide](#divide)
- [dot](#dot)
- [equals](#equals)
- [exactEquals](#exactequals)
- [floor](#floor)
- [forEach](#foreach)
- [fromValues](#fromvalues)
- [inverse](#inverse)
- [length](#length)
- [lerp](#lerp)
- [max](#max)
- [min](#min)
- [multiply](#multiply)
- [negate](#negate)
- [normalize](#normalize)
- [random](#random)
- [rotate](#rotate)
- [round](#round)
- [scale](#scale)
- [scaleAndAdd](#scaleandadd)
- [set](#set)
- [squaredDistance](#squareddistance)
- [squaredLength](#squaredlength)
- [str](#str)
- [subtract](#subtract)
- [transformMat2](#transformmat2)
- [transformMat2d](#transformmat2d)
- [transformMat3](#transformmat3)
- [transformMat4](#transformmat4)
- [zero](#zero)

### Type aliases

#### valueType

Ƭ **valueType**: [*vec2type*](#vec2type)

### Functions

#### add

▸ **add**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Adds two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** [*vec2type*](#vec2type)

out

___

#### angle

▸ **angle**(`a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): *number*

Get the angle between two 2D vectors

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | The first operand |
| `b` | [*vec2type*](#vec2type) | The second operand |

**Returns:** *number*

The angle in radians

___

#### ceil

▸ **ceil**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Math.ceil the components of a vec2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | vector to ceil |

**Returns:** [*vec2type*](#vec2type)

out

___

#### clone

▸ **clone**(`a`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Creates a new vec2 initialized with values from an existing vector

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | vector to clone |

**Returns:** [*vec2type*](#vec2type)

a new 2D vector

___

#### copy

▸ **copy**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Copy the values from one vec2 to another

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the source vector |

**Returns:** [*vec2type*](#vec2type)

out

___

#### create

▸ **create**(): [*vec2type*](#vec2type)

Creates a new, empty vec2

**Returns:** [*vec2type*](#vec2type)

a new 2D vector

___

#### cross

▸ **cross**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Computes the cross product of two vec2's
Note that the cross product must by definition produce a 3D vector

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** [*vec2type*](#vec2type)

out

___

#### distance

▸ **distance**(`a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): *number*

Calculates the euclidian distance between two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** *number*

distance between a and b

___

#### divide

▸ **divide**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Divides two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** [*vec2type*](#vec2type)

out

___

#### dot

▸ **dot**(`a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): *number*

Calculates the dot product of two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** *number*

dot product of a and b

___

#### equals

▸ **equals**(`a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): *boolean*

Returns whether or not the vectors have approximately the same elements in the same position.

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | The first vector. |
| `b` | [*vec2type*](#vec2type) | The second vector. |

**Returns:** *boolean*

True if the vectors are equal, false otherwise.

___

#### exactEquals

▸ **exactEquals**(`a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): *boolean*

Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | The first vector. |
| `b` | [*vec2type*](#vec2type) | The second vector. |

**Returns:** *boolean*

True if the vectors are equal, false otherwise.

___

#### floor

▸ **floor**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Math.floor the components of a vec2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | vector to floor |

**Returns:** [*vec2type*](#vec2type)

out

___

#### forEach

▸ `Const` **forEach**(`a`: *any*, `stride`: *any*, `offset`: *any*, `count`: *any*, `fn`: *any*, `arg`: *any*): *any*

Perform some operation over an array of vec2s.

**`function`**

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | *any* | the array of vectors to iterate over |
| `stride` | *any* | Number of elements between the start of each vec2. If 0 assumes tightly packed |
| `offset` | *any* | Number of elements to skip at the beginning of the array |
| `count` | *any* | Number of vec2s to iterate over. If 0 iterates over entire array |
| `fn` | *any* | Function to call for each vector in the array |
| `arg` | *any* | - |

**Returns:** *any*

a

___

#### fromValues

▸ **fromValues**(`x`: *number*, `y`: *number*): [*vec2type*](#vec2type)

Creates a new vec2 initialized with the given values

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `x` | *number* | X component |
| `y` | *number* | Y component |

**Returns:** [*vec2type*](#vec2type)

a new 2D vector

___

#### inverse

▸ **inverse**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Returns the inverse of the components of a vec2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | vector to invert |

**Returns:** [*vec2type*](#vec2type)

out

___

#### length

▸ **length**(`a`: [*vec2type*](#vec2type)): *number*

Calculates the length of a vec2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | vector to calculate length of |

**Returns:** *number*

length of a

___

#### lerp

▸ **lerp**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type), `t`: *number*): [*vec2type*](#vec2type)

Performs a linear interpolation between two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |
| `t` | *number* | interpolation amount, in the range [0-1], between the two inputs |

**Returns:** [*vec2type*](#vec2type)

out

___

#### max

▸ **max**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Returns the maximum of two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** [*vec2type*](#vec2type)

out

___

#### min

▸ **min**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Returns the minimum of two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** [*vec2type*](#vec2type)

out

___

#### multiply

▸ **multiply**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Multiplies two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** [*vec2type*](#vec2type)

out

___

#### negate

▸ **negate**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Negates the components of a vec2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | vector to negate |

**Returns:** [*vec2type*](#vec2type)

out

___

#### normalize

▸ **normalize**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Normalize a vec2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | vector to normalize |

**Returns:** [*vec2type*](#vec2type)

out

___

#### random

▸ **random**(`out`: [*vec2type*](#vec2type), `scale`: *number*): [*vec2type*](#vec2type)

Generates a random vector with the given scale

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `scale` | *number* | - |

**Returns:** [*vec2type*](#vec2type)

out

___

#### rotate

▸ **rotate**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type), `rad`: *number*): [*vec2type*](#vec2type)

Rotate a 2D vector

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | The receiving vec2 |
| `a` | [*vec2type*](#vec2type) | The vec2 point to rotate |
| `b` | [*vec2type*](#vec2type) | The origin of the rotation |
| `rad` | *number* | The angle of rotation in radians |

**Returns:** [*vec2type*](#vec2type)

out

___

#### round

▸ **round**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Math.round the components of a vec2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | vector to round |

**Returns:** [*vec2type*](#vec2type)

out

___

#### scale

▸ **scale**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: *number*): [*vec2type*](#vec2type)

Scales a vec2 by a scalar number

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the vector to scale |
| `b` | *number* | amount to scale the vector by |

**Returns:** [*vec2type*](#vec2type)

out

___

#### scaleAndAdd

▸ **scaleAndAdd**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type), `scale`: *number*): [*vec2type*](#vec2type)

Adds two vec2's after scaling the second operand by a scalar value

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |
| `scale` | *number* | the amount to scale b by before adding |

**Returns:** [*vec2type*](#vec2type)

out

___

#### set

▸ **set**(`out`: [*vec2type*](#vec2type), `x`: *number*, `y`: *number*): [*vec2type*](#vec2type)

Set the components of a vec2 to the given values

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `x` | *number* | X component |
| `y` | *number* | Y component |

**Returns:** [*vec2type*](#vec2type)

out

___

#### squaredDistance

▸ **squaredDistance**(`a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): *number*

Calculates the squared euclidian distance between two vec2's

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** *number*

squared distance between a and b

___

#### squaredLength

▸ **squaredLength**(`a`: [*vec2type*](#vec2type)): *number*

Calculates the squared length of a vec2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | vector to calculate squared length of |

**Returns:** *number*

squared length of a

___

#### str

▸ **str**(`a`: [*vec2type*](#vec2type)): *string*

Returns a string representation of a vector

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `a` | [*vec2type*](#vec2type) | vector to represent as a string |

**Returns:** *string*

string representation of the vector

___

#### subtract

▸ **subtract**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `b`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Subtracts vector b from vector a

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the first operand |
| `b` | [*vec2type*](#vec2type) | the second operand |

**Returns:** [*vec2type*](#vec2type)

out

___

#### transformMat2

▸ **transformMat2**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `m`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Transforms the vec2 with a mat2

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the vector to transform |
| `m` | [*vec2type*](#vec2type) | matrix to transform with |

**Returns:** [*vec2type*](#vec2type)

out

___

#### transformMat2d

▸ **transformMat2d**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `m`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Transforms the vec2 with a mat2d

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the vector to transform |
| `m` | [*vec2type*](#vec2type) | matrix to transform with |

**Returns:** [*vec2type*](#vec2type)

out

___

#### transformMat3

▸ **transformMat3**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `m`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Transforms the vec2 with a mat3
3rd vector component is implicitly '1'

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the vector to transform |
| `m` | [*vec2type*](#vec2type) | matrix to transform with |

**Returns:** [*vec2type*](#vec2type)

out

___

#### transformMat4

▸ **transformMat4**(`out`: [*vec2type*](#vec2type), `a`: [*vec2type*](#vec2type), `m`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Transforms the vec2 with a mat4
3rd vector component is implicitly '0'
4th vector component is implicitly '1'

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |
| `a` | [*vec2type*](#vec2type) | the vector to transform |
| `m` | [*vec2type*](#vec2type) | matrix to transform with |

**Returns:** [*vec2type*](#vec2type)

out

___

#### zero

▸ **zero**(`out`: [*vec2type*](#vec2type)): [*vec2type*](#vec2type)

Set the components of a vec2 to zero

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `out` | [*vec2type*](#vec2type) | the receiving vector |

**Returns:** [*vec2type*](#vec2type)

out


<a name="modulesvec3md"></a>

[vjmap](#readmemd) / [Exports](#modulesmd) / vec3
