In this code block:
# p1 is starting or anchor point of the line segment
foreach my $p1 (@{$points}) {
# p2 is end point of the line segment
foreach my $p2 (@{$points}) {
# We don't need to caculate if anchor and end are the same
# or we have already seen these two pairs [reversed] before
unless ( ($p1 == $p2) || $found->{$p2}->{$p1} ) {
# Compute the edge
my $edge = sqrt(
($p1->[0] - $p2->[0])**2 +
($p1->[1] - $p2->[1])**2
);
# Push the whole thing on a stack
push (@edges, [ $edge, $p1->[0], $p1->[1], $p2->[0], $p2->[1]
+]);
# Keep track of the edges we've already computed (no need to d
+o so twice)
$found->{$p2}->{$p1} = 1;
}
}
}
Can you perhaps do:
# outer loop goes through all points
for my $i ( 0 .. $#{$points}) {
# inner loop only through those not visited yet
for my $j ( ( $i + 1 ) .. $#{$points}) {
# Compute the edge
my $edge = sqrt(
( $points->[$i][0] - $points->[$j][0] )**2 +
( $points->[$i][1] - $points->[$j][1] )**2
);
# Push the whole thing on a stack
push (@edges, [
$edge,
$points->[$i][0],
$points->[$i][1],
$points->[$j][0],
$points->[$j][1]
]
);
}
}
}
I don't know enough about the problem space to be sure this is helpful or hurtful...
-
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.