2
0
Эх сурвалжийг харах

Fixed an edge case in cubic curve distance evaluation

Viktor Chlumský 4 жил өмнө
parent
commit
8c6fb790b5
1 өөрчлөгдсөн 9 нэмэгдсэн , 10 устгасан
  1. 9 10
      core/edge-segments.cpp

+ 9 - 10
core/edge-segments.cpp

@@ -172,21 +172,20 @@ SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const
     // Iterative minimum distance search
     for (int i = 0; i <= MSDFGEN_CUBIC_SEARCH_STARTS; ++i) {
         double t = (double) i/MSDFGEN_CUBIC_SEARCH_STARTS;
-        for (int step = 0;; ++step) {
-            Vector2 qe = p[0]+3*t*ab+3*t*t*br+t*t*t*as-origin; // do not simplify with qa !!!
-            double distance = nonZeroSign(crossProduct(direction(t), qe))*qe.length();
-            if (fabs(distance) < fabs(minDistance)) {
-                minDistance = distance;
-                param = t;
-            }
-            if (step == MSDFGEN_CUBIC_SEARCH_STEPS)
-                break;
+        Vector2 qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
+        for (int step = 0; step < MSDFGEN_CUBIC_SEARCH_STEPS; ++step) {
             // Improve t
             Vector2 d1 = 3*as*t*t+6*br*t+3*ab;
             Vector2 d2 = 6*as*t+6*br;
             t -= dotProduct(qe, d1)/(dotProduct(d1, d1)+dotProduct(qe, d2));
-            if (t < 0 || t > 1)
+            if (t <= 0 || t >= 1)
                 break;
+            qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
+            double distance = nonZeroSign(crossProduct(direction(t), qe))*qe.length();
+            if (fabs(distance) < fabs(minDistance)) {
+                minDistance = distance;
+                param = t;
+            }
         }
     }