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

## Recommended Posts

fguihen    100
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 on other sites
mike25025    494
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 on other sites
fguihen    100
sorry, but reading through that, i dont know what its doing. whats fabs? im sure theres an easier method than this. thanks though

##### Share on other sites
Eelco    301
you have asked the same question about three times now, and got the answer handed to you even more often.

im giving up.

##### Share on other sites
SuperNerd    100
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
}