[Perldl] non-rectangular selection from 2D piddle

Karl Glazebrook karlglazebrook at mac.com
Wed Sep 8 23:35:20 HST 2010


Folk,

I wrote a PDL vectorized version of the 'points in a polygon' program a few years ago for my own use. [BTW Matt the problem was selecting points in a area of a color-color diagram]

Here is the code. i/o are all vectors. Read it and weep :-)  

Karl

# Test points tx,ty to be in a polygon following 
# http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
# Nice fast VECTOR code!
sub pnpoly{
   my($tx, $ty, $vertx, $verty) = @_;
   my $testx = $tx->dummy(0);
   my $testy = $ty->dummy(0);
   my $vertxj = $vertx->rotate(1);
   my $vertyj = $verty->rotate(1);
   $c =    sumover(    (($verty>$testy) != ($vertyj>$testy)) &
	         ($testx < ($vertxj-$vertx) * ($testy-$verty) / 
	         ($vertyj-$verty) + $vertx)
	         ) %2;
  return $c;
}





More information about the Perldl mailing list