Vector 2d for Love2d and Lua. Inspired on Vector2() of Godot Engine.
Download vector.lua and write on main.lua
vector = require "vector"| number | x |
| number | y |
Example:
a = vector(5, 4)
print(a:string()) --> vector(5, 4)
b = vector(3, 2)
if a.x > b.x then print(a .. b) end --> vector(5, 4)vector(3, 2)
c = a + b
print(c:string()) --> vector(8, 6)
b = -b --> vector(-3, -2)| Constant | Value |
|---|---|
| vector.UP | vector(0,-1) |
| vector.DOWN | vector(0,1) |
| vector.LEFT | vector(-1, 0) |
| vector.RIGHT | vector(1, 0) |
| vector.ZERO | vector(0,0) |
| vector.ONE | vector(1,1) |
vector:string()Returns the vector as string, also, tostring(vector) works.
vector:angle()Returns the angle of vector in radians.
vector:normalized()Set the vector normalized. Normalizing a vector means reducing its length to 1 while preserving its direction.
vector:distanceTo(vector)Returns the distance between 2 vectors.
vector:distanceSquaredTo(vector)Returns the distance squared between 2 vectors.
vector:distance()Returns the distance of the vector.
vector:distanceSquared()Returns the distance squared of the vector.
vector:dot(vector)Returns the dot product of 2 vectors.
vector:perpDot(vector)Returns the Perp Dot Product of 2 vectors.
vector:toPolar(angle, lenght)Set the vector to the polar coordinate.
vector:abs()Set the absolute value of the vector.
vector:round(decimals)Set the vector with decimals, 0 or ignore for integer number. Example: vector(2.5, 3.4):round() --> vector(3, 3)
vector:rotated(phi)Set the vector rotated by phiradians.
vector:cross(vector)Returns the 2 dimensional analog of the cross product with the given vector.
vector:perpendicular()Set the vector rotated 90°.
vector:lerpTo(vector, time)Set the result of the linear interpolation between this vector and vector by amount time. time is in the range of 0.0 - 1.0, representing the amount of interpolation.
vector:unpack()Returns x, y value of the vector.
this = vector(a, b) + vector(c, d) --> vector(a+c, b+d)this = vector(a, b) - vector(c, d) --> vector(a-c, b-d)this = vector(a, b) * vector(c, d) --> vector(a*c, b*d)this = vector(a, b) / vector(c, d) --> vector(a/c, b/d)this = vector(a, b) ^ c --> vector(a^c, b^c)this = vector(a, b) .. vector(c, d) --> "vector(a, b)vector(c, d)"