Cl Vector

Vector class.

More managable than the vector2d utility library in some cases.

Derived from Object.

Usage:

     local vec1 = Vector(1, 3)
     local vec2 = Vector({ 1, 3 })
     local vec3 = Vector({ x = 1, y = 3 })
     -- All of these create the same vector with x = 1 and y = 3.
     -- Advantage over util library: easy to chain and use operators with!
     local newVec = Vector(1,2):rotateBy( math.pi ):snap( 0.5, 0.5 ) + Vector( 1, 1 )
     -- Also supports unary operations, equality checks, and string concatenation
     local minVec = -Vector(1,1)
     local isTrue = Vector(1,1) == Vector(1,1)
     local isFalse = Vector(1,1) == Vector(1,2)
     local str = "Zero vector: "..Vector(0,0)

Class Vector

Vector:unpack () Unpacks vector, returns x and y.
Vector:copy () Copies this vector.
Vector:distance (vec) Get distance to other vector.
Vector:distance2 (vec) Get distance^2 to other vector.
Vector:length () Get the length of this vector.
Vector:length2 () Get the length^2 of this vector.
Vector:angle () Get the angle (in radians) of the vector.
Vector:snap (gx, gy) Snap this vector to the given grid.
Vector:approach (vec, step) Approach another vector at the given step.
Vector:round ([d=0]) Rounds this vector off.
Vector:perpendicular () Gets perpendicular vector respective to this vector.
Vector:dot (vec) Get dot product of this vector with the other vector.
Vector:cross (vec) Get cross product of this vector with the other vector.
Vector:projectOn (vec) Project this vector onto the other.
Vector:mirrorOn (vec) Mirror this vector respective to the other.
Vector:normalize () Normalize this vector.
Vector:getNormal () Get normal vector of this vector.
Vector:trim (maxLength) Trim this vector to the given length.
Vector:getTrimmed (maxLength) Get trimmed vector of this vector.
Vector:rotate (r) Rotate vector by the given angle.
Vector:getRotated (r) Get rotated vector of this vector.
Vector:multiply (s) Multiply this vector by a scalar or other vector.
Vector:divide (s) Divide this vector by a scalar or other vector.
Vector:add (v) Add this vector to another vector.
Vector:subtract (v) Subtract another vector from this vector.


Class Vector

Vector:unpack ()
Unpacks vector, returns x and y.

Returns:

  1. number x
  2. number y
Vector:copy ()
Copies this vector.

Returns:

    Vector vector
Vector:distance (vec)
Get distance to other vector.

Parameters:

Returns:

    number distance

See also:

Vector:distance2 (vec)
Get distance^2 to other vector.

Parameters:

Returns:

    number distance squared

See also:

Vector:length ()
Get the length of this vector.

Returns:

    number length

See also:

Vector:length2 ()
Get the length^2 of this vector.

Returns:

    number length squared

See also:

Vector:angle ()
Get the angle (in radians) of the vector.

Returns:

    number angle

See also:

Vector:snap (gx, gy)
Snap this vector to the given grid.

Parameters:

  • gx number grid size x
  • gy number grid size y

Returns:

    Vector self

See also:

Vector:approach (vec, step)
Approach another vector at the given step.

Parameters:

  • vec Vector other vector
  • step number step size (distance to cover towards other vector)

Returns:

    Vector self

See also:

Vector:round ([d=0])
Rounds this vector off.

Parameters:

  • d number number of decimals to round to (default 0)

Returns:

    Vector self
Vector:perpendicular ()
Gets perpendicular vector respective to this vector.

Returns:

    Vector vector new perpendicular vector

See also:

Vector:dot (vec)
Get dot product of this vector with the other vector.

Parameters:

See also:

Vector:cross (vec)
Get cross product of this vector with the other vector.

Parameters:

See also:

Vector:projectOn (vec)
Project this vector onto the other.

Parameters:

Returns:

    Vector projected vector

See also:

Vector:mirrorOn (vec)
Mirror this vector respective to the other.

Parameters:

Returns:

    Vector mirrored vector

See also:

Vector:normalize ()
Normalize this vector.

Returns:

    Vector self

See also:

Vector:getNormal ()
Get normal vector of this vector.

Returns:

    Vector normalized vector

See also:

Vector:trim (maxLength)
Trim this vector to the given length.

Parameters:

  • maxLength number trim length

Returns:

    Vector self

See also:

Vector:getTrimmed (maxLength)
Get trimmed vector of this vector.

Parameters:

  • maxLength number trim length

Returns:

    Vector trimmed vector

See also:

Vector:rotate (r)
Rotate vector by the given angle.

Parameters:

  • r number angle (radians) to rotate by

Returns:

    Vector self

See also:

Vector:getRotated (r)
Get rotated vector of this vector.

Parameters:

  • r number angle (radians) to rotate by

Returns:

    Vector rotated vector

See also:

Vector:multiply (s)
Multiply this vector by a scalar or other vector. Used by the * operator.

Parameters:

  • s number, Vector or table scalar or vector or table in format { [x], [y] } or { x = [x], y = [y] }

Returns:

    Vector self

Usage:

    local mulVec = Vector(1,3) * Vector(2,1) * 3
     -- mulVec is Vector(6, 9)
Vector:divide (s)
Divide this vector by a scalar or other vector. Used by the / operator.

Parameters:

  • s number, Vector or table scalar or vector or table in format { [x], [y] } or { x = [x], y = [y] }

Returns:

    Vector self

Usage:

    local divVec = Vector(6,9) / Vector(2,1) / 3
     -- divVec is Vector(1, 3)
Vector:add (v)
Add this vector to another vector. Used by the + operator.

Parameters:

  • v Vector or table vector or table in format { [x], [y] } or { x = [x], y = [y] }

Returns:

    Vector self

Usage:

    local addVec = Vector(12, 1) + Vector(3, 2)
     -- addVec is Vector(15, 3)
Vector:subtract (v)
Subtract another vector from this vector. Used by the - operator.

Parameters:

  • v Vector or table vector or table in format { [x], [y] } or { x = [x], y = [y] }

Returns:

    Vector self

Usage:

    local subVec = Vector(15, 3) - Vector(3, 2)
     -- subVec is Vector(12, 1)
generated by LDoc 1.4.3 Last updated 2015-04-20 03:01:38