[vec] Add functions to compute angles, minor style fixes.
This commit is contained in:
parent
2959abc4f6
commit
00d41367d6
21
vec.lua
21
vec.lua
|
@ -10,6 +10,7 @@
|
||||||
local min, max = math.min, math.max
|
local min, max = math.min, math.max
|
||||||
local sin, cos = math.sin, math.cos
|
local sin, cos = math.sin, math.cos
|
||||||
local abs = math.abs
|
local abs = math.abs
|
||||||
|
local atan2 = math.atan2
|
||||||
local sqrt = math.sqrt
|
local sqrt = math.sqrt
|
||||||
|
|
||||||
local vec = {}
|
local vec = {}
|
||||||
|
@ -172,28 +173,32 @@ function vec.dist3(x1,y1,z1, x2,y2,z2)
|
||||||
return sqrt(dx*dx + dy*dy + dz*dz)
|
return sqrt(dx*dx + dy*dy + dz*dz)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Rotate point (px,py) around (ox,oy) by the provided
|
--- Rotate vector (vx,vy) around (ox,oy) by the provided
|
||||||
-- sine and cosine.
|
-- sine and cosine.
|
||||||
--
|
--
|
||||||
-- This function should only be used for (valuable)
|
-- This function should only be used for (valuable)
|
||||||
-- optimization purposes.
|
-- optimization purposes.
|
||||||
function vec.rotatesincos(px,py, sina,cosa, ox,oy)
|
function vec.rotatesincos(vx,vy, sina,cosa, ox,oy)
|
||||||
return ox + cosa*px - sina*py,
|
return ox + cosa*vx - sina*vy,
|
||||||
oy + sina*px + cosa*py
|
oy + sina*vx + cosa*vy
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Rotate point (px,py) around (ox,oy) about rot radians.
|
--- Rotate vector (vx,vy) around (ox,oy) about rot radians.
|
||||||
function vec.rotatepoint(px,py, rot, ox,oy)
|
function vec.rotate(vx,vy, rot, ox,oy)
|
||||||
ox = ox or 0
|
ox = ox or 0
|
||||||
oy = oy or 0
|
oy = oy or 0
|
||||||
|
|
||||||
local sina,cosa = sin(rot),cos(rot)
|
local sina,cosa = sin(rot),cos(rot)
|
||||||
|
|
||||||
-- vec.rotatesincos(px,py, sina,cosa, ox,oy)
|
-- vec.rotatesincos(px,py, sina,cosa, ox,oy)
|
||||||
return ox + cosa*px - sina*py,
|
return ox + cosa*vx - sina*vy,
|
||||||
oy + sina*px + cosa*py
|
oy + sina*vx + cosa*vy
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function vec.angle(x,y) return atan2(y,x) end
|
||||||
|
|
||||||
|
function vec.angleto(x1,y1, x2,y2) return atan2(y1,x1) - atan2(y2,x2) end
|
||||||
|
|
||||||
--- Transform world coordinates to screen coordinates.
|
--- Transform world coordinates to screen coordinates.
|
||||||
--
|
--
|
||||||
-- @param x (number) World coordinate X.
|
-- @param x (number) World coordinate X.
|
||||||
|
|
Loading…
Reference in New Issue