[vec] Add functions to compute angles, minor style fixes.

This commit is contained in:
Lorenzo Cogotti 2023-06-01 22:47:35 +02:00
parent 2959abc4f6
commit 00d41367d6
1 changed files with 13 additions and 8 deletions

21
vec.lua
View File

@ -10,6 +10,7 @@
local min, max = math.min, math.max
local sin, cos = math.sin, math.cos
local abs = math.abs
local atan2 = math.atan2
local sqrt = math.sqrt
local vec = {}
@ -172,28 +173,32 @@ function vec.dist3(x1,y1,z1, x2,y2,z2)
return sqrt(dx*dx + dy*dy + dz*dz)
end
--- Rotate point (px,py) around (ox,oy) by the provided
--- Rotate vector (vx,vy) around (ox,oy) by the provided
-- sine and cosine.
--
-- This function should only be used for (valuable)
-- optimization purposes.
function vec.rotatesincos(px,py, sina,cosa, ox,oy)
return ox + cosa*px - sina*py,
oy + sina*px + cosa*py
function vec.rotatesincos(vx,vy, sina,cosa, ox,oy)
return ox + cosa*vx - sina*vy,
oy + sina*vx + cosa*vy
end
--- Rotate point (px,py) around (ox,oy) about rot radians.
function vec.rotatepoint(px,py, rot, ox,oy)
--- Rotate vector (vx,vy) around (ox,oy) about rot radians.
function vec.rotate(vx,vy, rot, ox,oy)
ox = ox or 0
oy = oy or 0
local sina,cosa = sin(rot),cos(rot)
-- vec.rotatesincos(px,py, sina,cosa, ox,oy)
return ox + cosa*px - sina*py,
oy + sina*px + cosa*py
return ox + cosa*vx - sina*vy,
oy + sina*vx + cosa*vy
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.
--
-- @param x (number) World coordinate X.