Skip to content

Class: Vector

Michael Ebens edited this page Jan 22, 2016 · 1 revision

Vector is a class that represents a two-dimensional vector (or, point). Thanks to hump.vector, on which this class is largely based.

Properties

x
The X coordinate of this vector.

y
The Y coordinate of this vector.

Methods

initialize(x, y)
Initialises the vector.

x: The initial value for the X coordinate. The default is 0.
x: The initial value for the Y coordinate. The default is 0.

cross(v)
Returns the cross product (a number) of both vectors. Equals the area of the parallelogram spanned by both vectors.

v: The other vector.

len()
Returns the length (a number) of this vector, essentially using this operation: math.sqrt(v.x * v.x + v.y * v.y).

lenSq()
Returns the length as above, but squared.

normalize()
Normalises and returns this vector, meaning that this vector will face the same direction, but with a length of 1.

normalized()
Copies this vector and normalises it as above. The copy is then returned.

perpendicular()
Copies this vector and performs a quick 90 degree rotation (it's faster than v:rotated(math.tau / 4)). The copy is then returned.

projectOn(v) Projects this vector onto another and returns the resulting vector.

v: The vector to project this one onto.

rotate(by)
Rotates and returns this vector by the amount specified.

by: The amount to rotate this vector by (in radians).

rotated(by) Creates a copy of this and rotates it by the amount specified. The copy is then returned.

by: The amount to rotate the copy by (in radians).

set(x, y)
Sets this vector's X and Y coordinates.

x: The X coordinate to set. If nil, the X coordinate won't be set.
y: The Y coordinate to set. If nil, the Y coordinate won't be set.

unpack()
Returns both the X and Y coordinates of this vector.

Operators

__add(v)
If v is a vector, it will add the two vectors' coordinates together and return the resulting vector. If v is a number it will return a copy of this vector with v added to both coordinates.

__div(v)
If v is a vector, it will divide this vector's coordinates by v's and return the resulting vector. If v is a number it will return a copy of this vector with both coordinates divided by v.

__eq(v)
Returns true if the coordinates of this vector and v are both equal; false otherwise.

__le(v)
Returns true if self.x < v.x or (self.x == v.x and self.y < v.y).

__lt(v)
Returns true if the coordinates of this vector and v are both less than or equal to one another; false otherwise.

__mul(v)
If v is a vector, it will multiply the two vectors' coordinates and return the resulting vector. If v is a number it will return a copy of this vector with both coordinates multiplied by v.

__sub(v)
If v is a vector, it will subtract v's coordinates by this vector's coordinates and return the resulting vector. If v is a number it will return a copy of this vector with both coordinates divided by v.

__tostring()
Returns a string in the format of "(x,y)".

__unm()
Copies this vector and negates both coordinates. The copy is then returned.