Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Optimize vector length calculations (#11549)
  • Loading branch information
Kalabasa committed Aug 27, 2021
1 parent a7188bd commit d36dca3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
9 changes: 1 addition & 8 deletions builtin/common/misc_helpers.lua
Expand Up @@ -209,14 +209,7 @@ end

--------------------------------------------------------------------------------
function math.hypot(x, y)
local t
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)
return math.sqrt(x * x + y * y)
end

--------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions builtin/common/vector.lua
Expand Up @@ -67,7 +67,7 @@ metatable.__eq = vector.equals
-- unary operations

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
-- Note: we can not use __len because it is already used for primitive table length

Expand Down Expand Up @@ -104,7 +104,7 @@ function vector.distance(a, b)
local x = a.x - b.x
local y = a.y - b.y
local z = a.z - b.z
return math.hypot(x, math.hypot(y, z))
return math.sqrt(x * x + y * y + z * z)
end

function vector.direction(pos1, pos2)
Expand Down

0 comments on commit d36dca3

Please sign in to comment.