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

Duncan Hanson duncan.hanson at gmail.com
Tue May 9 01:39:27 CEST 2006


SVN commit 538815 by dhanson:

BUG:117822 mostly fixed.

 M  +42 -4     kstvcurve.cpp  


--- trunk/extragear/graphics/kst/src/libkstmath/kstvcurve.cpp #538814:538815
@@ -1613,10 +1613,48 @@
 
 
 double KstVCurve::distanceToPoint(double xpos, double dx, double ypos) const {
-  int i_near_x = getIndexNearXY(xpos, dx, ypos);
-  double near_x, near_y;
-  point(i_near_x, near_x, near_y);
-  return fabs(ypos - near_y);
+// find the y distance between the curve and a point.
+  KstVectorPtr xv = *_inputVectors.find(COLOR_XVECTOR);
+  KstVectorPtr yv = *_inputVectors.find(COLOR_YVECTOR);
+  if (!xv || !yv) {
+    return 0; // anything better we can do?
+  }
+
+  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;
+
+    while (i_bot + 1 < i_top) {
+      int i0 = (i_top + i_bot)/2;
+
+      double rX = xv->interpolate(i0, NS);
+      if (xpos < rX) {
+        i_top = i0;
+      } else {
+        i_bot = i0;
+      }
+    }
+    // end borrowed
+
+    double x_bot, y_bot, x_top, y_top;
+    point(i_bot,x_bot,y_bot);
+    point(i_top,x_top,y_top);
+
+    double near_y = (y_top-y_bot)*(xpos-x_bot) + y_bot; // calculate y value for line segment between x_bot and x_top.
+
+    return( 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);
+    return fabs(ypos - near_y);
+  }
 }
 
 void KstVCurve::paintLegendSymbol(KstPainter *p, const QRect& bound) {


More information about the Kst mailing list