开发者

difference between parametric and algebraic equation of sphere intersection with line

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开发者_Python百科  

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

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)

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

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

which is the same equation for k that 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.


So your question basically 'what is the best way to solve the sphere ray intersection'. I think you already are using the best way from a coding point of view i.e. solving the quadratic equation (this is exactly what I do in my ray-tracing project pvtrace. There are few reason I think this is the best approach:

  1. It's analytical i.e. no numerical iteration required!
  2. To work out if the sphere is hit you don't have to call any math.h function i.e. you can calculate the discriminant for intersection (as you pointed out) just using b^2 - 4.a.c >= 0.
  3. If you need to go one step further and calculate the intersection point then you simply make one sqrt() function call. I anticipate this will be faster then multiple calls to trig. functions coupled with a Newton-Raphson, plus it will be much less code (always a good thing!).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜