sub edges2poly { my $edges = shift; ## Ref to the AoAoA above. ## For each edge in the list (which is reordered as this loop progresses) for my $i ( 0 .. $#$edges - 1 ) { ## Stringyfy the end-point my $target = "@{ $edges->[ $i ][ 1 ] }"; ## And scan the remain edges for my $j ( $i + 1 .. $#$edges ) { ## Comparing the target, ## first against the start if( $target eq "@{ $edges->[ $j ][ 0 ] }" ) { ## found the next edge correctly oriented. last if $j == $i + 1; ## nowt to do if its already the next edge } ## and then the end of each elsif( $target eq "@{ $edges->[ $j ][ 1 ] }" ) { ## Found it, but its ends need swapping @{ $edges->[ $j ] } = reverse @{ $edges->[ $j ] }; last if $j == $i + 1; ## nowt more to do if its the next edge } else { next; ## try the next edge } ## If we got here, we found it (and possible swapped then ends, ## But it is in the wrong place in the list, so swap the edges. @{ $edges }[ $i+1, $j ] = @{ $edges }[ $j, $i+1 ]; last; ## And skip to the next target } } ## return an AoA of just the required points for the bounding polygon. return( $edges->[ 0 ][ 0 ], map{ $_->[ 1 ] } @$edges ); }