home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d386 / xlispstat.lha / XLispStat / src1.lzh / IView / iviewscale.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-03  |  4.0 KB  |  165 lines

  1. /* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
  2. /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
  3. /* You may give out copies of this software; for conditions see the    */
  4. /* file COPYING included with this distribution.                       */
  5.  
  6. #include "xlisp.h"
  7. #include "osdef.h"
  8. #ifdef ANSI
  9. #include "xlproto.h"
  10. #include "xlsproto.h"
  11. #include "iviewproto.h"
  12. #else
  13. #include "xlfun.h"
  14. #include "xlsfun.h"
  15. #include "iviewfun.h"
  16. #endif ANSI
  17.  
  18. /* forward declarations */
  19. #ifdef ANSI
  20. double dmin(double,double),dmax(double,double);
  21. void adjust_range_step(double,double *,double *,int *),
  22.      apply_scale_shift_data(IVIEW_WINDOW,unsigned,double,double),
  23.      scale_to_range(IVIEW_WINDOW,unsigned,double,double);
  24. #else
  25. double dmin(),dmax();
  26. void adjust_range_step(),
  27.      apply_scale_shift_data(),
  28.      scale_to_range();
  29. #endif
  30.  
  31. #ifndef TRUE
  32. #define TRUE 1
  33. #endif TRUE
  34. #ifndef FALSE
  35. #define FALSE 0
  36. #endif FALSE
  37.  
  38. static double dmin(x, y) double x, y; { return((x > y) ? y : x); }
  39. static double dmax(x, y) double x, y; { return((x > y) ? x : y); }
  40.  
  41. static void adjust_range_step(value, low, high, inited)
  42.     double value, *low, *high;
  43.     int *inited;
  44. {
  45.   if (*inited) { 
  46.     *low = dmin(*low, value); 
  47.     *high = dmax(*high, value);
  48.   }
  49.   else {
  50.     *low = value;
  51.     *high = value;
  52.     *inited = TRUE;
  53.   }
  54. }
  55.  
  56. void IViewGetVisibleRange(w, var, plow, phigh)
  57.     IVIEW_WINDOW w;
  58.     unsigned var;
  59.     double *plow, *phigh;
  60. {
  61.   int inited, i, n;
  62.   double value, low, high;
  63.   
  64.   inited = FALSE;
  65.   
  66.   n = IViewNumPoints(w);
  67.   for (i = 0; i < n; i++) {
  68.     if (! IViewPointMasked(w, i) && IViewPointState(w, i) != pointInvisible) {
  69.       value = IViewPointScaledValue(w, var, i);
  70.       adjust_range_step(value, &low, &high, &inited);
  71.     }
  72.   }
  73.   n = IViewNumLines(w);
  74.   for (i = 0; i < n; i++) {
  75.     if (! IViewLineMasked(w, i)) {
  76.       value = IViewLineScaledValue(w, var, i);
  77.       adjust_range_step(value, &low, &high, &inited);
  78.     }      
  79.   }
  80. #ifdef USESTRINGS
  81.   n = IViewNumStrings(w);
  82.   for (i = 0; i < n; i++) {
  83.     if (! IViewStringMasked(w, i)) {
  84.     value = IViewStringScaledValue(w, var, i);
  85.       adjust_range_step(value, &low, &high, &inited);
  86.     }
  87.   }
  88. #endif /* USESTRINGS */
  89.   if (plow != nil) *plow = IViewDecodeValue(w, low, var);
  90.   if (phigh != nil) *phigh = IViewDecodeValue(w, high, var);
  91. }
  92.  
  93. static void apply_scale_shift_data(w, var, scale, shift)
  94.     IVIEW_WINDOW w;
  95.     unsigned var;
  96.     double scale, shift;
  97. {
  98.   double value;
  99.   int i, n;
  100.  
  101.   if (var >= IViewNumVariables(w)) return;
  102.   IViewSetScale(w, var, scale * IViewScale(w, var));
  103.   IViewSetShift(w, var, scale * IViewShift(w, var) + shift);
  104.   
  105.   n = IViewNumPoints(w);
  106.   for (i = 0; i < n; i++) {
  107.     value = IViewPointScaledValue(w, var, i);
  108.     value = scale * value + shift;
  109.     IViewSetPointScaledValue(w, var, i, value);
  110.   }
  111.   n = IViewNumLines(w);
  112.   for (i = 0; i < n; i++) {
  113.     value = IViewLineScaledValue(w, var, i);
  114.     value = scale * value + shift;
  115.     IViewSetLineScaledValue(w, var, i, value);
  116.   }
  117. #ifdef USESTRINGS
  118.   n = IViewNumStrings(w);
  119.   for (i = 0; i < n; i++) {
  120.     value = IViewStringScaledValue(w, var, i);
  121.     value = scale * value + shift;
  122.     IViewSetStringScaledValue(w, var, i, value);
  123.   }
  124. #endif /* USESTRINGS */
  125. }
  126.  
  127. static void scale_to_range(w, var, low, high)
  128.     IVIEW_WINDOW w;
  129.     unsigned var;
  130.     double low, high;
  131. {
  132.   double old_low, old_high, scale, shift, old_scale, old_shift;
  133.  
  134.   if (var >= IViewNumVariables(w)) return;
  135.   IViewGetVisibleRange(w, var, &old_low, &old_high);
  136.   if (old_high <= old_low) return;
  137.   scale = (high - low) / (old_high - old_low);
  138.   shift = - scale * old_low + low;
  139.  
  140.   old_scale = IViewScale(w, var);
  141.   old_shift = IViewShift(w, var);
  142.   if (old_scale > 0.0) {
  143.     scale = scale / old_scale;
  144.     shift = shift - scale * old_shift;
  145.   }
  146.   
  147.   apply_scale_shift_data(w, var, scale, shift);
  148. }
  149.  
  150. void IViewScaleToRange(w, var, low, high)
  151.     IVIEW_WINDOW w;
  152.     unsigned var;
  153.     double low, high;
  154. {
  155.   scale_to_range(w, var, low, high);
  156. }
  157.  
  158. void IViewApplyScaleShift(w, var, scale, shift)
  159.     IVIEW_WINDOW w;
  160.     unsigned var;
  161.     double scale, shift;
  162. {
  163.   apply_scale_shift_data(w, var, scale, shift);
  164. }
  165.