[Kst] kdeextragear-2/kst/kst

Barth Netterfield netterfield at astro.utoronto.ca
Fri Jan 30 15:03:49 CET 2004


CVS commit by netterfield: 

Improve datamode - better rules for what 'closest pixel' means.
Some compiler warning fixes.


  M +43 -0     kstbasecurve.cpp   1.9
  M +1 -0      kstbasecurve.h   1.14
  M +4 -4      kstplot.cpp   1.39
  M +2 -2      kstplot.h   1.20
  M +15 -5     kstview.cpp   1.79


--- kdeextragear-2/kst/kst/kstbasecurve.cpp  #1.8:1.9
@@ -97,4 +97,47 @@ 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();
+
+  if (xIsRising()) {
+
+    iN = i0 = getIndexNearX(x);
+    getPoint(i0, xi, yi);
+
+    while ((i0>0) && (x-dx<xi)) {
+      i0--;
+      getPoint(i0, xi, yi);
+    }
+    getPoint(iN, xi, yi);
+
+    while ((iN<sc-1) && (x+dx>xi)) {
+      iN++;
+      getPoint(iN, xi, yi);
+    }
+  } else {
+    i0 = 0;
+    iN = sampleCount()-1;
+  }
+
+  index = i0;
+  getPoint(index, xi, yi);
+  dy = fabs(y-yi);
+
+  for (i=i0+1; i<=iN; i++) {
+    getPoint(i, xi, yi);
+    dyi = fabs(y-yi);
+    if (dyi<dy) {
+      dy = dyi;
+      index = i;
+    }
+  }
+  return(index);
+}
+
 
 // vim: ts=2 sw=2 et

--- kdeextragear-2/kst/kst/kstbasecurve.h  #1.13:1.14
@@ -41,4 +41,5 @@ public:
   virtual void getPoint(int i, double &x1, double &y1) = 0;
   virtual int getIndexNearX(double x);
+  virtual int getIndexNearXY(double x, double dx, double y);
   virtual KstCurveType type() const = 0;
 

--- kdeextragear-2/kst/kst/kstplot.cpp  #1.38:1.39
@@ -888,9 +888,9 @@ void KstPlot::drawDotAt(QPainter& p, dou
 
   double x_min, x_max, y_min, y_max;
-  double X1, Y1;
+  int X1, Y1;
   getLScale(x_min, y_min, x_max, y_max);
 
-  X1 = PlotRegion.width() * (x - x_min) / (x_max - x_min) + PlotRegion.left();
-  Y1 = PlotRegion.height() * (y_max - y) / (y_max - y_min) + PlotRegion.top();
+  X1 = int(PlotRegion.width() * (x - x_min) / (x_max - x_min) + PlotRegion.left());
+  Y1 = int(PlotRegion.height() * (y_max - y) / (y_max - y_min) + PlotRegion.top());
 
   if (!PlotRegion.contains(X1, Y1)) {
@@ -905,5 +905,5 @@ void KstPlot::drawDotAt(QPainter& p, dou
 
 
-void KstPlot::paint(QPainter &p, double in_xleft_bdr_px, bool printMode) {
+void KstPlot::paint(QPainter &p, double in_xleft_bdr_px) {
   double XTick, YTick, Xorg, Yorg; // Tick interval and position
   double x_min, x_max, y_min, y_max;

--- kdeextragear-2/kst/kst/kstplot.h  #1.19:1.20
@@ -61,5 +61,5 @@ public:
   ~KstPlot();
 
-  void paint(QPainter &pd, double X_LeftBorder=0, bool printMode = false);
+  void paint(QPainter &pd, double X_LeftBorder=0);
   void drawDotAt(QPainter& p, double x, double y);
   double getXBorder(QPainter &p);

--- kdeextragear-2/kst/kst/kstview.cpp  #1.78:1.79
@@ -244,5 +244,5 @@ void KstView::print(KPrinter *printer)
     h = 3000.0;
     p.setWindow((int)0, (int)0, (int)w, (int)h);
-    pl->paint(p, maxXBorder, true);
+    pl->paint(p, maxXBorder);
   }
 }
@@ -393,13 +393,23 @@ void KstView::updateMouse() {
     }
 
-    double xmin, ymin, xmax, ymax, xpos, ypos;
+    double xmin, ymin, xmax, ymax, xpos, ypos, dx_per_pix;
     pPlot->getLScale(xmin, ymin, xmax, ymax);
 
-    xpos = (double)(pos.x() - plot_rect.left())/(double)plot_rect.width();
-    xpos = xpos * (xmax - xmin) + xmin;
+    // find mouse location in plot units
+    xpos = (double)(pos.x() - plot_rect.left())/(double)plot_rect.width() *
+           (xmax - xmin) + xmin;
     if (pPlot->isXLog()) {
       xpos = pow(10.0, xpos);
     }
 
+    // convert 1 pixel to plot units.
+    dx_per_pix = (double)(pos.x()+2 - plot_rect.left())/
+                 (double)plot_rect.width() *
+                 (xmax - xmin) + xmin;
+    if (pPlot->isXLog()) {
+      dx_per_pix = pow(10.0, dx_per_pix);
+    }
+    dx_per_pix-=xpos;
+
     ypos = (double)(pos.y() - plot_rect.top())/(double)plot_rect.height();
     ypos = ypos * (ymin - ymax) + ymax;
@@ -421,5 +431,5 @@ void KstView::updateMouse() {
       for (KstBaseCurveList::Iterator i = pPlot->Curves.begin();
            i != pPlot->Curves.end(); ++i) {
-        i_near_x = (*i)->getIndexNearX(xpos);
+        i_near_x = (*i)->getIndexNearXY(xpos, dx_per_pix, ypos);
         (*i)->getPoint(i_near_x, near_x, near_y);
         d = fabs(ypos-near_y);





More information about the Kst mailing list