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)
To solve the system
start by squaring both sides of each equation, and then add all three equations together. Then simplify the right hand side using trigonometric identities until you get
which is the same equation for
kthat you had before.In general, though, this may not be practical approach. Since you are trying to write a raytracer, you don’t want to solve every equation by hand. Instead, use some system solving algorithms. A good starting point may be looking up some information on Newton’s method and secant method for several variables. Any introductory textbook on numerical analysis should contain plenty of information that will get you started.