-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathellipse.lisp
349 lines (287 loc) · 12.5 KB
/
ellipse.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
;;;-*- Mode:Common-Lisp; Package:PICTURES; Base:10 -*-
;;;
;;;
;;;
;;; TEXAS INSTRUMENTS INCORPORATED
;;; P.O. BOX 149149
;;; AUSTIN, TEXAS 78714-9149
;;;
;;; Copyright (C)1987,1988,1989,1990 Texas Instruments Incorporated.
;;;
;;; Permission is granted to any individual or institution to use, copy, modify,
;;; and distribute this software, provided that this complete copyright and
;;; permission notice is maintained, intact, in all copies and supporting
;;; documentation.
;;;
;;; Texas Instruments Incorporated provides this software "as is" without
;;; express or implied warranty.
;;;
;;; Authors: Delmar Hager, James Dutton, Teri Crowe
;;; Contributors: Kerry Kimbrough, Patrick Hogan, Eric Mielke
(in-package "PICTURES")
;Method: rectangle-size
;Method: rectangle-width and (setf rectangle-width)
;Method: rectangle-height and (setf rectangle-height)
;Method: scale-transform
(defclass ellipse (rectangle)
()
(:documentation "A graphic that represents a ellipse in either object coordinates."))
;Filled-Ellipse Class Definition:
(defclass filled-ellipse ( ellipse filled-rectangle)
()
(:documentation "Filled ellipse class in pictures"))
;Filled-Ellipse-Edge Class Definition:
(defclass filled-ellipse-edge ( ellipse filled-rectangle-edge)
()
(:documentation "Filled ellipse edge class in pictures"))
(defun make-ellipse (x-min y-min width height &rest options )
"Make a ellipse with the with the given X-MIN, Y-MIN, WIDTH and HEIGHT.
The following keyword OPTIONS are allowed: GSTATE PARENT SENESITIVITY TRANSFORM PLIST"
(APPLY #'MAKE-INSTANCE 'ellipse
:point-seq (complete-rectangle x-min y-min (+ x-min width) (+ y-min height))
options))
(defun make-filled-ellipse (x-min y-min width height &rest options )
"Make a filled-ellipse with the given X-MIN, Y-MIN, WIDTH and HEIGHT.
The following keyword OPTIONS are allowed: GSTATE PARENT SENESITIVITY TRANSFORM PLIST"
(APPLY #'MAKE-INSTANCE 'filled-ellipse
:point-seq (complete-rectangle x-min y-min (+ x-min width) (+ y-min height))
options))
(defun make-filled-ellipse-edge (x-min y-min width height &rest options )
"Make a filled-ellipse-edge with the given X-MIN, Y-MIN, WIDTH and HEIGHT.
The following keyword OPTIONS are allowed: GSTATE PARENT SENESITIVITY TRANSFORM PLIST"
(APPLY #'MAKE-INSTANCE 'filled-ellipse-edge
:point-seq (complete-rectangle x-min y-min (+ x-min width) (+ y-min height))
options))
(DEFMETHOD ellipse-origin-x ((ellipse ellipse))
(vertex-x ellipse 0)
)
(DEFMETHOD (SETF ellipse-origin-x) (origin-x (ellipse ellipse) )
(LET ((difference (- origin-x (vertex-x ellipse 0))))
(extent-changed ellipse)
(DOTIMES (pos 4 origin-x)
(SETF (vertex-x ellipse pos) (+ difference (vertex-x ellipse pos)) ))))
(DEFMETHOD ellipse-origin-y ((ellipse ellipse))
(vertex-y ellipse 0))
(DEFMETHOD (SETF ellipse-origin-y) (origin-y (ellipse ellipse) )
(LET ((difference (- origin-y (vertex-y ellipse 0))))
(extent-changed ellipse)
(DOTIMES (pos 4 origin-y)
(SETF (vertex-y ellipse pos) (+ difference (vertex-y ellipse pos)) ))))
(DEFMETHOD ellipse-width ((ellipse ellipse))
(with-slots (vertices transform) ellipse
(VALUES (distance
(vertex-x ellipse 0) (vertex-y ellipse 0) (vertex-x ellipse 1) (vertex-y ellipse 1)))))
(DEFMETHOD (SETF ellipse-width) (width (ellipse ellipse))
(with-slots (vertices transform) ellipse
(extent-changed ellipse)
(MULTIPLE-VALUE-BIND (x y dx dy) (compute-point (VERTEX-X ELLIPSE 0)(VERTEX-Y ELLIPSE 0)
(vertex-x ellipse 1)(vertex-y ellipse 1) width)
(SETF (vertex-x ellipse 1) x)
(SETF (vertex-y ellipse 1) y)
(SETF (vertex-x ellipse 2) (+ (vertex-x ellipse 2) dx))
(SETF (vertex-y ellipse 2) (+ (vertex-y ellipse 2) dy))))
ellipse)
(DEFMETHOD ellipse-height ((ellipse ellipse))
(with-slots (vertices transform) ellipse
(VALUES (distance
(vertex-x ellipse 0) (vertex-y ellipse 0) (vertex-x ellipse 3) (vertex-y ellipse 3)))))
(DEFMETHOD (SETF ellipse-height) (height (ellipse ellipse))
(with-slots (vertices transform) ellipse
(extent-changed ellipse)
(MULTIPLE-VALUE-BIND (x y dx dy) (compute-point (VERTEX-X ELLIPSE 0)(VERTEX-Y ELLIPSE 0)
(vertex-x ellipse 3)(vertex-y ellipse 3) height)
(SETF (vertex-x ellipse 3) x)
(SETF (vertex-y ellipse 3) y)
(SETF (vertex-x ellipse 2) (+ (vertex-x ellipse 2) dx))
(SETF (vertex-y ellipse 2) (+ (vertex-y ellipse 2) dy)))
ellipse))
(defmethod draw-graphic ((ellipse ellipse) (view view)
&optional min-x min-y width height)
(declare (type (or null wcoord) min-x min-y width height))
(LET ((world-transform (graphic-world-transform ellipse)))
(with-slots (vertices extent) ellipse
(WHEN (visible-p ellipse)
(with-vector temp-vertices
(copy-to-vector vertices temp-vertices)
(if (AND world-transform (= (t12 world-transform)(t21 world-transform) 0))
(PROGN
(transform-point-seq world-transform temp-vertices)
(view-draw-arc view ; Yes, use draw-ellipse to draw it
(min-value-vector temp-vertices 0)
(min-value-vector temp-vertices 1)
(- (max-value-vector temp-vertices 0)(min-value-vector temp-vertices 0))
(- (max-value-vector temp-vertices 1)(min-value-vector temp-vertices 1))
0 (* 2.0 pi)
(graphic-gstate ellipse)))
(with-vector scan-vector
(scan-ellipse (ellipse-origin-x ellipse)(ellipse-origin-y ellipse)
(ellipse-width ellipse)(ellipse-height ellipse) scan-vector
(view-pixel-size view))
(transform-point-seq world-transform scan-vector)
(view-draw-polygon view ; No, use draw-polygon to draw it
scan-vector
(graphic-gstate ellipse)))
)
))
))
ellipse)
(defmethod draw-graphic ((ellipse filled-ellipse) (view view)
&optional min-x min-y width height)
(declare (type (or null wcoord) min-x min-y width height))
(LET ((world-transform (graphic-world-transform ellipse)))
(with-slots (vertices extent) ellipse
(WHEN (visible-p ellipse)
(with-vector temp-vertices
(copy-to-vector vertices temp-vertices)
(if (AND world-transform (= (t12 world-transform)(t21 world-transform) 0))
(PROGN
(transform-point-seq world-transform temp-vertices)
(view-draw-filled-arc view ; Yes, use draw-ellipse to draw it
(min-value-vector temp-vertices 0)
(min-value-vector temp-vertices 1)
(- (max-value-vector temp-vertices 0)(min-value-vector temp-vertices 0))
(- (max-value-vector temp-vertices 1)(min-value-vector temp-vertices 1))
0 (* 2.0 pi)
(graphic-gstate ellipse)))
(with-vector scan-vector
(scan-ellipse (ellipse-origin-x ellipse)(ellipse-origin-y ellipse)
(ellipse-width ellipse)(ellipse-height ellipse) scan-vector
(view-pixel-size view))
(transform-point-seq world-transform scan-vector)
(view-draw-filled-polygon view ; No, use draw-polygon to draw it
scan-vector
(graphic-gstate ellipse)))
)
))
))
ellipse)
(defmethod draw-graphic ((ellipse filled-ellipse-edge) (view view)
&optional min-x min-y width height)
(declare (type (or null wcoord) min-x min-y width height))
(LET ((world-transform (graphic-world-transform ellipse)))
(with-slots (vertices edge-gstate extent) ellipse
(WHEN (visible-p ellipse)
(with-vector temp-vertices
(copy-to-vector vertices temp-vertices)
(if (AND world-transform (= (t12 world-transform)(t21 world-transform) 0))
(PROGN
(transform-point-seq world-transform temp-vertices)
(view-draw-filled-arc view ; Yes, use draw-ellipse to draw it
(min-value-vector temp-vertices 0)
(min-value-vector temp-vertices 1)
(- (max-value-vector temp-vertices 0)(min-value-vector temp-vertices 0))
(- (max-value-vector temp-vertices 1)(min-value-vector temp-vertices 1))
0 (* 2.0 pi)
(graphic-gstate ellipse))
(view-draw-arc view ; Yes, use draw-ellipse to draw it
(min-value-vector temp-vertices 0)
(min-value-vector temp-vertices 1)
(- (max-value-vector temp-vertices 0)(min-value-vector temp-vertices 0))
(- (max-value-vector temp-vertices 1)(min-value-vector temp-vertices 1))
0 (* 2.0 pi)
(edge-gstate ellipse)))
(with-vector scan-vector
(scan-ellipse (ellipse-origin-x ellipse)(ellipse-origin-y ellipse)
(ellipse-width ellipse)(ellipse-height ellipse) scan-vector
(view-pixel-size view))
(transform-point-seq world-transform scan-vector)
(view-draw-filled-polygon view ; No, use draw-polygon to draw it
scan-vector
(graphic-gstate ellipse))
(view-draw-polygon view
scan-vector
(edge-gstate ellipse)))
)
))
))
ellipse)
(DEFMETHOD graphic-contains-p ((ellipse ellipse) x y &optional (pixel 1))
(DECLARE (type wcoord x y) )
(when (point-in-extents-p ellipse x y pixel)
(LET ((world-transform (graphic-world-transform ellipse))
(line-width (OR (AND
(graphic-gstate ellipse)
(gstate-line-width ellipse) (+ (/ (gstate-line-width ellipse) 2) pixel)) pixel)))
(with-slots (vertices) ellipse
(with-vector temp-vertices
(copy-to-vector vertices temp-vertices)
(with-vector scan-vector
(scan-ellipse (ellipse-origin-x ellipse)(ellipse-origin-y ellipse)
(ellipse-width ellipse)(ellipse-height ellipse) scan-vector
pixel)
(transform-point-seq world-transform scan-vector)
(point-near-line scan-vector line-width x y)))))))
(DEFMETHOD graphic-contains-p ((ellipse filled-ellipse) x y &optional (pixel 1))
(DECLARE (type wcoord x y) )
(when (point-in-extents-p ellipse x y pixel)
(LET ((world-transform (graphic-world-transform ellipse)))
(with-slots (vertices) ellipse
(with-vector temp-vertices
(copy-to-vector vertices temp-vertices)
(with-vector scan-vector
(scan-ellipse (ellipse-origin-x ellipse)(ellipse-origin-y ellipse)
(ellipse-width ellipse)(ellipse-height ellipse) scan-vector
pixel)
(transform-point-seq world-transform scan-vector)
(WHEN
(inside-p scan-vector (make-point :x x :y y)) t )
))))))
(DEFMETHOD graphic-contains-p ((ellipse filled-ellipse-edge) x y &optional (pixel 1))
(DECLARE (type wcoord x y) )
(when (point-in-extents-p ellipse x y pixel)
(LET ((world-transform (graphic-world-transform ellipse))
(line-width (OR (AND
(graphic-gstate ellipse)
(gstate-line-width ellipse) (+ (/ (gstate-line-width ellipse) 2) pixel)) pixel)))
(with-slots (vertices) ellipse
(with-vector temp-vertices
(copy-to-vector vertices temp-vertices)
(with-vector scan-vector
(scan-ellipse (ellipse-origin-x ellipse)(ellipse-origin-y ellipse)
(ellipse-width ellipse)(ellipse-height ellipse) scan-vector
pixel)
(transform-point-seq world-transform scan-vector)
(WHEN (OR
(inside-p scan-vector (make-point :x x :y y))
(point-near-line scan-vector line-width x y) ) t )
))))))
(DEFMACRO ellipse-y-coordinate (x a2 b2)
"Determine a given Y value in an ellispe for X and the aspects A and B. A2 and B2 are A and B squared"
`(SQRT (- ,b2 (/ (* ,x ,x ,b2) (* ,a2)))))
(DEFUN scan-ellipse ( origin-x origin-y width height vector pixel)
"Contruct an ellipse from the ORIGIN-X, ORIGIN-Y, WIDTH, HEIGHT and store the new vertices in VECTOR"
(SETF (FILL-POINTER vector) 0)
(LET* ((a (- (/ width 2) ))
(a2 (* a a))
(b (/ height 2))
(b2 (* b b))
(smoothness (* pixel 1)))
(VECTOR-PUSH-EXTEND a vector 100)
(VECTOR-PUSH-EXTEND (ellipse-y-coordinate a a2 b2) vector 100)
(DO ((x (+ a (* pixel smoothness)) (+ x (* pixel (LET ((difference
(ABS (/ (- (ELT vector (- (FILL-POINTER vector) 1))
(ELT vector (- (FILL-POINTER vector) 3))) pixel))))
(COND
((< difference 1) 8)
((< difference 2) 6)
((< difference 3) 4)
((< difference 4) 2)
(t 1))))
)))
((> x 0))
(VECTOR-PUSH-EXTEND x vector 100)
(VECTOR-PUSH-EXTEND (ellipse-y-coordinate x a2 b2) vector 100)
))
(DO ((y (- (FILL-POINTER vector) 1) (- y 2)))
((< y 0))
(VECTOR-PUSH-EXTEND (- (ELT vector (- y 1))) vector 100)
(VECTOR-PUSH-EXTEND (ELT vector y) vector 100))
(DO ((y (- (FILL-POINTER vector) 1) (- y 2)))
((< y 0))
(VECTOR-PUSH-EXTEND (ELT vector (- y 1)) vector 100)
(VECTOR-PUSH-EXTEND (- (ELT vector y)) vector 100))
(DO ((y (- (FILL-POINTER vector) 1) (- y 2)))
((< y 0))
(SETF (ELT vector (- y 1)) (+ (ELT vector (- y 1)) origin-x (/ width 2)))
(SETF (ELT vector y) (+ (ELT vector y) origin-y (/ height 2))))
)