Archived

This topic is now archived and is closed to further replies.

Intersection between a line and a plane..

This topic is 6360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If anybody can help me with this I would greatly appreciate it.. I need to find the intersection between a line and and a plane for polygon splitting purposes..the way I do it now is: *Find the distance from the first point to the plane.. *Find the distance from the second point to to the plane.. *Get a percentage from the 2 distances.. *Add the vector of the line multiplied by this percentage to the first point. At right angles this works beautifully, but the closer the line gets to being parallel with the plane, floating point imprecision renders the distances(which I use for my percentage) more and more inaccurate..in most cases it''s not too obvious, but I know there must be a better way..does anybody know the definitive FDA approved formula for finding the intersection between a line and a plane? "Like all good things, it starts with a monkey.."

Share this post


Link to post
Share on other sites
Here''s the data that we need to work with:

float lp1x //starting point of the line segment
float lp1y //
float lp1z //

float lp2x //end point of the line segment
float lp2y //
float lp2z //

float spx //a point known to be on the plane
float spy //any point of the polygon will do
float spz //

float nx //normal vector of the plane
float ny //
float nz //

//here''s my old way of doing things:
//warning:I modified alot of this to make it easier
//to understand so forgive me for any typos..this should
//still work, though, I didn''t hack it too much

float dist1 = ((lp1x-spx)*nx+(lp1y-spy)*ny+(lp1z-spz)*nz);
float dist2 = ((lp2x-spx)*nx+(lp2y-spy)*ny+(lp2z-spz)*nz);
float percent = (((dist1+dist2)*(dist1/(dist1+dist2)))/sqrt(
((lp2x-lp1x)*(lp2x-lp1x))+
((lp2y-lp1y)*(lp2y-lp1y))+
((lp2z-lp1z)*(lp2z-lp1z))
));
float intx = lp2x-((lp2x-lp1x)*percent);//here''s
float inty = lp2y-((lp2y-lp1y)*percent);//our
float intz = lp2z-((lp2z-lp1z)*percent);//intersection

//now that I have the official formula, it would look
//like this..haven''t tested it yet
float percent = -(((lp1x*nx)+(lp1y*ny)+(lp1z*nz))-(spx*nx+spy*ny+spz*nz))/(((lp2x-lp1x)*nx)+((lp2y-lp1y)*ny)+((lp2z-lp1z)*nz));
float intx = lp2x-((lp2x-lp1x)*percent);//here''s
float inty = lp2y-((lp2y-lp1y)*percent);//our
float intz = lp2z-((lp2z-lp1z)*percent);//intersection

as you can see the second is much better, get to avoid the
dreaded sqrt..as I said, I haven''t officially tested the
new way and I really chopped up the original so feel free
to slam me if either of these don''t work..I can guarantee
that the first one works, if anyone wants the original code
email me..the reason I changed alot of it was because I was
pulling the data out of surface and vector structs and I wanted
to demonstrate with discrete variables, so yes, I know there''s
alot of duplicate math going on with the line vector..


"Like all good things, it starts with a monkey.."

Share this post


Link to post
Share on other sites