# 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; } } } #### # 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] ] ); } } }