• Advertisement
Sign in to follow this  

Point in Line Segment, 2D how to?

This topic is 4620 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

I have the following Equation for finding the intersecting point between 2 rays:
    bool LineIntersect(CVector2& point1A,
		       CVector2& point1B,
		       CVector2& point2A,
		       CVector2& point2B,
		       CVector2& out)
    {
      CVector2 dir1 = point1B - point1A;
      CVector2 dir2 = point2B - point2A;
      float a1 = dir1.y;
      float b1 = -dir1.x;
      float d1 = (point1A.x*dir1.y)-(point1A.y*dir1.x);

      float a2 = dir2.y;
      float b2 = -dir2.x;
      float d2 = (point2A.x*dir2.y)-(point2A.y*dir2.x);

      float denominator = (a1*b2)-(a2*b1);
      if(denominator==0) return false;
      out.x = ((b2*d1)-(b1*d2))/denominator;
      out.y = ((a1*d2)-(a2*d1))/denominator;
      /*
	P(t) = P0 + t(P1-P0)
      */
      return true;
    }

it works, but it does not restrain itself to the segments defined by point1A,point1B and point1A,point1B, as you can see by the comment, I would like to find out the "t" value for the intersection point so I can just check whether this value is between 0 and 1 to return whether or not the segments intersect. But how do I find the value of t for each segment?, as a reminder t=0 at Point1A and/or at Point2A and t=1 at Point1B and/or at Point2B. Thanks for your help [smile]

Share this post


Link to post
Share on other sites
Advertisement
See here, if I am understanding you correctly you want to find what the site is calling ua and ub. If not I don't know what you are looking for.

Share this post


Link to post
Share on other sites
Yup, works, here is the new function for posterity:


bool LinesIntersect(const CVector2& p1,
const CVector2& p2,
const CVector2& p3,
const CVector2& p4,
CVector2& out)
{
// from http://astronomy.swin.edu.au/~pbourke/geometry/lineline2d/
float denominator = ((p4.y-p3.y)*(p2.x-p1.x)) -
((p4.x-p3.x)*(p2.y-p1.y));
if(denominator==0.0f) return false;
float t1 = (((p4.x - p3.x)*(p1.y-p3.y))-
((p4.y-p3.y)*(p1.x-p3.x)))/denominator;
float t2 = (((p2.x - p1.x)*(p1.y-p3.y))-
((p2.y-p1.y)*(p1.x-p3.x)))/denominator;
out.x = p1.x + t1*(p2.x - p1.x);
out.y = p1.y + t1*(p2.y - p1.y);
if((t1<0.0f)||(t1>1.0f)||(t2<0.0f)||(t2>1.0f))
return false;
return true;
}




[Edited by - Kwizatz on May 30, 2005 11:01:44 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement