Jump to content

  • Log In with Google      Sign In   
  • Create Account

Check a Point on a Line


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
7 replies to this topic

#1   Members   

122
Like
Likes
Like

Posted 27 August 2001 - 01:52 AM

How do I check if (x,y) is on the line (x1,y1)- (x2,y2)?

#2   Members   

116
Like
Likes
Like

Posted 27 August 2001 - 02:02 AM

well, you could get the y-intercept 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.

#3   Members   

122
Like
Likes
Like

Posted 27 August 2001 - 02:16 AM

Make the following check:

  
x1 - x x2 - x
------ == ------
y1 - y y2 - y


Of course, it may not be exactly accurate, but then you can check it with a tolerance value.

~ Dragonus

#4   Members   

122
Like
Likes
Like

Posted 28 August 2001 - 09:42 PM

thanks, the algorithm seems to work
but when i implement it in my game, it doesn''t work... probably wrong with implemention. i have to check it out and try to figure out what i did wrong...

#5   Members   

122
Like
Likes
Like

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 (degrees-minutes-seconds) 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:

  
if(fabs(v - (int)v) > .9999999)
{
v = (int)v;
if(v >= 0) v++;
else v--;
}
else if(fabs(v - (int)v) < 0.0000001)
v = (int)v;


~ Dragonus


#6   Moderators   

1160
Like
Likes
Like

Posted 29 August 2001 - 06:25 AM

The equation for a line whose segment is (x1,y1)-(x2,y2) has the following equation:

(y-y1)=(y2-y1)/(x2-x1)*(x-x1)

which can be rearranged to:

(x2-x1)*(y-y1)-(y2-y1)*(x-x1)=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((x2-x1)*(y-y1)-(y2-y1)*(x-x1))<0.001)
{
//point is on the line, or close enough
}




#7   Moderators   

1160
Like
Likes
Like

Posted 29 August 2001 - 08:01 AM

Here''s a sample program demonstrating point on a line.

#8   Members   

122
Like
Likes
Like

Posted 29 August 2001 - 09:36 AM

thankyou very much... i got it to work..
thanks to both of you





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.