local function b(b,y,V,S)local X=y:Cross(S)local U=V.x-b.x local G=V.y-b.y local L=V.z-b.z local D=Det3x3(y.x,-S.x,X.x,y.y,-S.y,X.y,y.z,-S.z,X.z)if D==0 then return ZERO_VECTOR3 end local g=Det3x3(U,-S.x,X.x,G,-S.y,X.y,L,-S.z,X.z)/D local F=Det3x3(y.x,U,X.x,y.y,G,X.y,y.z,L,X.z)/D local E=b+g*y local k=V+F*S local m=E+.5*(k-E)if(k-E).Magnitude<.25 then return m 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
