in reply to Clockwise or Counter-clockwise

sub direction{ local $_; push @_, $_[0]; $_ += $a->[0] * $b->[1] - $a->[1] * $b->[0] while ($a,$b)=(shift,$_[0]), @_; $_ < 0; } my @unitCubeAnti = ([0,0],[0,1],[1,1],[1,0]); print direction(@unitCubeAnti) ? 'Anticlockwise' : 'Clockwise'; my @unitCubeClock = reverse @unitCubeAnti; print direction(@unitCubeClock) ? 'Anticlockwise' : 'Clockwise';

Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Replies are listed 'Best First'.
Re: Re: Clockwise or Counter-clockwise
by tall_man (Parson) on Feb 20, 2003 at 19:18 UTC
    Your example will not run as given because it destroys the @unitCubeAnti within the direction subroutine. I suggest copying the input list to a local variable first:
    sub direction{ local $_; my (@poly) = @_; push @poly, $poly[0]; $_ += $a->[0] * $b->[1] - $a->[1] * $b->[0] while ($a,$b)=(shift @poly,$poly[0]), @poly; print "result is $_\n"; $_ < 0; } my @unitCubeAnti = ([0,1],[1,1],[0,0],[1,0]); print direction(@unitCubeAnti) ? 'Anticlockwise' : 'Clockwise'; print "\n"; my @unitCubeClock = reverse @unitCubeAnti; print direction(@unitCubeClock) ? 'Anticlockwise' : 'Clockwise'; print "\n";
    I tested a self-crossing unit "cube" in the input instead of a straight one, and got the result "0". This refutes my idea that self-crossing polygons will work with this algorithm.