I'm writing a Raytracer in C, and to draw a sphere I'm using the Cartesian equation:
x^2 + y^2 + z^2 = R^2.
I have my eye position (x_eye, y_eye, z_eye) and my eye vector (Vx, Vy, Vz). The parametric equation of my line is:
x = x_eye + k * Vx
y = y_eye + k * Vy
z = z_eye + k * Vz
I put the parametric equation of my line in the Cartesian equation of the sphere in order to solve it
(x_eye + k * Vx)^2 + (y_eye + k * Vy)^2 + (z_eye + k * Vz)^2 = R^2
(Vx^2 + Vy^2 + Vz^2) * k^2 + 2 * (x_eye*Vx + y_eye*Vy + z_eye*Vz) * k + (x_eye^2 + y_eye^2 + z_eye^2 - R^2) = 0
I got now an equation like ax^2 + bx + c = 0 and define a, b, c with:
a = (Vx^2 + Vy^2 + Vz^2) * k^2
b = 2 * (x_eye * Vx + y_eye * Vy + z_eye * Vz) * k
c = (x_eye^2 + y_eye^2 + z_eye^2 - R^2)
then i can find k for each pixel if there is intersection (b^2 - 4.a.c >= 0).
But is there any other way to find k using these parametric equation of line and sphere
line :
x = x_eye + k * Vx
y = y_eye + k * Vy
z = z_eye + k * Vz
and for sphere:
x = R.cos(u).cos(v)
y = R.sin(u).cos(v)
z = R.sin(v)
how could i find k with these two parametric equation?
should I do
x_eye + k * Vx = R.cos(u).cos(v)
y_eye + k * Vy = R.sin(u).cos(v)
z_eye + k * Vz = R.sin(v)