-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathline.lisp
More file actions
280 lines (218 loc) · 9.69 KB
/
line.lisp
File metadata and controls
280 lines (218 loc) · 9.69 KB
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
;;;-*- 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)
;Line Class Definition:
(defclass line (extent-cache graphic)
(
(start-x :type wcoord
:initarg :start-x
:accessor line-start-point-x
:documentation "object x-coordinate of start point for the line")
(start-y :type wcoord
:initarg :start-y
:accessor line-start-point-y
:documentation "object y-coordinate of start point for the line")
(end-x :type wcoord
:initarg :end-x
:accessor line-end-point-x
:documentation "object x-coordinate of end point for the line")
(end-y :type wcoord
:initarg :end-y
:accessor line-end-point-y
:documentation "object y-coordinate of end point for the line")
)
(:documentation "A graphic that represents a line"))
;Function: make-line
; Return a new line object whose end points are (START-X, START-Y) and
; (END-X, END-Y).
(defun make-line (start-x start-y end-x end-y &rest options)
;; (declare (type wcoord start-x start-y end-x end-y))
(apply #'make-instance 'line
:start-x start-x
:start-y start-y
:end-x end-x
:end-y end-y
options))
;; Method: line-start-point
;; Return the coordinates of the START-POINT of the given LINE.
(defmethod line-start-point ((line line))
(with-slots (start-x start-y) line
(values start-x start-y)))
(defmethod (setf line-start-point-x) :after (start-x (line line))
(declare (type wcoord start-x))
(declare (ignore start-x))
(extent-changed line))
;Method: line-start-point-y
; Return or change the x coordinate of the START-POINT of the given LINE.
(defmethod (setf line-start-point-y) :after (start-y (line line))
(declare (type wcoord start-y))
(declare (ignore start-y))
(extent-changed line))
;Method: line-end-point
; Return the coordinates of the END-POINT of the given LINE.
(defmethod line-end-point ((line line))
(with-slots (end-x end-y) line
(values end-x end-y)))
;Method: line-end-point-x
; Return or change the x coordinate of the END-POINT of the given LINE.
(defmethod (setf line-end-point-x) :after (end-x (line line))
(declare (type wcoord end-x))
(declare (IGNORE end-x))
(extent-changed line))
;Method: line-end-point-y
; Return or change the x coordinate of the END-POINT of the given LINE.
(defmethod (setf line-end-point-y) :after (end-y (line line))
(declare (type wcoord end-y))
(declare (IGNORE end-y))
(extent-changed line))
; Graphic methods for line graphics
;Method: extent-compute
; Compute the extent rectangle for the LINE.
; Note: A graphic's extent rectangle is defined in the object coordinate system. This
; means that each graphic should apply its own transform to its computed extent
; before returning it.
(defmethod extent-compute ((line line))
(with-slots (start-x start-y end-x end-y transform) line
(let (new-start-x new-start-y new-end-x new-end-y)
(multiple-value-setq (new-start-x new-start-y)
(transform-point transform start-x start-y))
(multiple-value-setq (new-end-x new-end-y)
(transform-point transform end-x end-y))
(make-extent-rect
:xmin (min new-start-x new-end-x)
:ymin (min new-start-y new-end-y)
:xmax (max new-start-x new-end-x)
:ymax (max new-start-y new-end-y)))))
;Method: draw-graphic
; Draw the LINE object in the given VIEW. If MIN-X, MIN-Y, WIDTH, and HEIGHT
; are given, then only parts of the object that lie within the given rectangle
; need to be drawn.
(DEFMACRO line-visible-p (graphic)
`(AND (NOT (AND (and min-x min-y width height) ; Was optional rect given
(not (graphic-within-p ,graphic min-x min-y width height))
(not (graphic-intersects-p ,graphic min-x min-y width height))))
(PROGN
(UNLESS (valid-extent-p (graphic-extent ,graphic)))
(OR (>= (/ (- (extent-rect-xmax extent) (extent-rect-xmin extent))(view-scale view) ) (view-pixel-size view))
(= (/ (- (extent-rect-xmax extent) (extent-rect-xmin extent)) (view-scale view) ) 0))
(OR
(>= (/ (- (extent-rect-ymax extent) (extent-rect-ymin extent))(view-scale view) ) (view-pixel-size view))
(= (/ (- (extent-rect-ymax extent) (extent-rect-ymin extent)) (view-scale view) ) 0))
)
(viewable-p ,graphic)))
(defmethod draw-graphic ((line line) (view view)
&optional min-x min-y width height)
(declare (type (or null wcoord) min-x min-y width height))
(with-slots (gstate start-x start-y end-x end-y extent) line
(WHEN (line-visible-p line)
(MULTIPLE-VALUE-BIND (x y) (transform-point (graphic-world-transform line) start-x start-y)
(MULTIPLE-VALUE-BIND (x1 y1) (transform-point (graphic-world-transform line) end-x end-y)
(view-draw-line view x y x1 y1
(graphic-gstate line)))))))
(defmethod draw-graphic-clipped ((graphic line) (view view)
min-x min-y width height)
(declare (type wcoord min-x min-y width height))
(with-slots (extent) graphic
(WHEN (line-visible-p graphic)
(SETF (gstate-clip-mask graphic) (clip-mask graphic view min-x min-y width height))
(draw-graphic graphic view)
(SETF (gstate-clip-mask graphic) :none))
))
(DEFMETHOD graphic-damage ((graphic line))
"Records graphic as a damaged region in each view to which it is attached."
(with-slots ( gstate) graphic
(LET ((extent (world-extent graphic)))
(WHEN (valid-extent-p extent)
(UNLESS (extent-valid-p graphic) (graphic-extent graphic))
(LET* ((xmin (extent-rect-xmin extent))
(ymin (extent-rect-ymin extent)))
(do ((g graphic (graphic-parent g)))
((null g))
(dolist (view (graphic-views g))
(LET
((width (COND ((AND (NOT (AND gstate (gstate-line-width graphic)))
(= (- (extent-rect-xmax extent) (extent-rect-xmin extent)) 0))
(view-pixel-size view))
((AND (NOT (AND gstate (gstate-line-width graphic)))
(< (- (extent-rect-xmax extent) (extent-rect-xmin extent)) (view-pixel-size view)))
(view-pixel-size view))
(t (- (extent-rect-xmax extent) (extent-rect-xmin extent)))))
(height (COND ((AND (NOT (AND gstate (gstate-line-width graphic)))
(= (- (extent-rect-ymax extent) (extent-rect-ymin extent)) 0))
(view-pixel-size view))
((AND (NOT (AND gstate (gstate-line-width graphic)))
(< (- (extent-rect-ymax extent) (extent-rect-ymin extent))(view-pixel-size view)))
(view-pixel-size view))
(t (- (extent-rect-ymax extent) (extent-rect-ymin extent))))))
(view-damage view xmin ymin width height)))))))))
(defmethod scale-transform ((graphic line) scale-x scale-y
&optional (fixed-x 0) (fixed-y 0))
(DECLARE (IGNORE fixed-x fixed-y))
(declare (type (or (satisfies plusp) (satisfies zerop)) scale-x scale-y))
(declare (type ocoord fixed-x fixed-y))
(graphic-damage graphic) ; Damage from old graphic
(with-slots (transform start-x start-y end-x end-y) graphic
(COND
((AND
(OR (AND (= start-x end-x)(NOT (= scale-x 1)))(AND (= start-y end-y)(NOT (= scale-y 1))))
(NOT transform)) nil)
( t (call-next-method)))
(extent-changed graphic) ; Notify graphic his extent may have changed
(graphic-damage graphic) ; Damage from new graphic
transform))
;Method: normalize-graphic
; Normalize the LINE by applying its transform to its geometry, changing it
; accordingly, and then setting its transform to nil (the identity transform).
; Nothing of value is returned.
(defmethod normalize-graphic :before ((line line))
(with-slots (start-x start-y end-x end-y transform) line
(multiple-value-setq (start-x start-y)
(transform-point transform start-x start-y))
(multiple-value-setq (end-x end-y)
(transform-point transform end-x end-y))))
;;This method is for determining if a pick is on a line
(DEFMETHOD graphic-contains-p ((line line) x y &optional pixel)
(DECLARE (type wcoord x y) )
(SETF pixel (/ (OR (AND (graphic-gstate line) (gstate-line-width line)) pixel 2) 2))
(with-slots (start-x start-y end-x end-y transform) line
(MULTIPLE-VALUE-BIND (sx sy)(transform-point (graphic-world-transform line) start-x start-y)
(MULTIPLE-VALUE-BIND (ex ey) (transform-point (graphic-world-transform line) end-x end-y)
(point-on-line-p pixel x y sx sy ex ey)
)))
)
;function for determining if a point is on a line
;no consideration is given to make sure the point is within the endpoints, this check is done by the
;graphic in the method: graphic-contains-p
(defun point-on-line-p ( aperture x y x1 y1 x2 y2)
(SETF aperture (* 2 aperture))
(COND
( (= (FLOOR x1) (FLOOR x2)) (AND
(<= (ABS (- x x1)) aperture)
(>= y (MIN y1 y2))
(<= y (MAX y1 y2)))) ;perpendicular line with infinite slope
( (= (FLOOR y1)(FLOOR y2)) (AND
(<= (ABS (- y y1)) aperture)
(>= x (MIN x1 x2))
(<= x (MAX x1 x2))) ) ;horizontal line
(t (<= (MIN (ABS (+ (/ (* (- y2 y1)(- x x1)) (- x2 x1)) y1 (- y)));pp 227-228 Computer Graphics - Harrington
(ABS (+ (/ (* (- x2 x1)(- y y1)) (- y2 y1)) x1 (- x)))) aperture) )
)
)