1

First of all: I know I can calc the distance from a point to a line to check if the point was on the line. This is what I do for detecting clicks (with an offset) on a line.

But before that, I want to apply a general check around the diagonal line. The line itself with Start and End point defines a rectangular area:

Pstart(sx, sy), Pend(ex, ey).

I can use boundary check to determine if the Point(px, py) was inside that rectangle:

sx <= px && ex >= px && sy <= px && ey >= py

But this only applies if the line goes from top left to bottom right. If it goes a different direction I have to modify the algorithm. How could I use my formula above regardless of the line direction?

How can I get the formula to respect the direction accordingly?

Jarrod
  • 9,349
  • 5
  • 58
  • 73
membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

3

Just compare for Math.min(sx, ex) <= px <= Math.max(sx, ex) and likewise for the y dimension.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

Line2D.ptSegDist(x1, y1, x2, y2, xP, yP) returns 0.0 iff the point (xP, yP) is on the line segment from (x1, y1) to (x2, y2). Line2D.ptLineDist does the same thing for the infinite line.

Parth Soni
  • 11,158
  • 4
  • 32
  • 54
  • That's what I do for deeper analysis, but I want to include a precheck to not having to run the ptSegDist algorithm against all my lines, but only the lines that lie inside the defined rectangle of the line start and end points. – membersound Mar 25 '13 at 14:57
  • @membersound: i think [this](http://stackoverflow.com/questions/2752725/finding-whether-a-point-lies-inside-a-rectangle-or-not) may help you for that :) – Parth Soni Mar 25 '13 at 15:05