5

I'm writing a tool in Java which does a little drawing on a Graphics object.

But, I'm stuck on a problem that I don't quite know how to solve. Hoping someone can help.

How can I determine if a point x,y on a Graphics object touches a line that extends from, for example, 200,200 to 392,144.

Sounds simple, but I'm stumped...help!

rog8gm
  • 169
  • 2
  • 2
  • 10
  • possible duplicate of [How can I tell if a point belongs to a certain line?](http://stackoverflow.com/questions/907390/how-can-i-tell-if-a-point-belongs-to-a-certain-line) – zaf Jun 08 '11 at 10:05

7 Answers7

7

That has little to do with the Graphics object, actually. It's just some simple math.

Your example line has the formula

        

with t in [0, 1]. So to find out whether the point is on the line, just solve the linear equation system

        

If t is the same for both equations in the system and between 0 and 1, you have a solution. So you need to calculate:

        

Unless my math fails me; it's been a while.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Joey
  • 344,408
  • 85
  • 689
  • 683
  • Small addition: In your case, (x, y) is given and you can calculate `t` for both subformulas (`x = 200 + t_x * 192`; `y = 200 + t_y * -56`). If `t_x` and `t_y` are the same value and this value is between 0 and 1, (x, y) is on the line. – schnaader Jun 08 '11 at 09:07
  • Hm, given that one usually deals with on-screen pixels, maybe the math approach is only half the answer. – Joey Jun 08 '11 at 09:28
  • Isn't it `t_x = (x - 200) / 192` and `t_y = (y - 200) / -56`? – Yew Long Jun 08 '11 at 09:49
  • Yew: Gah, thanks. That's what I get for doing math in the morning. Fixed. – Joey Jun 08 '11 at 10:07
  • user: Note that if you need some tolerance for points on or off the line segment, then perhaps [Ben's answer](http://stackoverflow.com/questions/6276361/java-is-a-line-stretching-from-a-b-to-c-d-touching-pointx-y/6277078#6277078) works better. – Joey Jun 08 '11 at 10:58
2

You can work out the equation of the line connecting the two points.

Equation of line: y = mx+c

m is the gradient: m = (y2-y1)/(x2-x1);

c is the y-intercept: c = y1 - m * x1;

Once you have your equation, you can test whether any point lies on the line by plugging in its x-coordinate and checking if the y-coordinate coming out from the equation matches.

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • Note that you need to check whether the point is beyond the start or end of the line segment as well. Probably not for the `Line2D` variant, but for the math one. – Joey Jun 08 '11 at 09:16
  • 2
    Line2D contains() always return false – proactif Jun 08 '11 at 09:21
2

There are correct answers already, but I think it might be more generally useful to have a formula that gives the distance of any point from a specified line. Then you can just check if this is zero, or within whatever tolerance you choose. The following should work regardless of special cases like vertical lines (infinite gradient).

The distance of point X from the line AB is

    

where A, B and X are the 3D position vectors of the three points (just set z = 0 if you are only working in 2D) and x is the vector product. That comes to

    

where A = (a,b), B = (c,d) and X = (x,y). Then to check that the point is actually within the line segment and not elsewhere on the infinite line, you can use the scalar products: the distance of X along the line from A to B is

    

i.e.

    

and this should lie between 0 and

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ben
  • 2,348
  • 2
  • 19
  • 28
  • Oh, right. That one I totally forgot. And with a little leeway it should work acceptably for pixels on screen as well (where the points won't be *exactly* on the line, I guess). – Joey Jun 08 '11 at 10:08
  • Thanks Joey for the formula images. – Ben Jun 08 '11 at 10:39
  • Hi @Ben (or @Joey), can you confirm that the above equation is based on...Point X is (x, y) and Line AB is a line from (a,b) to (c,d)? Thanks – rog8gm Jun 08 '11 at 22:16
  • Hi @rog8gm. Yes, that is right. I've clarified this in the text now. – Ben Jun 09 '11 at 08:05
1

This has been already answered here: How can I tell if a point belongs to a certain line?.

Community
  • 1
  • 1
Ryan Fernandes
  • 8,238
  • 7
  • 36
  • 53
0

Sounds like you need find the equation of the line between two point. From there you can use that equation to prove that if your point touch the line. The equation of a line is typically written as y=mx+b where m is the slope and b is the y-intercept.

shh
  • 682
  • 1
  • 5
  • 20
0

Line2D API will help you. See the ptLineDist(double PX,double PY)method. It returns 0.0 if point lies on the line.

Kamahire
  • 2,149
  • 3
  • 21
  • 50
-1

Mathematically, you could find slope of the two points and compare with the slope of new point to each of the old points.

In Java, you could use Line2D.contains(double x, double y);

Edit: Folks at stackoverflow are so fast. :-)

Manimaran Selvan
  • 2,166
  • 1
  • 14
  • 14
  • Line2D contains() always return false. – Joey Oct 07 '11 at 14:04
  • You are correct. Should have read the javadoc before answering in the forum!! According to Javadoc - "Tests if a specified coordinate is inside the boundary of this Line2D. This method is required to implement the Shape interface, but in the case of Line2D objects it always returns false since a line contains no area." – Manimaran Selvan Oct 08 '11 at 18:26