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