I am tracing a ray in Matlab and I have an expression for a "line- to sphere intersection" that works:

$a = 1 + Ax^2 + Ay^2$

$b = 2\cdot(-z_s + A_x\cdot(B_x-x_s) + A_y\cdot(B_y-y_s))$

$c = z_s^2 + (B_x-x_s)^2 + (B_y - y_s)^2 - R^2$

This is part of a code in Matlab, and works fine. It is derived from substituting

$(x=A_x\cdot z+B_x, y=A_y\cdot z+B_y)$ (This is line in parametric, I believe.)

into:

$((x-x_s)^2+(y-y_s)^2+(z-z_s)^2 = R^2)$ (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=A_x\cdot z+B_x, y=A_y\cdot z+B_y)$ (line) into: $((x-x_s)^2+(y-y_s)^2 = R^2)$ (cylinder)

Putting into quadratic formula and ending up with:

$a = A_x^2+A_y^2$;

$b = 2\cdot A_x\cdot B_x - 2\cdot A_x\cdot x_s + 2\cdot A_y\cdot B_y - 2\cdot A_y\cdot y_s;$

$c = -2\cdot B_x\cdot x_s - 2\cdot B_y\cdot y_s + B_x^2 + B_y^2 + x_s^2+y_s^2 - R^2;$

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 + Ax^2 + Ay^2$

$b = 2\cdot(-z_s + A_x\cdot(B_x-x_s) + A_y\cdot(B_y-y_s))$

$c = z_s^2 + (B_x-x_s)^2 + (B_y - y_s)^2 - R^2$