Re: Distance point <=> straight line in space
- From: "Bartc" <bc@xxxxxxxxxx>
- Date: Thu, 31 Jul 2008 10:42:29 GMT
"Daniel Kraft" <d@xxxxxxxx> wrote in message news:g6qck5$evf$1@xxxxxxxxxxxxxxxxxxxxxxxx
Hi,
in a program, I've got a straight line in space defined by two points (p1 and p2) on it, and a set (~ 10000) points q_i in space. Then I need to calculate for each of those points the distance to the straight. At the moment I'm doing it like this:
vector g = normalized (p2 - p1) // Find normalized direction
for i = 1 to n
distance_i = norm (cross_product (p2 - q_i, g))
I believe this formula is fairly straight-forward (a single cross-product and a norm); unfortunatelly, my program spents a large amount of time (~ 95% of total runtime) in this routine and the current duration is unacceptable.
Is there a better approach I could take to do this calculation that requires less operations?
Doing this 10,000 times shouldn't take any time at all. So you must be repeating the whole thing, presumably with a different set of points and/or a different line to measure to.
Have you looked globally at your code to see if all these calculations are necessary? Is every result of the 10000 used? (Perhaps you can calculate the distance on demand). If the line is constant, do any of the points repeat (so you can remember the distance)?
Are you calculating some sort of minimum or maximum distance? (Then there might be some quick way of rejecting many of the points.)
Where do the points come from? Would it be feasible to create them as vectors from the line endpoint anyway? Would it be feasible to reorientate the line and points so that the line is along the X,Y or Z axis? Then the calculation is simpler.
And, you might want to take a closer look at the generated code (only because I don't trust optimisers myself).
--
Bartc
.
- Follow-Ups:
- Re: Distance point <=> straight line in space
- From: Daniel Kraft
- Re: Distance point <=> straight line in space
- References:
- Distance point <=> straight line in space
- From: Daniel Kraft
- Distance point <=> straight line in space
- Prev by Date: Re: Distance point <=> straight line in space
- Next by Date: Re: Distance point <=> straight line in space
- Previous by thread: Re: Distance point <=> straight line in space
- Next by thread: Re: Distance point <=> straight line in space
- Index(es):
Relevant Pages
|