-
Notifications
You must be signed in to change notification settings - Fork 35
/
shapes.lisp
199 lines (178 loc) · 7.25 KB
/
shapes.lisp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(in-package #:opticl)
(defun fill-image (img &rest vals)
(with-image-bounds (height width)
img
(loop for i below height
do (loop for j below width
do
(setf (pixel img i j) (values-list vals))))))
(defun fill-image* (img list)
(multiple-value-call #'fill-image img (values-list list)))
(defun horizontal-line (img y x0 x1 &rest vals)
(declare (type fixnum y x0 x1))
(with-image-bounds (ymax xmax)
img
(let ((y (constrain y 0 (1- ymax)))
(x0 (constrain x0 0 (1- xmax)))
(x1 (constrain x1 0 (1- xmax))))
(loop for x fixnum from x0 to x1
do (setf (pixel img y x) (values-list vals))))))
(defun horizontal-line* (img y x0 x1 list)
(multiple-value-call #'horizontal-line img y x0 x1 (values-list list)))
(defun vertical-line (img y0 y1 x &rest vals)
(declare (type fixnum y0 y1 x))
(with-image-bounds (ymax xmax)
img
(let ((y0 (constrain y0 0 (1- ymax)))
(y1 (constrain y1 0 (1- xmax)))
(x (constrain x 0 (1- xmax))))
(loop for y fixnum from y0 to y1
do (when (pixel-in-bounds img y x)
(setf (pixel img y x) (values-list vals)))))))
(defun vertical-line* (img y0 y1 x list)
(multiple-value-call #'vertical-line img y0 y1 x (values-list list)))
(defun draw-line (img y0 x0 y1 x1 &rest vals)
(declare (type fixnum y0 x0 y1 x1))
(let ((dx (- x1 x0))
(dy (- y1 y0)))
(declare (type fixnum dx dy))
(let ((absdx (abs dx))
(absdy (abs dy)))
(declare (type fixnum absdx absdy))
(let ((xstep (if (minusp dx) -1 1))
(ystep (if (minusp dy) -1 1)))
(if (>= absdx absdy)
(let ((d (- (* 2 absdy) absdx))
(incr-e (* 2 absdy))
(incr-ne (* 2 (- absdy absdx)))
(x x0)
(y y0))
(declare (type fixnum d incr-e incr-ne x y))
(when (pixel-in-bounds img y x)
(setf (pixel img y x) (values-list vals)))
(dotimes (i absdx)
(cond
((<= d 0)
(incf d incr-e)
(incf x xstep))
(t
(incf d incr-ne)
(incf x xstep)
(incf y ystep)))
(when (pixel-in-bounds img y x)
(setf (pixel img y x) (values-list vals)))))
(let ((d (- (* 2 absdx) absdy))
(incr-n (* 2 absdx))
(incr-ne (* 2 (- absdx absdy)))
(x x0)
(y y0))
(declare (type fixnum d incr-n incr-ne x y))
(when (pixel-in-bounds img y x)
(setf (pixel img y x) (values-list vals)))
(dotimes (i absdy)
(cond
((<= d 0)
(incf d incr-n)
(incf y ystep))
(t
(incf d incr-ne)
(incf y ystep)
(incf x xstep)))
(when (pixel-in-bounds img y x)
(setf (pixel img y x) (values-list vals))))))))))
(defun draw-line* (img y0 x0 y1 x1 list)
(multiple-value-call #'draw-line img y0 x0 y1 x1 (values-list list)))
(defun draw-circle (img center-y center-x radius &rest vals)
"draws a circle centered at (x, y) with radius r on a image."
(declare (type fixnum center-y center-x radius))
(flet ((pixel-if (y x vals)
(when (pixel-in-bounds img y x)
(setf (pixel img y x) (values-list vals)))))
(flet ((circle-points (y x)
(pixel-if (+ center-y y) (+ center-x x) vals)
(pixel-if (+ center-y x) (+ center-x y) vals)
(pixel-if (- center-y x) (+ center-x y) vals)
(pixel-if (- center-y y) (+ center-x x) vals)
(pixel-if (- center-y y) (- center-x x) vals)
(pixel-if (- center-y x) (- center-x y) vals)
(pixel-if (+ center-y x) (- center-x y) vals)
(pixel-if (+ center-y y) (- center-x x) vals)))
(let ((x 0)
(y radius)
(d (- 1 radius))
(delta-e 3)
(delta-se (+ (* -2 radius) 5)))
(declare (type fixnum x y d delta-e delta-se))
(circle-points y x)
(do () ((>= x y))
(if (< d 0)
(progn
(incf d delta-e)
(incf delta-e 2)
(incf delta-se 2))
(progn
(incf d delta-se)
(incf delta-e 2)
(incf delta-se 4)
(decf y)))
(incf x)
(circle-points y x))))))
(defun draw-circle* (img center-y center-x radius list)
(multiple-value-call #'draw-circle img center-y center-x radius (values-list list)))
(defun fill-circle (img center-y center-x radius &rest vals)
"draws a filled circle centered at (x, y) with radius r on a image."
(declare (type fixnum center-y center-x radius))
(flet ((circle-lines (y x)
(apply #'horizontal-line img (- center-y y) (- center-x x) (+ center-x x) vals)
(apply #'horizontal-line img (- center-y x) (- center-x y) (+ center-x y) vals)
(apply #'horizontal-line img (+ center-y y) (- center-x x) (+ center-x x) vals)
(apply #'horizontal-line img (+ center-y x) (- center-x y) (+ center-x y) vals)))
(let ((x 0)
(y radius)
(d (- 1 radius))
(delta-e 3)
(delta-se (+ (* -2 radius) 5)))
(declare (type fixnum x y d delta-e delta-se))
(circle-lines y x)
(do () ((>= x y))
(if (< d 0)
(progn
(incf d delta-e)
(incf delta-e 2)
(incf delta-se 2))
(progn
(incf d delta-se)
(incf delta-e 2)
(incf delta-se 4)
(decf y)))
(incf x)
(circle-lines y x)))))
(defun fill-circle* (img center-y center-x radius list)
(multiple-value-call #'fill-circle img center-y center-x radius (values-list list)))
(defun draw-rectangle (img y0 x0 y1 x1 &rest vals)
(apply #'horizontal-line img y0 x0 x1 vals)
(apply #'vertical-line img y0 y1 x0 vals)
(apply #'vertical-line img y0 y1 x1 vals)
(apply #'horizontal-line img y1 x0 x1 vals))
(defun draw-rectangle* (img y0 x0 y1 x1 list)
(multiple-value-call #'draw-rectangle img y0 x0 y1 x1 (values-list list)))
(defun fill-rectangle (img y0 x0 y1 x1 &rest vals)
(loop for x from x0 to x1
do
(apply #'vertical-line img y0 y1 x vals)))
(defun fill-rectangle* (img y0 x0 y1 x1 list)
(multiple-value-call #'fill-rectangle img y0 x0 y1 x1 (values-list list)))
(defun draw-triangle (img y0 x0 y1 x1 y2 x2 &rest vals)
(apply #'draw-line img y0 x0 y1 x1 vals)
(apply #'draw-line img y1 x1 y2 x2 vals)
(apply #'draw-line img y2 x2 y0 x0 vals))
(defun draw-triangle* (img y0 x0 y1 x1 y2 x2 list)
(multiple-value-call #'draw-triangle img y0 x0 y1 x1 y2 x2 (values-list list)))
(defun draw-polygon (img points &rest vals)
(loop for p across points
do (let ((p1 (elt p 0))
(p2 (elt p 1)))
(when (and (consp p1) (consp p2))
(apply #'draw-line img (car p1) (cdr p1) (car p2) (cdr p2) vals)))))
(defun draw-polygon* (img points list)
(multiple-value-call #'draw-polygon img points (values-list list)))