Sign in to follow this  
fguihen

check if a point is on a line ( range checking)

Recommended Posts

hi. i can check if two lines intersect, and find the intersection point, but the problem is that my method checks along infinite lines, and not line segments, so i have to do a check to make sure returned point is on both lines tested. currently i am doing a big nested if statement
if (xIntersect >= minX.Xval && xIntersect >= minX.Yval)
				{
					if (xIntersect <= maxX.Xval && xIntersect <= maxX.Yval)
					{
						if (yIntersect >= minY.Xval && yIntersect >= minY.Yval)
						{
							if (yIntersect <= maxY.Xval && yIntersect <= maxY.Yval)
							{
								//point is on two line segments
								magnitude  = magnitude - 2;
								angle = angle + 1;
								if (magnitude < 0)
								{
									magnitude = 0;
								}
							}
						}
					}
				}
				else
				{
					magnitude++;
					if(magnitude > 4)
					{
						magnitude = 4;
					}
				}
			}

this is not working properly, such as if the line is sloping where point one is greater than point 2 . i got max and min x and y of each line and tried it like that, but its still not working properly. can anyone give me an elligant, simple solution to this?

Share this post


Link to post
Share on other sites
bool pointOnLine(float px,float py,float lx,float ly,float lx2,float ly2)
{
if(fabs(lx2-lx)<=0.0001)
{
if(fabs(ly-py)>0.0001)return false;
if(fabs(ly2-ly)<=0.0001)return false;
float d = (ly-py)/(ly2-ly);
if((d<0)||(d>1))return false;
return true;
}
if(fabs(ly2-ly)<=0.0001)
{
if(fabs(lx-px)>0.0001)return false;
float d = (lx-px)/(lx2-lx);
if((d<0)||(d>1))return false;
return true;
}
float d = (lx-px)/(lx2-lx);
float d2 = (ly-py)/(ly2-ly);
if((d<0)||(d>1))return false;
if((d2<0)||(d2>1))return false;
return (fabs(d-d2)>=0.0001);
}

i didnt check my math so this might not work

Share this post


Link to post
Share on other sites
If the x of the point is between the x of the two endpoints of the line and the y of the point is between the y of the two endpoints of the line and you know that it is on the infinite line then it means that is is on the point is on the line. You could use do that this way.

if ((line.x1 - point.x) / (line.x1 - line.x2) <= 1 &&
(line.y1 - point.y) / (line.y1 - line.y2) <= 1 &&
(line.x1 - point.x) / (line.x1 - line.x2) >= 0 &&
(line.y1 - point.y) / (line.y1 - line.y2) >= 0)
{
// point on line
}
else
{
// point not on line
}

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