split apart the math in nearest_point_index and short-circuit if we know the candidate is no good

This commit is contained in:
Jesse Vincent 2013-04-07 19:53:15 -04:00
parent e8ca1e59a6
commit 7ec6332141

View File

@ -248,13 +248,22 @@ sub nearest_point_index {
my $point_y = $point->[Y];
for my $i (0..$#$points) {
my $d = (($point_x - $points->[$i]->[X])**2) + (($point_y - $points->[$i]->[Y])**2);
if (!defined $distance || $d < $distance) {
$nearest_point_index = $i;
$distance = $d;
last if $distance < epsilon;
}
my $d = ($point_x - $points->[$i]->[X])**2;
# If the X distance of the candidate is > than the total distance of the
# best previous candidate, we know we don't want it
next if (defined $distance && $d > $distance);
# If the total distance of the candidate is > than the total distance of the
# best previous candidate, we know we don't want it
$d += ($point_y - $points->[$i]->[Y])**2;
next if (defined $distance && $d > $distance);
$nearest_point_index = $i;
$distance = $d;
last if $distance < epsilon;
}
return $nearest_point_index;
}