mirror of
https://github.com/minetest/minetest.git
synced 2024-11-04 14:53:45 +01:00
Optimize vector length calculations (#11549)
This commit is contained in:
parent
a7188bd6f5
commit
d36dca3aba
@ -209,14 +209,7 @@ end
|
|||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
function math.hypot(x, y)
|
function math.hypot(x, y)
|
||||||
local t
|
return math.sqrt(x * x + y * y)
|
||||||
x = math.abs(x)
|
|
||||||
y = math.abs(y)
|
|
||||||
t = math.min(x, y)
|
|
||||||
x = math.max(x, y)
|
|
||||||
if x == 0 then return 0 end
|
|
||||||
t = t / x
|
|
||||||
return x * math.sqrt(1 + t * t)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
@ -67,7 +67,7 @@ metatable.__eq = vector.equals
|
|||||||
-- unary operations
|
-- unary operations
|
||||||
|
|
||||||
function vector.length(v)
|
function vector.length(v)
|
||||||
return math.hypot(v.x, math.hypot(v.y, v.z))
|
return math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
|
||||||
end
|
end
|
||||||
-- Note: we can not use __len because it is already used for primitive table length
|
-- Note: we can not use __len because it is already used for primitive table length
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ function vector.distance(a, b)
|
|||||||
local x = a.x - b.x
|
local x = a.x - b.x
|
||||||
local y = a.y - b.y
|
local y = a.y - b.y
|
||||||
local z = a.z - b.z
|
local z = a.z - b.z
|
||||||
return math.hypot(x, math.hypot(y, z))
|
return math.sqrt(x * x + y * y + z * z)
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector.direction(pos1, pos2)
|
function vector.direction(pos1, pos2)
|
||||||
|
Loading…
Reference in New Issue
Block a user