Home

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)
 

Functions

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


Functions

vec2.add (v, x, y)
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.

Parameters

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

Usage

  • my_vec = my_vec + your_vec
  • my_vec:add( your_vec )
  • my_vec:add( 5, 3 )

Return value:

The result of the vector operation.
vec2.dist (v, v2)
Gets the distance from the Vec2.

Parameters

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

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).

Return value:

The distance calculated.
vec2.dist2 (v, v2)
Gets the squared distance from the Vec2 (saves a sqrt())

Parameters

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

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.

Return value:

The distance calculated.
vec2.div (v, mod)
Divides a vector by a number.

Parameters

  • v: Vector to divide.
  • mod: Amount to divide by.

Usage

  • my_vec = my_vec / 3
  • my_vec:div(3)

Return value:

The result of the vector operation.
vec2.get (v)
Gets the cartesian positions of the vector.

Parameters

  • v: Vector to get position of.

Usage:

x,y = my_vec:get()

Return value:

X and Y position of the vector.
vec2.mod (v)
Gets the modulus of the vector.

Parameters

  • v: Vector to get modulus of.

Return value:

The modulus of the vector.
vec2.mul (v, mod)
Multiplies a vector by a number.

Parameters

  • v: Vector to multiply.
  • mod: Amount to multiply by.

Usage

  • my_vec = my_vec * 3
  • my_vec:mul( 3 )

Return value:

The result of the vector operation.
vec2.new (x, y)
Creates a new vector.

Parameters

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

Usage

  • vec2.new( 5, 3 ) -- creates a vector at (5,3)
  • vec2.new() -- creates a vector at (0,0)

Return value:

The new vector.
vec2.newP (m, a)
Creates a new vector using polar coordinates.

Parameters

  • m: If set, the modulus for the new vector.
  • a: If set, the angle for the new vector, in degrees.

Usage

  • vec2.newP( 1000, 90 ) -- creates a vector at (0,1000)
  • vec2.newP() -- creates a vector at (0,0)

Return value:

The new vector.
vec2.polar (v)
Gets polar coordinates of a vector. The angle is in degrees, not radians.

Parameters

  • v: Vector to get polar coordinates of.

Usage:

modulus, angle = my_vec:polar()

Return value:

The modulus and angle of the vector.
vec2.set (v, x, y)
Sets the vector by cartesian coordinates.

Parameters

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

Usage:

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

Parameters

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

Usage:

my_vec:setP( 1, 90 ) -- my_vec is now (0,1)
vec2.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.

Parameters

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

Usage

  • my_vec = my_vec - your_vec
  • my_vec:sub( your_vec )
  • my_vec:sub( 5, 3 )

Return value:

The result of the vector operation.

Valid XHTML 1.0!