The difference between the parametric and algebraic equations of intersection of a sphere with a line

I write Raytracer in C, and to draw the sphere I use 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). My line parametric equation:

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 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  

Now I have an equation of type 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 an intersection (b ^ 2 - 4.ac> = 0).

But is there any other way to find k using these parametric equations of the line and sphere
line:

x = x_eye + k * Vx  
y = y_eye + k * Vy  
z = z_eye + k * Vz  

and for the scope:

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 equations?
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)  
+3
2

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)

, . , ,

(x_eye + k * Vx)^2 + (y_eye + k * Vy)^2 + (z_eye + k * Vz)^2 = R2  

k, .

, , . raytracer, . . . , .

+2

, " ". , , .. ( , pvtrace. , , :

  • , .. !
  • , math.h, ( ), b ^ 2 - 4.ac >= 0.
  • , sqrt(). , , . Newton-Raphson, ( !).
+1

All Articles