local function l(l,O,h,A)local K=O:Cross(A)local Q=h.x-l.x local f=h.y-l.y local C=h.z-l.z local V=Det3x3(O.x,-A.x,K.x,O.y,-A.y,K.y,O.z,-A.z,K.z)if V==0 then return ZERO_VECTOR3 end local H=Det3x3(Q,-A.x,K.x,f,-A.y,K.y,C,-A.z,K.z)/V local j=Det3x3(O.x,Q,K.x,O.y,f,K.y,O.z,C,K.z)/V local e=l+H*O local S=h+j*A local U=e+.5*(S-e)if(S-e).Magnitude<.25 then return U else return ZERO_VECTOR3 end end
-- Smart Circle mode needs the intersection of 2 rays that are known to be in the same plane
-- because they are generated from cross products with a common vector. This function is computing
-- that intersection, but it's actually the general solution for the point halfway between where
-- two skew lines come nearest to each other, which is more forgiving.
local function RayIntersection(p0, v0, p1, v1)
	local v2 = v0:Cross(v1)
	local d1 = p1.x - p0.x
	local d2 = p1.y - p0.y
	local d3 = p1.z - p0.z
	local denom = Det3x3(v0.x, -v1.x, v2.x, v0.y, -v1.y, v2.y, v0.z, -v1.z, v2.z)

	if denom == 0 then
		return ZERO_VECTOR3 -- No solution (rays are parallel)
	end

	local t0 = Det3x3(d1, -v1.x, v2.x, d2, -v1.y, v2.y, d3, -v1.z, v2.z) / denom
	local t1 = Det3x3(v0.x, d1, v2.x, v0.y, d2, v2.y, v0.z, d3, v2.z) / denom
	local s0 = p0 + t0 * v0
	local s1 = p1 + t1 * v1
	local s = s0 + 0.5 * (s1 - s0)

	-- 0.25 studs is a threshold for deciding if the rays are
	-- close enough to be considered intersecting, found through testing
	if (s1 - s0).Magnitude < 0.25 then
		return s
	else
		return ZERO_VECTOR3
	end
end
