Sign in to follow this  

Point in Line Segment, 2D how to?

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
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
float denominator = ((p4.y-p3.y)*(p2.x-p1.x)) -
if(denominator==0.0f) return false;
float t1 = (((p4.x - p3.x)*(p1.y-p3.y))-
float t2 = (((p2.x - p1.x)*(p1.y-p3.y))-
out.x = p1.x + t1*(p2.x - p1.x);
out.y = p1.y + t1*(p2.y - p1.y);
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this