# Module `vec2`

Represents a 2D vector in Lua.

This module allows you to manipulate 2D vectors. Usage is generally as follows:

``` my_vec = vec2.new( 3, 2 ) -- my_vec is now (3,2)
my_vec:add( 5, 3 ) -- my_vec is now (8,5)
my_vec = my_vec * 3 -- my_vec is now (24,15)
your_vec = vec2.new( 5, 2 ) -- your_vec is now (5,2)
my_vec = my_vec - your_vec -- my_vec is now (19,13)
```

To call members of the metatable always use:

``` vector:function( param )
```

## Functions

 new (x, y) Creates a new vector. newP ([m=0[, a=0]]) Creates a new vector using polar coordinates. add (v, x, y) Adds two vectors or a vector and some cartesian coordinates. sub (v, x, y) Subtracts two vectors or a vector and some cartesian coordinates. mul (v, mod) Multiplies a vector by a number. div (v, mod) Divides a vector by a number. get (v) Gets the cartesian positions of the vector. polar (v) Gets polar coordinates of a vector. set (v, x, y) Sets the vector by cartesian coordinates. setP (v, m, a) Sets the vector by polar coordinates. dist (v, v2) Gets the distance from the Vec2. dist2 (v, v2) Gets the squared distance from the Vec2 (saves a sqrt()) mod (v) Gets the modulus of the vector.

## Functions

new (x, y)
Creates a new vector. ( x, y )

### Parameters:

• x number If set, the X value for the new vector.
• y number If set, the Y value for the new vector.

### Returns:

Vec2 The new vector.

### Usage:

• `vec2.new( 5, 3 ) -- creates a vector at (5,3)`
• `vec2.new() -- creates a vector at (0,0)`
newP ([m=0[, a=0]])
Creates a new vector using polar coordinates. ( m, a )

### Parameters:

• m number If set, the modulus for the new vector. (default 0)
• a number If set, the angle for the new vector, in degrees. (default 0)

### Returns:

Vec2 The new vector.

### Usage:

• `vec2.newP( 1000, 90 ) -- creates a vector at (0,1000)`
• `vec2.newP() -- creates a vector at (0,0)`
Adds two vectors or a vector and some cartesian coordinates.

If x is a vector it adds both vectors, otherwise it adds cartesian coordinates to the vector. ( v, x, y )

### Parameters:

• v Vector Vector getting stuff subtracted from.
• x number or Vec2 X coordinate or vector to add to.
• y number or nil Y coordinate or nil to add to.

### Returns:

Vec2 The result of the vector operation.

### Usage:

• `my_vec = my_vec + your_vec`
• `my_vec:add( your_vec )`
• `my_vec:add( 5, 3 )`
sub (v, x, y)
Subtracts two vectors or a vector and some cartesian coordinates.

If x is a vector it subtracts both vectors, otherwise it subtracts cartesian coordinates to the vector. ( v, x, y )

### Parameters:

• v Vec2 Vector getting stuff subtracted from.
• x number or Vec2 X coordinate or vector to subtract.
• y number or nil Y coordinate or nil to subtract.

### Returns:

Vec2 The result of the vector operation.

### Usage:

• `my_vec = my_vec - your_vec`
• `my_vec:sub( your_vec )`
• `my_vec:sub( 5, 3 )`
mul (v, mod)
Multiplies a vector by a number. ( v, mod )

### Parameters:

• v Vec2 Vector to multiply.
• mod number Amount to multiply by.

### Returns:

Vec2 The result of the vector operation.

### Usage:

• `my_vec = my_vec * 3`
• `my_vec:mul( 3 )`
div (v, mod)
Divides a vector by a number. ( v, mod )

### Parameters:

• v Vec2 Vector to divide.
• mod number Amount to divide by.

### Returns:

Vec2 The result of the vector operation.

### Usage:

• `my_vec = my_vec / 3`
• `my_vec:div(3)`
get (v)
Gets the cartesian positions of the vector. (v)

### Parameters:

• v Vec2 Vector to get position of.

### Returns:

1. number X position of the vector.
2. number Y position of the vector.

### Usage:

`x,y = my_vec:get()`
polar (v)
Gets polar coordinates of a vector.

The angle is in degrees, not radians. (v)

### Parameters:

• v Vec2 Vector to get polar coordinates of.

### Returns:

1. number The modulus of the vector.
2. number The angle of the vector.

### Usage:

`modulus, angle = my_vec:polar()`
set (v, x, y)
Sets the vector by cartesian coordinates. ( v, x, y )

### Parameters:

• v Vec2 Vector to set coordinates of.
• x number X coordinate to set.
• y number Y coordinate to set.

### Usage:

`my_vec:set(5, 3) -- my_vec is now (5,3)`
setP (v, m, a)
Sets the vector by polar coordinates. ( v, m, a )

### Parameters:

• v Vec2 Vector to set coordinates of.
• m number Modulus to set.
• a number Angle to set, in degrees.

### Usage:

`my_vec:setP( 1, 90 ) -- my_vec is now (0,1)`
dist (v, v2)
Gets the distance from the Vec2. ( v, v2 )

### Parameters:

• v Vec2 Vector to act as origin.
• v2 Vec2 Vector to get distance from, uses origin (0,0) if not set.

### Returns:

number The distance calculated.

### Usage:

• `my_vec:dist() -- Gets length of the vector (distance from origin).`
• `my_vec:dist( your_vec ) -- Gets distance from both vectors (your_vec - my_vec).`
dist2 (v, v2)
Gets the squared distance from the Vec2 (saves a sqrt()) ( v, v2 )

### Parameters:

• v Vec2 Vector to act as origin.
• v2 Vec2 Vector to get squared distance from, uses origin (0,0) if not set.

### Returns:

number The distance calculated.

### Usage:

• `my_vec:dist2() -- Gets squared length of the vector (distance squared from origin).`
• `my_vec:dist2( your_vec ) -- Gets squared distance from both vectors (your_vec - my_vec)^2.`
mod (v)
Gets the modulus of the vector. (v)

### Parameters:

• v Vec2 Vector to get modulus of.

### Returns:

number The modulus of the vector.
generated by LDoc 1.4.6 Last updated 2020-11-30 07:37:22