Some (untested) pseudo-code that may help (assuming from your description that you have separate arrays for each of X, Y, and Z):
- calculate distance from origin (or some other point) for each point in your set
@dist = map sqrt($x[$_]**2+$y[$_]**2+$z[$_]**2), 0..$#x;
or, for some $x0, $y0, $z0,
@dist = map sqrt(($x[$_]-$x0)**2+($y[$_]-$y0)**2+($z[$_]-$z0)**2), 0..$#x;
- sort points by that distance
@indices = sort {$dist[$a]<=>$dist[$b]} 0..$#dist;
@dist = @dist[@indices];
@x = @x[@indices];
@y = @y[@indices];
@z = @z[@indices];
@name = @name[@incides];
- loop through your points, for each point checking distance of the following points only until the distance
from origin has increased by your cutoff
for my $one (0..$#@dist-1) {
my $two = $one;
while (++$two < @dist && $dist[$two]-$dist[$one] <= $cutoff) {
print "found pair: ($x[$one],$y[$one],$z[$one]) and
($x[$two],$y[$two],$z[$two])\n"
if sqrt(($x[$one]-$x[$two])**2+($y[$one]-$y[$two])**2+($z[
+$one]-$z[$two])**2) <= $cutoff;
}
}
This limits how much work the interior loop has to do
for each iteration of the exterior loop (unless you have
points mosty lying within cutoff distance from the surface of a sphere around your chosen origin, so some knowlege of your data will help choose a good origin).
Update: added code fragments
Update: sort the names with the points :)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|