Line sphere intersect


#1

I write a test program for the function ON_Intersect( const ON_Line&, const ON_Sphere&, ON_3dPoint&, ON_3dPoint& ) using RhinoSDK, I find that when the Line do not intersect with the
sphere, there are also intersections. Can you give me some advices to solve the problem?

The program is as flows.

ON_3dPoint O(0,0,0);
const double d = 10;
ON_Sphere m_sphere(O, d);
double x=0;double y=20;double z1 = -20; double z2= 20;
ON_3dPoint from(x, y,z1), to(x,y,z2);
const ON_Line ray(from, to);
ON_3dPoint A,B;
int num = ON_Intersect(ray,m_sphere,A,B);
ON_NurbsSurface nSF;
m_sphere.GetNurbForm(nSF);
 
context.m_doc.AddPointObject(A);
context.m_doc.AddPointObject(B);
context.m_doc.AddCurveObject(ray);
context.m_doc.AddSurfaceObject(nSF);
context.m_doc.Redraw();

Thank you


(Dale Lear) #2

The way
int ON_Interesect(
const ON_Line&,
const ON_Sphere&,
ON_3dPoint&,
ON_3dPoint& )

works is described in the header file comments for the function’s declaration.

// returns 0 = no intersections,
// 1 = one intersection, 
// 2 = 2 intersections
// If 0 is returned, first point is the point 
// on line closest to sphere and 2nd point is the point
// on the sphere closest to the line.
// If 1 is returned, first point is obtained by evaluating
// the line and the second point is obtained by evaluating
// the sphere.

In the code you posted, you need to look at the value of “num” to determine what the output values mean.

When num=0, the line and sphere do not intersect. The returned “A” = the point on the line that is closest to the sphere and “B” = the point on the sphere that is closest to the line.

If this does not help, please add Rhino .3dm file to your reply using the button on the top toolbar that looks like a cloud with an up arrow in it. The button tool tip will say “image”, but you can use it to attach any type of file.


#3

Thank you, I can get the intersections correctly now