From 7ec63321413f10f68f389140c7488b2bb7d14fac Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Sun, 7 Apr 2013 19:53:15 -0400 Subject: [PATCH] split apart the math in nearest_point_index and short-circuit if we know the candidate is no good --- lib/Slic3r/Geometry.pm | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/Slic3r/Geometry.pm b/lib/Slic3r/Geometry.pm index e1a591025f..9f0a631f41 100644 --- a/lib/Slic3r/Geometry.pm +++ b/lib/Slic3r/Geometry.pm @@ -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; }