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?

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top