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"
;; nicetick by ymg (Translation code by Mark Ransom at Stack Overflow) ;
;; ;
;; Argument: minv, Minimum Value ;
;; maxv, Maximum Value ;
;; ndiv, Max. Number of Divisions ;
;; ;
;; Returns: A list of 3 items, where: ;
;; Item 1, Minimum Extent of Graph ;
;; Item 2, Maximum Extent of Graph ;
;; Item 3, Spacing of Divisions ;
;; ;
;; Notes: Requires Function Floor and Ceil ;
;; ;
(defun nicetick
(minv maxv ndiv
/ minimum magnitude residual tick
) (setq minimum
(/ (- maxv minv
) ndiv
) magnitude
(expt 10.0 (floor
(/ (log minimum
) (log 10)))) residual (/ minimum magnitude)
)
((> residual
5) (setq tick
(* 10 magnitude
))) ((> residual
2) (setq tick
(* 5 magnitude
))) ((> residual
1) (setq tick
(* 2 magnitude
))) (t
(setq tick magnitude
)) )
(list (* tick
(floor
(/ minv tick
))) ; Minimum Extent of Graph ; (* tick (ceil (/ maxv tick))) ; Maximum Extent of Graph ;
tick ; Spacing of Divisions ;
)
)
;; Floor function, Returns the largest integer not greater than x. ;
;; Ceiling function, Returns the smallest integer not less than x. ;