Rewrite math.hypot() to compute more directly, with far fewer operations.
Rewrite vector.length() and vector.distance() to each use a single
sqrt operation, rather than two (which they were doing via math.hypot).
Add vector.length_square() and vector.distance_square(), which return
squared distance more cheaply by omitting the sqrt operation.
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
a.z == b.z
end
+function vector.length_square(v)
+ return v.x*v.x + v.y*v.y + v.z*v.z
+end
+
function vector.length(v)
- return math.hypot(v.x, math.hypot(v.y, v.z))
+ return math.sqrt(vector.length_square(v))
end
function vector.normalize(v)
}
end
-function vector.distance(a, b)
+function vector.distance_square(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 x*x + y*y + z*z
+end
+
+function vector.distance(a, b)
+ return math.sqrt(vector.distance_square(a, b))
end
function vector.direction(pos1, pos2)
^ x is a table or the x position.
vector.direction(p1, p2) -> vector
vector.distance(p1, p2) -> number
+vector.distance_square(p1, p2) -> number
+^ Cheaper than squaring vector.distance(p1, p2)
vector.length(v) -> number
+vector.length_square(v) -> number
+^ Cheaper than squaring vector.length(v)
vector.normalize(v) -> vector
vector.round(v) -> vector
vector.equals(v1, v2) -> bool