开发者

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?

I'm writing a tool in Java w开发者_Go百科hich 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!


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

Your example line has the formula

        

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?

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

        

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?

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:

        

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?

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


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

    

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?

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

    

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?

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

    

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?

i.e.

    

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?

and this should lie between 0 and

Java - Is a line stretching from (a,b) to (c,d) touching Point(x,y)?


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.


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


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.


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


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. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜