Java Forum / General / April 2006
Point between 2 points
Genevieve - 18 Apr 2006 19:43 GMT Hi,
How would I find if a point(x, y) is between 2 points? It could depend of x or y like point(50,100) is between (25,100) and (150,100) AND point(50,100) is between (50,25) and (50,150).
Thanks... Gen
Patricia Shanahan - 18 Apr 2006 19:47 GMT > Hi, > [quoted text clipped - 6 lines] > Thanks... > Gen How about diagonals? Do you consider (50,50) to be between (0,0) and (100,100)?
Patricia
Genevieve - 18 Apr 2006 19:49 GMT Hi!
Yes it would be...
> > Hi, > > [quoted text clipped - 11 lines] > > Patricia Vova Reznik - 18 Apr 2006 20:08 GMT > Hi, > [quoted text clipped - 6 lines] > Thanks... > Gen Point located between 2 points when distance between line (between points) and testing point = 0. Read: Line2D public double ptLineDist(double PX, double PY)
alexandre_paterson@yahoo.fr - 18 Apr 2006 20:58 GMT > > Hi, > > [quoted text clipped - 12 lines] > Line2D > public double ptLineDist(double PX, double PY) no... that's wrong.
That method, as it names implies, calculates the distance between a line and a point.
I think the correct method is :
public double ptSegDist(double x1, double y1, double x2, double y2, double px, double py)
wich can be used (amongst other) to determine if a point is located between two points.
James McGill - 18 Apr 2006 21:01 GMT > That method, as it names implies, calculates the distance between a > line and a point. Which, if Zero, implies that the line contains the point, correct?
alexandre_paterson@yahoo.fr - 18 Apr 2006 22:08 GMT > > That method, as it names implies, calculates the distance between a > > line and a point. > > Which, if Zero, implies that the line contains the point, correct? I stand corrected by both you and Oliver.
:) alexandre_paterson@yahoo.fr - 19 Apr 2006 06:49 GMT > > That method, as it names implies, calculates the distance between a > > line and a point. > > Which, if Zero, implies that the line contains the point, correct? Correct... But this is *not* what the OP asked.
The OP asked how to check if a point was between a segment.
So I was right. ptLineDist is *not* what the OP needs.
System.out.println(Line2D.ptLineDist(1, 1, 2, 2, 3, 3); System.out.println(Line2D.ptSegDist(1, 1, 2, 2, 3, 3));
outputs:
0 1.4142
So I was right after all and didn't stand corrected. The method, as it names implies, calculates the distance between a point and a line, this is *not* what the OP asked.
Oliver Wong - 18 Apr 2006 21:16 GMT >> > Hi, >> > [quoted text clipped - 25 lines] > wich can be used (amongst other) to determine if a point is located > between two points. I think the method you cited (ptSegdist), and the method Vova did (ptLineDist) do the exact same thing, except ptLineDist compares against "this" line, while your method compares against the provided line (specified via [x1,y1]-[x2,y2]).
- Oliver
alexandre_paterson@yahoo.fr - 19 Apr 2006 06:54 GMT > >> > Hi, > >> > [quoted text clipped - 32 lines] > > - Oliver Re Oliver :)
Nope it's actually not how it works...
System.out.println(Line2D.ptLineDist(1, 1, 2, 2, 3, 3)); System.out.println(Line2D.ptSegDist(1, 1, 2, 2, 3, 3));
outputs:
0 1.4142
I basically got lost in this thread... I thought James was correcting me by asking a question... While he was really asking a question.
So afterall Vova was wrong and I didn't stand corrected by James for he was asking a question :)))
Oliver Wong - 19 Apr 2006 15:16 GMT >> >> > Hi, >> >> > [quoted text clipped - 45 lines] > 0 > 1.4142 You're probably right (though I didn't actually verify this on my own JVM). I was actually thinking something along the lines of:
Line2D myLine = new Line2D(1,1,2,2); System.out.println(myLine.ptLineDist(3,3));
but then I noticed Line2D cannot be instanciated, so obviously my interpretation was wrong. I was basing my guesses off of the javadocs, without actually writing code to test.
- Oliver
Vova Reznik - 19 Apr 2006 15:33 GMT >>> >> > Hi, >>> >> > [quoted text clipped - 58 lines] > > - Oliver You may instantiate Line2D subs (Float or Double).
Line2D hypotenuse = new Line2D.Double(25, 100, 150, 100); double cathetus = d.ptLineDist(50, 100);
or Pifagor Samosskii is still actual.
Don't see a problem to discuss so long.
Oliver Wong - 18 Apr 2006 20:18 GMT > Hi, > [quoted text clipped - 3 lines] > AND > point(50,100) is between (50,25) and (50,150). So basically, your two points define the two corners of a rectilinear rectangle, and you want to test if your third point is inside of that rectangle?
See http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Rectangle.html#contains(java.aw t.Point)
- Oliver
James McGill - 18 Apr 2006 20:56 GMT > How would I find if a point(x, y) is between 2 points? How to find if a point is located on a line segment defined by two points in 2-space? Or something else?
alexandre_paterson@yahoo.fr - 18 Apr 2006 21:02 GMT > Hi, > [quoted text clipped - 3 lines] > AND > point(50,100) is between (50,25) and (50,150). Hi,
point C is between the segment defined by points A and B if the distance from A to C plus the distance from B to C is equal to the distance from A to C.
This can be computed on a single (ugly) line or you can use Line2D's ptSegDist() method and check if you receive 0.0D as the result.
Alex
Alex Hunsley - 19 Apr 2006 00:26 GMT >> Hi, >> [quoted text clipped - 15 lines] > > Alex Checking for 0.0D (or 0.0F) isn't always a practical solution, due to floating point inaccuracies. Normally you should check that the result is within a certain error range of the desired result, i.e. get the distance and check that it is less than or equal to 0.000001 (for example).
alexandre_paterson@yahoo.fr - 19 Apr 2006 05:12 GMT > >> Hi, > >> [quoted text clipped - 20 lines] > is within a certain error range of the desired result, i.e. get the > distance and check that it is less than or equal to 0.000001 (for example). you mean using a small positive quantity... An... An epsilon! (lowercase epsilon).
:) I'm fully aware of that ;)
I even have the old habit of calling the double holding the epsilon EPS in my code. I guess I could use ' e ' if I wanted to be fancy nowadays :)
There still exists cases where inaccuracy exists as you point out, but the OP is unlikely to encounter it when working with what looks like integer screen coordinates. And using an epsilon only helps to define an "acceptable" error margin: for sufficiently large (and small) values, there will still be cases where the answer will be mathematically wrong.
Of course the inaccuracy problem in this trivial case can be completely solved by basic maths without involving floating-points arithmetics at all...
For example, given four points (enclosed within simple quotes to be sure to have the full number and to show that it's not a Java syntax ;) :
p0x='1' p0y='2'
p1x='89884656743115785407263711865852178399035283762922498299458738401578630390014269380294779316383439085770229476757191232117160663444732091384233773351768758493024955288275641038122745045194664472037934254227566971152291618451611474082904279666061674137398913102072361584369088590459649940625202013092062429184'
p1y='179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368'
p2x='22471164185778946351815927966463044599758820940730624574864684600394657597503567345073694829095859771442557369189297808029290165861183022846058443337942189623256238822068910259530686261298666118009483563556891742788072904612902868520726069916515418534349728275518090396092272147614912485156300503273015607296'
p2y='44942328371557892703631855932926089199517641881461249149729369200789315195007134690147389658191719542885114738378595616058580331722366045692116886675884379246512477644137820519061372522597332236018967127113783485576145809225805737041452139833030837068699456551036180792184544295229824970312601006546031214592'
p3x='22471164185778946351815927966463044599758820940730624574864684600394657597503567345073694829095859771442557369189297808029290165861183022846058443337942189623256238822068910259530686261298666118009483563556891742788072904612902868520726069916515418534349728275518090396092272147614912485156300503273015607294'
p3y='44942328371557892703631855932926089199517641881461249149729369200789315195007134690147389658191719542885114738378595616058580331722366045692116886675884379246512477644137820519061372522597332236018967127113783485576145809225805737041452139833030837068699456551036180792184544295229824970312601006546031214592'
I may find it perfectly acceptable to expect a mathematically correct answer given, by an algorithm, to these two questions (note that all these numbers are in the range supported by a double) :
a. is the point p2 inside the line segment defined by [p0,p1] ?
b. is the point p3 inside the line segment defined by [p0, p1] ?
and floating-point arithmetic, epsilon withstanding or not, may pose quite some problems.
(c.l.j.p. really lacked a post with a single line longer than 314 characters today ;)
While a solution (for example using Java) not involving floating-point numbers is trivial to write...
It all comes down to the requirements of the OP, wich weren't clearly formulated. Are we talking about integers? If so in what range? Are we talking about decimal numbers? If so where does these come from ? (ie accurately represented in a file, say as text, so that we can transfer them without losing any precision into, eg, BigDecimal? Or are they coming from some other part of the program, represented using FP numbers and hence probably already affected by precision errors)? Do they come from an Usenet post represented using some funky uncommon representation? etc.
:) See you soon in this group,
Alex
alexandre_paterson@yahoo.fr - 19 Apr 2006 05:17 GMT > I even have the old habit of calling the double holding the > epsilon EPS in my code. I guess I could use ' e ' if I wanted > to be fancy nowadays :) I'm really sad... That 'e' (really an ASCII 'e' here) was supposed to show as an epsilon (Unicode).
groups.google.com would show it to me as an epsilon before posting it (when previewing), but then changed it to an 'e' apparently :(
Oliver Wong - 19 Apr 2006 15:19 GMT >> >> Hi, >> >> [quoted text clipped - 39 lines] > large (and small) values, there will still be cases where the answer > will be mathematically wrong. If this is for graphics or games, what is usually happens is that we don't talk about lines at all, but rather "very thin" rectangles for act as lines. Or equivalently, we talk about lines with thickness. At that point, it becomes very precise what we meant when we say the point is "in" the line, and the appropriate epsilon value is more obvious.
- Oliver
Dimitri Maziuk - 19 Apr 2006 00:11 GMT Genevieve sez:
> Hi, > [quoted text clipped - 3 lines] > AND > point(50,100) is between (50,25) and (50,150). You would recall y = ax + b from your school math and realise that you know exes and whys for all three points.
Dima
 Signature Yes, Java is so bulletproofed that to a C programmer it feels like being in a straightjacket, but it's a really comfy and warm straightjacket, and the world would be a safer place if everyone was straightjacketed most of the time. -- Mark 'Kamikaze' Hughes
Chris Uppal - 19 Apr 2006 09:43 GMT > How would I find if a point(x, y) is between 2 points? Are you attempting to find whether a given point (say a mouse click) lies on /or near/ a line segment between two points ? If so then this thread may help:
http://groups.google.com/groups?as_umsgid=42188cc8$1$38039$bed64819@news.gradwell.net
If not then please ignore me ;-)
-- chris
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|