plane bezier seg intersection code some may find useful…
enjoy
--****************************************************
fn build_spline =
(
spl = splineShape();
crv = addNewSpline spl;
addKnot spl crv #bezier #curve [-40.9457,-47.6849,28.5503] [-36.5278,-55.1807,114.356] [-45.3636,-40.1891,-57.2554];
addKnot spl crv #bezier #curve [22.5917,-37.6313,-13.6633] [13.1029,-40.9434,60.2389] [32.0804,-34.3193,-87.5654];
spl.steps = 32;
updateShape spl;
spl;
)
--****************************************************
fn build_plane =
(
p = Plane length:100 width:100 pos:[-8.80521,-37.87,2.34646] dir:[0.306637,-0.0996364,0.946597] isSelected:off lengthsegs:1 widthsegs:1
)
--****************************************************
fn cuberoot x =
(
y = pow (abs x) (1.0/3.0);
if x < 0 then -y else y;
)
--****************************************************
fn solve_linear a b =
(
roots = #();
if abs (a as float) > 0.000001 then -- non Degenerate case
roots = #(-1.0 * b/a);
roots;
)
--****************************************************
fn solve_quad a b c =
(
roots = #();
if abs (a as float) < 0.000001 then -- Linear case, ax+b=0
roots = solve_linear b c;
else
(
v = 1.0 * b * b - 4.0 * a * c;
if abs v < 0.0000001 then
roots = #(-b/(2*a));
else if v > 0 then
(
temp = sqrt v;
roots = #((-b + temp)/(2 * a), (-b - temp)/(2 * a));
)
)
roots;
)
--****************************************************
fn solve_cubic a b c d =
(
roots = #();
if abs (a as float) < 0.000001 then -- quadratic case
(
roots = solve_quad b c d;
)
else -- Convert to depressed cubic t^3+pt+q = 0 (subst x = t - b/3a)
(
p = (3.0 * a * c - 1.0 * b * b)/(3.0 * a * a);
q = (2.0 * b * b * b - 9.0 * a * b * c + 27.0 * a * a * d)/(27.0 * a * a * a);
if abs p < 0.0000001 then -- p = 0 -> t^3 = -q -> t = -q^1/3
roots = #(cuberoot -q);
else if abs q < 0.0000001 then -- q = 0 -> t^3 + pt = 0 -> t(t^2+p)=0
roots = if p < 0 then #(sqrt -p, -sqrt -p) else #();
else
(
v = q*q/4 + p*p*p/27;
if abs v < 0.0000001 then -- val = 0 -> two roots
roots = #(-1.5*q/p, 3*q/p);
else if v > 0 then -- Only one real root
(
u = cuberoot (-q/2.0 - sqrt v);
roots = #(u - p/(3*u));
)
else -- val < 0, three roots, but needs to use complex numbers/trigonometric solution
(
u = 2.0 * sqrt (-p/3.0);
t = degToRad ((acos (3.0 * q/p/u))/3.0); -- val < 0 implies p < 0 and acos argument in [-1..1]
k = 2.0 * PI / 3.0;
roots = #(u * cos (radToDeg t), u * cos (radToDeg(t-k)), u * cos (radToDeg (t-2*k)));
)
)
for i = 1 to roots.count do roots[i] -= b/(3*a); -- Convert back from depressed cubic
)
roots;
)
--****************************************************
fn comp_plane_bez_seg_intersection pln spl crv seg =
(
-- get plane info
n = pln.dir
q = pln.pos
-- extract bezier info (no range checking)
p0 = getknotpoint spl crv seg;
p1 = getOutVec spl crv seg;
p2 = getinVec spl crv (seg + 1);
p3 = getknotpoint spl crv (seg + 1);
-- create the polynomial coefficients where possible intersections occur n.b(t) = n.q
a = dot n (3 * p1 - 3 * p2 + p3 - p0);
b = dot n (3 * p0 - 6 * p1 + 3 * p2);
c = dot n (3 * p1 - 3 * p0);
d = dot n (p0 - q);
-- solve for t
roots = solve_cubic a b c d;
for i in roots where i >= 0 and i <= 1 do point pos:(pathInterp spl crv i)
)
--****************************************************
delete objects
comp_plane_bez_seg_intersection (build_plane()) (build_spline()) 1 1



though trying to expand (1-x)^3 after 30 algebra free years was quite comical, thank god for the internet 
