Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reordering pairs in perl array

Tags:

arrays

perl

I have a series of 2-D coordinates being supplied to a perl program from another program. There are 4 of these and they make up a quad, giving 8 numbers in total, e.g:

x1 y1 x2 y2 x3 y3 x4 y4

I want to ensure that they are all specified in the same order, i.e. clockwise or counter clockwise. I already know how to do this and am doing it by looking at the sign of a cross product.

use strict;
use warnings;

my $line = "-0.702083 0.31 -0.676042 -0.323333 0.74375 -0.21 0.695833 0.485";
my @coord = split(/[,\s]+/, $line);

# Vector cross product (Z is 0) to test CW/CCW
my @v1 = (-$coord[2]+$coord[0], -$coord[3]+$coord[1]);
my @v2 = (-$coord[2]+$coord[4], -$coord[3]+$coord[5]);
my $cross = ($v1[0]*$v2[1]) - ($v1[1]*$v2[0]);

Once I've worked out if the order needs to be changed I currently change it using:

@coord = ($coord[6], $coord[7], $coord[4], $coord[5], 
          $coord[2], $coord[3], $coord[0], $coord[1]) if ($cross < 0);

This works, but I'm fairly sure it's not the nicest way of writing it in perl. Is there a more elegant, "perly" way of writing this change in order? Something that would work for $n 2-D pairs preferably. It's not a simple reverse the elements of an array problem.

like image 275
Flexo Avatar asked Aug 23 '11 09:08

Flexo


1 Answers

The last couple of lines can be rewritten using an array slice:

@coord = @coord[6,7,4,5,2,3,0,1] if $cross < 0;

To process an arbitrary number of pairs, you can use List::MoreUtils::natatime

use List::MoreUtils 'natatime';   

my $it = natatime 2, @coord;
@coord = (); 

while (my @vals = $it->()) {
    unshift @coord, @vals;
}
like image 114
Eugene Yarmash Avatar answered Oct 13 '22 00:10

Eugene Yarmash