How do I check if (x,y) is on the line (x1,y1) (x2,y2)?
Check a Point on a Line
Started by delbogun, Aug 27 2001 01:52 AM
7 replies to this topic
Sponsor:
#2 Members  Reputation: 116
Posted 27 August 2001  02:02 AM
well, you could get the yintercept form of the line and just plug in the x and y values. If the two sides are equal then the point is on the line.
My Homepage
How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.
My Homepage
How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.
#5 Members  Reputation: 122
Posted 29 August 2001  02:52 AM
Are you accounting for roundoff error? The effects of it (as yucky as it really is) might be present. I know I got hit with that yesterday. I was converting DMS (degreesminutesseconds) into decimal degrees and then back again, and it just doesn''t understand that, when I convert 20 minutes into 1/3°, that it''s supposed to give me 20'' back. Instead I get 19.99999999999999974152 or something to that effect. Thus, my GUI kept displaying 19''60".
You''ll want to do something similar to this:
~ Dragonus
You''ll want to do something similar to this:

~ Dragonus
#6 Moderators  Reputation: 1152
Posted 29 August 2001  06:25 AM
The equation for a line whose segment is (x1,y1)(x2,y2) has the following equation:
(yy1)=(y2y1)/(x2x1)*(xx1)
which can be rearranged to:
(x2x1)*(yy1)(y2y1)*(xx1)=0
So, you can plug an arbitrary x,y coordinate into the above equation, and if you get a 0(or very close to zero), then the point is on the line.
Of course, that's the line, which is infinite, and extends beyond the segment.
The line segment is bounded by a box that can be described with (x1,y1) and (x2,y2). You can check if x,y is within that box and on the line by doing this:
if((x<=x2  x<=x1) && (x>=x1  x>=x2) && (y>=y1  y>=y2) && (y<=y1  y<=y2) && fabs((x2x1)*(yy1)(y2y1)*(xx1))<0.001)
{
//point is on the line, or close enough
}
(yy1)=(y2y1)/(x2x1)*(xx1)
which can be rearranged to:
(x2x1)*(yy1)(y2y1)*(xx1)=0
So, you can plug an arbitrary x,y coordinate into the above equation, and if you get a 0(or very close to zero), then the point is on the line.
Of course, that's the line, which is infinite, and extends beyond the segment.
The line segment is bounded by a box that can be described with (x1,y1) and (x2,y2). You can check if x,y is within that box and on the line by doing this:
if((x<=x2  x<=x1) && (x>=x1  x>=x2) && (y>=y1  y>=y2) && (y<=y1  y<=y2) && fabs((x2x1)*(yy1)(y2y1)*(xx1))<0.001)
{
//point is on the line, or close enough
}