Author Topic: Extent and Divisions For Graph (Function nicetick)  (Read 1262 times)

0 Members and 1 Guest are viewing this topic.

ymg

  • Guest
Extent and Divisions For Graph (Function nicetick)
« on: May 08, 2015, 06:24:17 AM »
Here translation of a routine by Mark Ransom on Stack Overflow.

Might save some "Head Scratching" when you need to get a nice
extent and a resonable spacing for tick marks or divisions on a graph.

ymg

"Edited to prevent Integer division when computing magnitude"

Code - Auto/Visual Lisp: [Select]
  1. ;; nicetick    by ymg (Translation code by Mark Ransom at Stack Overflow)     ;
  2. ;;                                                                            ;
  3. ;; Argument: minv,  Minimum Value                                             ;
  4. ;;           maxv,  Maximum Value                                             ;
  5. ;;           ndiv,  Max. Number of Divisions                                  ;
  6. ;;                                                                            ;
  7. ;; Returns:  A list of 3 items, where:                                        ;
  8. ;;                     Item 1, Minimum Extent of Graph                        ;
  9. ;;                     Item 2, Maximum Extent of Graph                        ;
  10. ;;                     Item 3, Spacing of Divisions                           ;
  11. ;;                                                                            ;
  12. ;; Notes: Requires Function Floor and Ceil                                    ;
  13. ;;                                                                            ;
  14.  
  15. (defun nicetick (minv maxv ndiv / minimum magnitude residual tick)
  16.    (setq minimum (/ (- maxv minv)  ndiv)
  17.          magnitude (expt 10.0 (floor (/ (log minimum) (log 10))))
  18.          residual (/ minimum  magnitude)
  19.    )
  20.    (cond
  21.       ((> residual 5) (setq tick (* 10 magnitude)))
  22.       ((> residual 2) (setq tick (*  5 magnitude)))
  23.       ((> residual 1) (setq tick (*  2 magnitude)))
  24.       (t (setq tick magnitude))
  25.    )
  26.    
  27.    (list (* tick (floor (/ minv tick))) ; Minimum Extent of Graph             ;
  28.          (* tick (ceil  (/ maxv tick))) ; Maximum Extent of Graph             ;
  29.          tick                           ; Spacing of Divisions                ;
  30.    )     
  31. )  
  32.  
  33. ;; Floor function, Returns the largest integer not greater than x.            ;
  34. (defun floor (x) (if (minusp (rem x 1)) (- (fix x) 1) (fix x)))
  35. ;; Ceiling function, Returns the smallest integer not less than x.            ;
  36. (defun ceil  (x) (if (> (rem x 1) 0)    (+ (fix x) 1) (fix x)))
  37.  
« Last Edit: May 08, 2015, 10:32:46 AM by ymg »