Re: Point on Line Segment in 2D. Which code is faster ? Can you improveit ?
Skybuck Flying wrote:[color=blue]
> "Hans-Bernhard Broeker" <broeker@physik .rwth-aachen.de> wrote in message
> news:3p0cvdF86n t0U1@news.dfnci s.de...
>[color=green]
>>In comp.graphics.a lgorithms Skybuck Flying <nospam@hotmail .com> wrote:
>>
>>[Massive crossposting reduced to single group. Please, Skybuck, try
>>to follow netiquette a bit better in the future. If you feel a need
>>to X-post, at be a bit more selective to where --- this had *no*
>>business being posted to comp.arch or sci.electronics . And *always*
>>select a single group for the follow-ups.][/color]
>
> No, I like to have input from all those newsgroups since this is about
> optimizations and those newsgroup are related to software and hardware
> optimizations. C/Delphi/Asm for software/cpu optimizations and
> comp.arch/asm/eletronics about hardware optimizations and algorithms
> possibly about algorithm optimization. Those newsgroups are likely to
> contain people who know something about this.[/color]
I'm with Hans on this one. Skybuck, by your logic you should have
posted to ALL newsgroups, since there's probably someone in every
newsgroup who will be somehow "related to optimization". :-(
Comp.graphics.a lgorithms was the right place. Comp.lang.c is hard to
justify, since your question has nothing to do with C. But there's no
way to justify posting to sci.electronics .design, comp.arch, or
alt.comp.lang.b orland-delphi. Nor comp.lang.asm, unless you want a
response written in assembly language.
That said, other appropriate forums might have included comp.programmin g
or *maybe* comp.theory.
....[color=blue][color=green]
>>
>>You need to define the task a good deal more cleanly before an
>>implementatio n can reasonably be suggested. Missing information:
>>
>>1) What's a line segment, in this case? It may come as a surprise to
>>you, but ideas about the details of this actually differ. So: how do
>>you actually specify this line segment?[/color]
>
>
> A 2D line segment defined by (x1,y1) - (x2,y2)[/color]
Integer or floating point? In either case, how big are your points and
how fat is the line? Is it acceptable to return a false positive or
negative? If so, how often? If it's not acceptable, then you have a
problem. On a any machine that lacks infinite numerical precision, you
can never know whether an infinitely small point lies on an infinitely
thin line segment.
In the end, you're going to have to do something like this:
"Draw two lines, one from each point in the line segment to the
candidate point. If these lines' slopes are equal but differ in sign
(e.g. X and -X), then your point lies on the segment."
Of course, what does it mean for slope values to be "equal" on a digital
computer? Unless you can represent the problem entirely symbolically,
"equal" will be necessarily limited by the representation of the data
and the accuracy of the math. In the real world, epsilon is a necessary
evil.
[color=blue]
>[color=green]
>>2) When exactly do you want the test to return "true"? I.e.: how
>>close to the ideal segment does a point have to be to still be
>>considered "on" it?[/color]
>
>
> As precise as possible. (Floating point precision, no rounding)[/color]
This is an oxymoron. Floating point is inescapably about rounding.
Digital representations of floating point are approximations, as are the
math operations that manipulate them. The programmer has to manage
numerical imprecision on computers or live with wrong answers.
For some perspective, read David Goldberg's "What Every Computer
Scientist Should Know About Floating-Point Arithmetic":
Randy
Skybuck Flying wrote:[color=blue]
> "Hans-Bernhard Broeker" <broeker@physik .rwth-aachen.de> wrote in message
> news:3p0cvdF86n t0U1@news.dfnci s.de...
>[color=green]
>>In comp.graphics.a lgorithms Skybuck Flying <nospam@hotmail .com> wrote:
>>
>>[Massive crossposting reduced to single group. Please, Skybuck, try
>>to follow netiquette a bit better in the future. If you feel a need
>>to X-post, at be a bit more selective to where --- this had *no*
>>business being posted to comp.arch or sci.electronics . And *always*
>>select a single group for the follow-ups.][/color]
>
> No, I like to have input from all those newsgroups since this is about
> optimizations and those newsgroup are related to software and hardware
> optimizations. C/Delphi/Asm for software/cpu optimizations and
> comp.arch/asm/eletronics about hardware optimizations and algorithms
> possibly about algorithm optimization. Those newsgroups are likely to
> contain people who know something about this.[/color]
I'm with Hans on this one. Skybuck, by your logic you should have
posted to ALL newsgroups, since there's probably someone in every
newsgroup who will be somehow "related to optimization". :-(
Comp.graphics.a lgorithms was the right place. Comp.lang.c is hard to
justify, since your question has nothing to do with C. But there's no
way to justify posting to sci.electronics .design, comp.arch, or
alt.comp.lang.b orland-delphi. Nor comp.lang.asm, unless you want a
response written in assembly language.
That said, other appropriate forums might have included comp.programmin g
or *maybe* comp.theory.
....[color=blue][color=green]
>>
>>You need to define the task a good deal more cleanly before an
>>implementatio n can reasonably be suggested. Missing information:
>>
>>1) What's a line segment, in this case? It may come as a surprise to
>>you, but ideas about the details of this actually differ. So: how do
>>you actually specify this line segment?[/color]
>
>
> A 2D line segment defined by (x1,y1) - (x2,y2)[/color]
Integer or floating point? In either case, how big are your points and
how fat is the line? Is it acceptable to return a false positive or
negative? If so, how often? If it's not acceptable, then you have a
problem. On a any machine that lacks infinite numerical precision, you
can never know whether an infinitely small point lies on an infinitely
thin line segment.
In the end, you're going to have to do something like this:
"Draw two lines, one from each point in the line segment to the
candidate point. If these lines' slopes are equal but differ in sign
(e.g. X and -X), then your point lies on the segment."
Of course, what does it mean for slope values to be "equal" on a digital
computer? Unless you can represent the problem entirely symbolically,
"equal" will be necessarily limited by the representation of the data
and the accuracy of the math. In the real world, epsilon is a necessary
evil.
[color=blue]
>[color=green]
>>2) When exactly do you want the test to return "true"? I.e.: how
>>close to the ideal segment does a point have to be to still be
>>considered "on" it?[/color]
>
>
> As precise as possible. (Floating point precision, no rounding)[/color]
This is an oxymoron. Floating point is inescapably about rounding.
Digital representations of floating point are approximations, as are the
math operations that manipulate them. The programmer has to manage
numerical imprecision on computers or live with wrong answers.
For some perspective, read David Goldberg's "What Every Computer
Scientist Should Know About Floating-Point Arithmetic":
Randy
Comment