[Kst] kdeextragear-2/kst/kst

Andrew Walker arwalker at sumusltd.com
Mon Aug 16 17:41:51 CEST 2004


CVS commit by arwalker: 

Fix problem with highlighting nearest data point. This algorithm was working (barely) for increasing x values, but would typically fail to find the nearest point when the x values were not increasing.


  M +15 -9     kstbasecurve.cpp   1.13


--- kdeextragear-2/kst/kst/kstbasecurve.cpp  #1.12:1.13
@@ -101,9 +101,9 @@ int KstBaseCurve::getIndexNearX(double x
 /** getIndexNearXY: return index of point within (or closest too)
     x +- dx which is closest to y **/
-int KstBaseCurve::getIndexNearXY(double x, double dx, double y) {
-  int i,i0, iN, sc, index;
-  double xi, yi, dy, dyi;
-
-  sc = sampleCount();
+int KstBaseCurve::getIndexNearXY(double x, double dx_per_pix, double y) {
+  double xi, yi, dx, dxi, dy, dyi;
+  bool bFirst = true;
+  int i,i0, iN, index;
+  int sc = sampleCount();
 
   if (xIsRising()) {
@@ -129,14 +128,21 @@ int KstBaseCurve::getIndexNearXY(double 
   index = i0;
   point(index, xi, yi);
+  dx = fabs(x-xi);
   dy = fabs(y-yi);
 
   for (i=i0+1; i<=iN; i++) {
     point(i, xi, yi);
-    if (fabs(x-xi)<dx) {
+    dxi = fabs(x-xi);
+    if (dxi < dx_per_pix) {
+      dx = dxi;
       dyi = fabs(y-yi);
-      if (dyi<dy) {
-        dy = dyi;
+      if (bFirst || dyi < dy) {
+        bFirst = false;
         index = i;
+        dy = dyi;
       }
+    } else if(dxi < dx) {
+      dx = dxi;
+      index = i;
     }
   }





More information about the Kst mailing list