sorcery  math.lua at [01f4ba8ddc]

File lib/math.lua artifact 80f52c4aaf part of check-in 01f4ba8ddc


local fn = {}

fn.vsep = function(vec) -- separate a vector into a direction + magnitude
	local magnitude = math.max(math.abs(vec.x), math.abs(vec.y), math.abs(vec.z))
	local inv = 1 / magnitude
	return vector.multiply(vec,inv), magnitude
end

fn.vdcomp = function(dist,v1,v2) -- compare the distance between two points
	-- (cheaper than calculating distance outright)
	local d if v2
		then d = vector.subtract(v1,v2)
		else d = v1
	end
	local dsq = (d.x ^ 2) + (d.y ^ 2) + (d.z ^ 2)
	return dsq / (dist^2)
	-- [0,1) == less then
	-- 1 == equal
	-- >1 == greater than
end

return fn