Subject: | speedup for contains_point |
contains_point calls parts() and points() many times, when only one call is necessary. Here's a
fix.
sub contains_point {
my ( $self, $point ) = @_;
return 0 unless $self->bounds_contains_point( $point );
my $a = 0;
my ( $x0, $y0 ) = ( $point->X, $point->Y );
my @parts = $self->parts;
my @points = $self->points;
foreach my $index (1 .. $self->num_parts) {
my ( $x1, $y1 );
$index -= 1; # shift to a 0 index
my $beg = $parts[$index] || 0;
my $end = $parts[$index+1] || 0;
$end -= 1;
if($end < 0) { $end = $#points; }
foreach my $p2 (@points[$beg .. $end]){
my $x2 = $p2->X - $x0;
my $y2 = $p2->Y - $y0;
if ( defined( $y1 ) && ( ( $y2 >= 0 ) != ( $y1 >= 0 ) ) ) {
my $isl = $x1*$y2 - $y1*$x2;
if ( $y2 > $y1 ) {
--$a if $isl > 0;
} else {
++$a if $isl < 0;
}
}
( $x1, $y1 ) = ( $x2, $y2 );
}
}
return $a;
}