Re: Point between 2 points
- From: alexandre_paterson@xxxxxxxx
- Date: 18 Apr 2006 21:12:42 -0700
Alex Hunsley wrote:
alexandre_paterson@xxxxxxxx wrote:
Genevieve wrote:
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).
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
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).
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
.
- Follow-Ups:
- Re: Point between 2 points
- From: Oliver Wong
- Re: Point between 2 points
- From: alexandre_paterson
- Re: Point between 2 points
- References:
- Point between 2 points
- From: Genevieve
- Re: Point between 2 points
- From: alexandre_paterson
- Re: Point between 2 points
- From: Alex Hunsley
- Point between 2 points
- Prev by Date: Re: C++ programmer stumbles over pass by value. Need advice.
- Next by Date: Re: Point between 2 points
- Previous by thread: Re: Point between 2 points
- Next by thread: Re: Point between 2 points
- Index(es):
Relevant Pages
|
Loading