Debian Codegraph @

examples/oointro.scm

raw
1 ;;
2 ;; This is an excerpt of the example in the section
3 ;; "Introduction to the object system" in the reference manual.
4 ;;
5
6 (use scheme.list)
7
8 ;;;
9 ;;; 2D points
10 ;;;
11 (define-class <2d-point> ()
12 ((x :init-value 0.0 :init-keyword :x :accessor x-of)
13 (y :init-value 0.0 :init-keyword :y :accessor y-of)))
14
15 (define-class <2d-vector> ()
16 ((x :init-value 0.0 :init-keyword :x :accessor x-of)
17 (y :init-value 0.0 :init-keyword :y :accessor y-of)))
18
19 ;; Define some methods
20 (define-method move-by! ((pt <2d-point>) dx dy)
21 (inc! (x-of pt) dx)
22 (inc! (y-of pt) dy))
23
24 (define-method move-by! ((pt <2d-point>) (vec <2d-vector>))
25 (move-by! pt (x-of vec) (y-of vec)))
26
27 (define-method move-by! ((pt <2d-point>) (c <complex>))
28 (move-by! pt (real-part c) (imag-part c)))
29
30 (define-method point->list ((pt <2d-point>))
31 (list (x-of pt) (y-of pt)))
32
33 ;; Define a customized writer and comparison methods
34 (define-method write-object ((pt <2d-point>) port)
35 (format port "[[~a, ~a]]" (x-of pt) (y-of pt)))
36
37 (define-method write-object ((vec <2d-vector>) port)
38 (format port "<<~a, ~a>>" (x-of vec) (y-of vec)))
39
40 (define-method object-equal? ((a <2d-point>) (b <2d-point>))
41 (and (equal? (x-of a) (x-of b))
42 (equal? (y-of a) (y-of b))))
43
44 ;;;
45 ;;; Drawing shapes
46 ;;;
47 (define-class <shape> ()
48 ((color :init-value '(0 0 0) :init-keyword :color)
49 (thickness :init-value 2 :init-keyword :thickness)))
50
51 (define *shapes* '()) ;; global shape list
52
53 (define-method initialize ((self <shape>) initargs)
54 (next-method)
55 (push! *shapes* self))
56
57 (define-class <ps-device> () ())
58
59 (define-class <point-shape> (<shape>)
60 ((point :init-form (make <2d-point>) :init-keyword :point)))
61
62 (define-class <polyline-shape> (<shape>)
63 ((points :init-value '() :init-keyword :points)
64 (closed :init-value #f :init-keyword :closed)))
65
66 (define-method draw ((shapes <list>) (device <ps-device>))
67 (format #t "%%\n")
68 (for-each (cut draw <> device) shapes)
69 (format #t "showpage\n"))
70
71 (define-method draw ((self <shape>) (device <ps-device>))
72 (format #t "gsave\n")
73 (draw-path self device)
74 (apply format #t "~a ~a ~a setrgbcolor\n" (ref self 'color))
75 (format #t "~a setlinewidth\n" (ref self 'thickness))
76 (format #t "stroke\n")
77 (format #t "grestore\n"))
78
79 (define-method draw-path ((self <point-shape>) (device <ps-device>))
80 (apply format #t "newpath ~a ~a 1 0 360 arc closepath\n"
81 (point->list (ref self 'point))))
82
83 (define-method draw-path ((self <polyline-shape>) (device <ps-device>))
84 (let ((pts (ref self 'points)))
85 (when (>= (length pts) 2)
86 (format #t "newpath\n")
87 (apply format #t "~a ~a moveto\n" (point->list (car pts)))
88 (for-each (lambda (pt)
89 (apply format #t "~a ~a lineto\n" (point->list pt)))
90 (cdr pts))
91 (when (ref self 'closed)
92 (apply format #t "~a ~a lineto\n" (point->list (car pts))))
93 (format #t "closepath\n"))))
94
95 ;;;
96 ;;; A sample shape
97 ;;;
98 (use math.const)
99
100 (define (shape-sample)
101
102 ;; creates 5 corner points of pentagon
103 (define (make-corners scale)
104 (map (lambda (i)
105 (let ((pt (make <2d-point>)))
106 (move-by! pt (make-polar scale (* i 2/5 pi)))
107 (move-by! pt 200 200)
108 pt))
109 (iota 5)))
110
111 (set! *shapes* '()) ;; clear the shape list
112 (let* ((corners (make-corners 100)))
113 ;; a pentagon in green
114 (make <polyline-shape>
115 :color '(0 1 0) :closed #t
116 :points corners)
117 ;; a star-shape in blue
118 (make <polyline-shape>
119 :color '(1 0 0) :closed #t
120 :points (list (list-ref corners 0)
121 (list-ref corners 2)
122 (list-ref corners 4)
123 (list-ref corners 1)
124 (list-ref corners 3)))
125 ;; put dots in each corner of the star
126 (for-each (cut make <point-shape> :point <>)
127 (make-corners 90))
128 ;; draw the shapes
129 (draw *shapes* (make <ps-device>)))
130 )
131
132 ;; (with-output-to-file "oointro.ps" shape-sample)