Author Topic: Finding out which side of a line a point is on  (Read 4249 times)

0 Members and 1 Guest are viewing this topic.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Finding out which side of a line a point is on
« on: June 21, 2011, 12:41:46 am »
Here's a question that'll help me tremendously in my next project (hopefully contest entry):

Given a line , what's the simplest (fastest, least intensive) way to tell if a point is "above" or "below" it?

Thanks in advance for any help.

EDIT: What I actually want to do is test if a point starts above a line and goes below, in case anyone has a simpler way to test that. Any solution has to be specific in that the point must start on one side and end in the other, not the other way around.
« Last Edit: June 21, 2011, 12:45:24 am by Deep Thought »




Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Finding out which side of a line a point is on
« Reply #1 on: June 21, 2011, 12:48:06 am »
Its very simple.  If you have a point R with vector RS, you can determine if point P is above or below by taking the dot product of the vectors RS and RP.  If its positive, its above, negative, its below, and zero its on the line.  It requires just 2 multiplications and an addition.

EDIT: Actually wait, that doesn't work.  Let me revise that.

EDIT2: The correct formula was this:
(X2 - X1)*(Py - Y1) - (Y2 - Y1)*(Px - X1)

You can improve it slightly to this:
(X2 - X1)*(Py - Y1) + (Y1 - Y2)*(Px - X1)

That replaces a subtraction with an addition to speed it up.
« Last Edit: June 21, 2011, 01:03:02 am by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Finding out which side of a line a point is on
« Reply #2 on: June 21, 2011, 12:49:13 am »
Its very simple.  If you have a point R with vector RS, you can determine if point P is above or below by taking the dot product of RS with RP.  If its positive, its above, negative, its below, and zero its on the line.  It requires just 2 multiplications and an addition.

I think I learned that stuff in math class this year... Never have I wished more that I had paid attention x.x

Thanks Quigibo.

EDIT: Wait, how does that work? The dot product's sign is determined by a cosine, which should be positive on both ends, right?

I'm probably just missing the point entirely.

EDIT2: Oh. What's that called?
« Last Edit: June 21, 2011, 04:59:47 pm by Deep Thought »




Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Finding out which side of a line a point is on
« Reply #3 on: June 21, 2011, 12:51:08 am »
Alternatively, if you have a line of equation A*x+B*y=C, and a point (x0,y0), you can compare A*x0+B*y0 to C. If it's greater, it's on one side, if it's less than, it's on the other side, and if it's equal, it's on the line.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Finding out which side of a line a point is on
« Reply #4 on: June 21, 2011, 12:52:29 am »
Alternatively, if you have a line of equation A*x+B*y=C, and a point (x0,y0), you can compare A*x0+B*y0 to C. If it's greater, it's on one side, if it's less than, it's on the other side, and if it's equal, it's on the line.

And that makes sense now, thanks :)




Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Finding out which side of a line a point is on
« Reply #5 on: June 21, 2011, 12:58:31 am »
Also, if you ever want the exact distance from a point to a line, you'd do abs(A*x0+B*y0-C)/sqrt(A^2+B^2). This works especially well if sqrt(A^2+B^2) is a known constant (typically 1, but when working with integers something like 256 might be better)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman