[Kst] extragear/graphics/kst/src/libkstmath

Duncan Hanson duncan.hanson at gmail.com
Thu May 11 04:05:11 CEST 2006


SVN commit 539587 by dhanson:

BUG:117822 some changes to KstVCurve::distanceToPoint. still plenty of room for improvement

 M  +16 -13    kstvcurve.cpp  


--- trunk/extragear/graphics/kst/src/libkstmath/kstvcurve.cpp #539586:539587
@@ -1619,10 +1619,19 @@
     return 1.0E300; // anything better we can do?
   }
 
-  double distance = 1.0E300; // default value.
+  double distance = 1.0E300;
 
+  int i_near_x = getIndexNearXY(xpos, dx, ypos);
+  double near_x, near_y;
+  point(i_near_x, near_x, near_y);
+
+  if (fabs(near_x - xpos) < dx) {
+    distance = fabs(ypos - near_y); // initial estimate.
+  }
+
   if (hasLines() && xv->isRising()) {
   // if hasLines then we should find the distance between the curve and the point, not the data and the point. if isRising because it is (probably) to slow to use this technique if the data is unordered.
+    
     // borrowed from indexNearX. use binary search to find the indices immediately above and below our xpos.
     int i_top = NS - 1;
     int i_bot = 0;
@@ -1643,19 +1652,13 @@
     point(i_bot, x_bot, y_bot);
     point(i_top, x_top, y_top);
 
-    if (x_bot < xpos && x_top > xpos) {
-      double near_y = (y_top - y_bot) * (xpos - x_bot) + y_bot; // calculate y value for line segment between   x_bot and x_top.
-      distance = fabs(ypos - near_y);
+    if (x_bot <= xpos && x_top >= xpos) {
+      near_y = (y_top - y_bot) / (x_top - x_bot) * (xpos - x_bot) + y_bot; // calculate y value for line segment between x_bot and x_top.
+      
+      if (fabs(ypos - near_y) < distance) {
+        distance = fabs(ypos - near_y);
+      }
     }
-  } else {
-  // !hasLines- find the distance between the data and the point.
-    int i_near_x = getIndexNearXY(xpos, dx, ypos);
-    double near_x, near_y;
-    point(i_near_x, near_x, near_y);
-
-    if (fabs(near_x - xpos) < dx) {
-      distance = fabs(ypos - near_y);
-    }
   }
 
   return distance;


More information about the Kst mailing list