I am tracing a ray in Matlab and I have an expression for a "line- to sphere intersection" that works:
a=1+Ax2+Ay2
b=2⋅(−zs+Ax⋅(Bx−xs)+Ay⋅(By−ys))
c=z2s+(Bx−xs)2+(By−ys)2−R2
This is part of a code in Matlab, and works fine. It is derived from substituting
(x=Ax⋅z+Bx,y=Ay⋅z+By) (This is line in parametric, I believe.)
into:
((x−xs)2+(y−ys)2+(z−zs)2=R2) (This is the equation for a sphere, modified to find distance)
I am now trying to do the same for a cylinder, by substituting
(x=Ax⋅z+Bx,y=Ay⋅z+By) (line) into: ((x−xs)2+(y−ys)2=R2) (cylinder)
Putting into quadratic formula and ending up with:
a=A2x+A2y;
b=2⋅Ax⋅Bx−2⋅Ax⋅xs+2⋅Ay⋅By−2⋅Ay⋅ys;
c=−2⋅Bx⋅xs−2⋅By⋅ys+B2x+B2y+x2s+y2s−R2;
This doesn't seem to work. Can anyone tell me if something seems off with the math or logic?
Any comments at all would be greatly appreciated.
What I'm asking is this:
Is there a way to describe a line- cylinder intersection, using a,b,c, like I have for a line- sphere intersection(below)?
a=1+Ax2+Ay2
b=2⋅(−zs+Ax⋅(Bx−xs)+Ay⋅(By−ys))
c=z2s+(Bx−xs)2+(By−ys)2−R2