Represents a vector with x and y components.
Fields
The x component of the vector (read-only).
local v = Vector.xy(10, -5)
local xValue = v.x -- 10
The y component of the vector (read-only).
local v = Vector.xy(10, -5)
local yValue = v.y -- -5
Constructors
xy(x: number, y: number) -> Vector
Creates a vector with the specified x and y components.
local v = Vector.xy(5, -2) -- 5 -2
origin
origin() -> Vector
Returns the zero vector (0, 0).
local origin = Vector.origin() -- 0 0
scaleAndAdd
scaleAndAdd(a: Vector, b: Vector, scale: number) -> Vector
Returns a + b * scale.
local r = Vector.scaleAndAdd(Vector.xy(1,2), Vector.xy(3,4), 2) -- 7 10
scaleAndSub
scaleAndSub(a: Vector, b: Vector, scale: number) -> Vector
Returns a - b * scale.
local r = Vector.scaleAndSub(Vector.xy(7,10), Vector.xy(3,4), 2) -- 1 2
lerp
lerp(from: Vector, to: Vector, t: number) -> Vector
Returns the linear interpolation between the to and from
vector, using t where 0 returns the vector and 1 returns the other.
local p = Vector.lerp(Vector.xy(0,0), Vector.xy(8,0), 0.25) -- 2 0
normalized
normalized(v: Vector) -> Vector
Returns a normalized copy of the given vector.
local n = Vector.normalized(Vector.xy(10, 0)) -- Vector.xy(1, 0)
Static Functions
distance
distance(a: Vector, b: Vector) -> number
Returns the distance between the two vectors.
local d = Vector.distance(Vector.xy(3, 4), Vector.xy(0, 0)) -- 5
distanceSquared
distanceSquared(a: Vector, b: Vector) -> number
Returns the squared distance between the two vectors.
local d2 = Vector.distanceSquared(Vector.xy(3,4), Vector.xy(0,0)) -- 25
dot
dot(a: Vector, b: Vector) -> number
Returns the dot product of the two vectors.
local dp = Vector.dot(Vector.xy(1,2), Vector.xy(3,4)) -- 11 (1*3 + 2*4)
cross
cross(a: Vector, b: Vector) -> number
Returns the cross product of two vectors (z-component of the 3D cross product).
local c = Vector.cross(Vector.xy(1, 0), Vector.xy(0, 1)) -- 1
length
length(v: Vector) -> number
Returns the length of the given vector.
local len = Vector.length(Vector.xy(3, 4)) -- 5
lengthSquared
lengthSquared(v: Vector) -> number
Returns the squared length of the given vector.
local len2 = Vector.lengthSquared(Vector.xy(3, 4)) -- 25
Methods
length
length()
Deprecated - Use Vector.length instead.
Returns the length of the vector.
lengthSquared
lengthSquared()
Deprecated - Use Vector.lengthSquared instead.
Returns the squared length of the vector.
local v = Vector.xy(3, 4)
local len2 = v:lengthSquared() -- 25
normalized
normalized()
Deprecated - Use Vector.normalized instead.
Returns a normalized copy of the vector. If the length is zero,
the result is the zero vector.
local v = Vector.xy(10, 0)
local n = v:normalized() -- 1 0
__eq
__eq(rhs: Vector)
Returns true if the two vectors have equal components.
local a = Vector.xy(1, 2)
local b = Vector.xy(1, 2)
local c = Vector.xy(2, 1)
print(a == b) -- true
print(a == c) -- false
__unm
__unm()
Returns the negated vector.
local v = Vector.xy(2, -3)
local neg = -v -- -2 3
__add
__add(rhs: Vector)
Returns the sum of two vectors.
local a = Vector.xy(2, 3)
local b = Vector.xy(-1, 5)
local c = a + b -- 1 8
__sub
__sub(rhs: Vector)
Returns the difference between two vectors.
local a = Vector.xy(2, 3)
local b = Vector.xy(-1, 5)
local c = a - b -- 3 -2
__mul
__mul(rhs: number)
Returns the vector scaled by the given number.
local v = Vector.xy(3, -2)
local doubled = v * 2 -- 6 -4
__div
__div(rhs: number)
Returns the vector divided by the given number.
local v = Vector.xy(6, -4)
local half = v / 2 -- 3 -2
distance
distance(other: Vector)
Deprecated - Use Vector.distance instead.
Returns the distance to the other vector.
local a = Vector.xy(0, 0)
local b = Vector.xy(3, 4)
print(a:distance(b)) -- 5
distanceSquared
distanceSquared(other: Vector)
Deprecated - Use Vector.distanceSquared instead.
Returns the squared distance to the other vector.
local a = Vector.xy(0, 0)
local b = Vector.xy(3, 4)
print(a:distanceSquared(b)) -- 25
dot
dot(other: Vector)
Deprecated - Use Vector.dot instead.
Returns the dot product of the vector and the other vector.
local a = Vector.xy(1, 2)
local b = Vector.xy(3, 4)
print(a:dot(b)) -- 11 (1*3 + 2*4)
lerp
lerp(other: Vector, t: number)
Deprecated - Use Vector.lerp instead.
Returns the linear interpolation between the vector and the other
vector, using t where 0 returns the vector and 1 returns the other.
local a = Vector.xy(0, 0)
local b = Vector.xy(10, 0)
local p = a:lerp(b, 0.5) -- 5 0