how to find the two end points in a line ...

J

JD

Hi,

There are about 10+ 3-D points on the same line. I want to find the two end
points from these points. The efficiency is not an issue because there are
only 10 points or so. I just need a cleaner code to do it. Should I use
an STL set to store the distances among them and then retrieve the max
distance from the last element in the set? Then the question is how to know
which points are associated with this max distance? Your help is
appreciated.

JD
 
V

Victor Bazarov

JD said:
There are about 10+ 3-D points on the same line. I want to find the
two end points from these points. The efficiency is not an issue
because there are only 10 points or so. I just need a cleaner code
to do it. Should I use an STL set to store the distances among them
and then retrieve the max distance from the last element in the set? Then
the question is how to know which points are associated with
this max distance? Your help is appreciated.

You should be able to use any two points to calculate the actual line
(the points should be different), then pick any point and *sort* the
other points or all points based on their relative position to the
picked one. Whether you put them all in a vector (array) and sort the
vector (array), or put them in a multiset, does not matter. The trick
is to pick the right comparison function.

V
 
G

Gianni Mariani

Victor said:
You should be able to use any two points to calculate the actual line
(the points should be different), then pick any point and *sort* the
other points or all points based on their relative position to the
picked one. Whether you put them all in a vector (array) and sort the
vector (array), or put them in a multiset, does not matter. The trick
is to pick the right comparison function.

You need only pick one of the axes (x, y, or z) and do a one time scan
for the minimum and maximum. The axis you pick needs to be one that the
line co-efficient is non zero. The way to do that is to pick two
points and find the axis with the largest absolute change.
 
G

Guest

Hi,

There are about 10+ 3-D points on the same line. I want to find the two end
points from these points. The efficiency is not an issue because there are
only 10 points or so. I just need a cleaner code to do it. Should I use
an STL set to store the distances among them and then retrieve the max
distance from the last element in the set? Then the question is how to know
which points are associated with this max distance? Your help is
appreciated.

This kinds of questions are probably better asked in a groups such as
comp.graphics.algorithms. To get the endpoints transform their
coordinates to a system in which the lines is along the X-axis (or Y or
Z if you prefer) and then find the one with the smallest and greatest
X-value.
 
T

terminator

This kinds of questions are probably better asked in a groups such as
comp.graphics.algorithms. To get the endpoints transform their
coordinates to a system in which the lines is along the X-axis (or Y or
Z if you prefer) and then find the one with the smallest and greatest
X-value.

a better way can be to calculate the the 'tangent' vector and one
point on the line,and every point on the line can be represented via a
double/float index showing how many times the 'tangent' the point is
far from specific point:

point==index*tanjent+point_0;

now you can compare/sort indices.

regards,
FM.
 
J

Jim Langston

JD said:
Hi,

There are about 10+ 3-D points on the same line. I want to find the two
end points from these points. The efficiency is not an issue because there
are only 10 points or so. I just need a cleaner code to do it. Should I
use an STL set to store the distances among them and then retrieve the max
distance from the last element in the set? Then the question is how to
know which points are associated with this max distance? Your help is
appreciated.

Any of the x,y or z coordinates that change will help you. If the x
coordinate changes, just find the point with the min and max x and you have
the end points. x may not change though (if the line runs along the x axis)
then you could use the y or z. You don't really need to do any math, just
detemine which of the 3 coordinates (out of any) that are changing. You
could also sort by some arbitrary method and use the max and min of that
also. The normal method I use for sorting 3d points is in pseudo code:

operator<
if ( x1 < x2 )
return true;
else if ( x1 == x2 )
if ( y1 < y2 )
return true;
else if ( y1 == y2 )
if ( z1 < z2 )
return true;
return false;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top