By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Oct 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
JD wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 8 '07 #2

P: n/a
Victor Bazarov wrote:
JD wrote:
>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.
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.
Oct 8 '07 #3

P: n/a
On 2007-10-08 21:03, JD wrote:
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.

--
Erik Wikström
Oct 8 '07 #4

P: n/a
On Oct 9, 12:54 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-08 21:03, JD wrote:
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 toknow
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.
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.

Oct 9 '07 #5

P: n/a
"JD" <jd*******@yahoo.comwrote in message
news:ra******************@newssvr13.news.prodigy.n et...
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;
Oct 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.