-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing_racket.scm
executable file
·80 lines (70 loc) · 3.23 KB
/
drawing_racket.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;;;; DRAWING.SCM
;;; Not for student reading -- uses continuation passing style for multiple
;;; value return
;;; Apply (DRAW-... WINDOW N) to a curve to compute N points on the curve
;;; and display those points with coordinates in WINDOW.
(define (draw-points-on window n)
(let ((1/n (/ 1 n)))
(lambda (curve)
(define (iter count)
(let ((t (* count 1/n)))
(let ((ct (curve t)))
(let ((x (x-of ct))
(y (y-of ct)))
((draw-pixel window) (make-posn (* (exact->inexact x) 256) (* (exact->inexact y) 256)))
(if (>= count n)
'done
(iter (+ count 1)))))))
((clear-viewport window))
(iter 0))))
(define (draw-connected window n)
(let ((1/n (/ 1 n)))
(lambda (curve)
(define (iter x-old y-old count)
(let ((t (* count 1/n)))
(let ((ct (curve t)))
(let ((x-new (x-of ct))
(y-new (y-of ct)))
((draw-line window)
(make-posn (* x-old 256) (* y-old 256))
(make-posn (* (exact->inexact x-new) 256) (* (exact->inexact y-new) 256)))
(if (>= count n)
'done
(iter x-new y-new (+ count 1)))))))
((clear-viewport window))
(let ((c0 (curve 0)))
(iter (x-of c0) (y-of c0) 1)))))
;;; Apply (DRAW-...-SQUEEZED-... WINDOW N) to a curve to squeeze it to exactly
;;; fit in WINDOW and then compute and display N points on the curve in WINDOW.
(define (draw-points-squeezed-to-window window n)
(lambda (curve)
((draw-points-on window n)
(((corners curve n) squeeze-rectangular-portion) curve))))
(define (draw-connected-squeezed-to-window window n)
(lambda (curve)
((draw-connected window n)
(((corners curve n) squeeze-rectangular-portion) curve))))
;;; CORNERS computes the max and min values of the x and y coordinates
;;; of n points on a given curve.
;;; It then applies a given procedure CORNERS-USER of type (NUM,NUM,NUM,NUM --> TYP)
;;; to the four corner values, where TYP may be any type.
;;; CORNERS is of type (CURVE,NUM) --> (((NUM,NUM,NUM,NUM) --> TYP) --> TYP),
(define (corners curve n)
(let ((1/n (/ 1 n)))
(lambda (corners-user)
;;AUX calls receiver on corners of curve segment given by
;;parameter value t in the interval [1/count, 1]
(define (aux count receiver)
(let ((point (curve (* count 1/n))))
(let ((xc (x-of point))
(yc (y-of point)))
(if (>= count n)
(receiver xc xc yc yc)
(aux (+ count 1)
(lambda (x- x+ y- y+)
(receiver
(min x- xc)
(max x+ xc)
(min y- yc)
(max y+ xc))))))))
(aux 0 corners-user))))