gscheme/examples/plotter.scm

27 lines
752 B
Scheme

(define plotter
(lambda (f res x1 x2 y1 y2)
(let* ((dx (- x2 x1)) (dy (- y2 y1)) (delta (/ dx res)))
(letrec
((scaled
(lambda (f x y)
(f
(* res (/ (- x x1) dx))
(* res (/ (- y y1) dy)))))
(plotit
(lambda (x)
(scaled draw-line x (f x))
(if (< x x2) (plotit (+ x delta))))))
(draw-color 0 0 0)
(scaled draw-move 0 y1)
(scaled draw-line 0 y2)
(scaled draw-move x1 0)
(scaled draw-line x2 0)
(draw-color 255 0 0)
(scaled draw-move x1 (f x1))
(plotit x1)))))
(plotter (lambda (x) (* x x x)) 70 -5.0 5.0 -50.0 50.0)
(plotter sin 50 -5.0 5.0 -1.0 1.0)
(plotter (lambda (x) (* x (sin x))) 100 -25.0 25.0 -25.0 25.0)
(plotter (lambda (x) (+ (* x x) (* -5 x) 6)) 80 -1.0 5.0 -3.0 10.0)