0
|
1 |
"
|
4
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
68
|
3 |
All Rights Reserved
|
0
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
78
|
13 |
Geometric subclass:#Polygon
|
|
14 |
instanceVariableNames:'vertices'
|
0
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
68
|
17 |
category:'Graphics-Geometry'
|
0
|
18 |
!
|
|
19 |
|
|
20 |
Polygon comment:'
|
31
|
21 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
68
|
22 |
All Rights Reserved
|
36
|
23 |
|
84
|
24 |
$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.10 1995-08-10 13:18:21 claus Exp $
|
31
|
25 |
'!
|
|
26 |
|
|
27 |
!Polygon class methodsFor: 'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
68
|
32 |
All Rights Reserved
|
0
|
33 |
|
31
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
84
|
45 |
$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.10 1995-08-10 13:18:21 claus Exp $
|
|
46 |
$Revision: 1.10 $
|
31
|
47 |
"
|
|
48 |
!
|
|
49 |
|
|
50 |
documentation
|
|
51 |
"
|
|
52 |
Polygon - an array of points
|
78
|
53 |
|
31
|
54 |
Adds simple boundary checking methods to Array.
|
|
55 |
(needs much more - such as inside check, area computation etc.)
|
|
56 |
"
|
|
57 |
! !
|
0
|
58 |
|
78
|
59 |
!Polygon class methodsFor: 'instance creation'!
|
|
60 |
|
|
61 |
vertices:anArrayOfPoints
|
|
62 |
^ self new vertices:anArrayOfPoints
|
|
63 |
|
|
64 |
"
|
|
65 |
Polygon vertices:(Array with:10@10
|
|
66 |
with:20@20
|
|
67 |
with:30@30)
|
|
68 |
|
|
69 |
Polygon vertices:(#(10 10 100 0 50 50) pairWiseCollect:[:x :y | x @ y])
|
|
70 |
"
|
|
71 |
!
|
|
72 |
|
|
73 |
fromRectangle:aRectangle
|
|
74 |
^ self new vertices:(aRectangle asArrayOfPoints)
|
|
75 |
|
|
76 |
"
|
|
77 |
Polygon fromRectangle:(50@50 corner:100@100)
|
|
78 |
"
|
|
79 |
! !
|
|
80 |
|
|
81 |
!Polygon methodsFor: 'enumerating'!
|
|
82 |
|
|
83 |
verticesDo:aBlock
|
|
84 |
"evaluate aBlock for each point"
|
|
85 |
|
|
86 |
vertices do:aBlock
|
|
87 |
|
|
88 |
"
|
|
89 |
|v p|
|
|
90 |
|
|
91 |
v := View new open.
|
|
92 |
[v shown] whileFalse:[Processor yield].
|
|
93 |
|
|
94 |
p := Polygon vertices:(Array with:5@5
|
|
95 |
with:50@5
|
|
96 |
with:30@30
|
|
97 |
with:5@5).
|
|
98 |
|
|
99 |
p displayOn:v.
|
|
100 |
(Delay forSeconds:3) wait.
|
|
101 |
|
|
102 |
p verticesDo:[:p | v displayRectangleX:p x -3 y:p y -3 width:6 height:6]
|
|
103 |
"
|
|
104 |
!
|
|
105 |
|
|
106 |
edgesDo:aTwoArgBlock
|
|
107 |
"evaluate aTwoArgBlock for each pair of vertices"
|
|
108 |
|
|
109 |
1 to:vertices size-1 do:[:i |
|
|
110 |
aTwoArgBlock value:(vertices at:i) value:(vertices at:i+1)
|
|
111 |
].
|
|
112 |
|
|
113 |
"
|
|
114 |
|v p|
|
|
115 |
|
|
116 |
v := View new open.
|
|
117 |
[v shown] whileFalse:[Processor yield].
|
|
118 |
|
|
119 |
p := Polygon vertices:(Array with:5@5
|
|
120 |
with:50@5
|
|
121 |
with:30@30
|
|
122 |
with:5@5).
|
|
123 |
|
|
124 |
p displayOn:v.
|
|
125 |
(Delay forSeconds:3) wait.
|
|
126 |
|
|
127 |
p edgesDo:[:p1 :p2 | v lineWidth:3. v displayLineFrom:p1 to:p2]
|
|
128 |
"
|
|
129 |
! !
|
|
130 |
|
0
|
131 |
!Polygon methodsFor: 'accessing'!
|
|
132 |
|
78
|
133 |
vertices
|
|
134 |
"return the array containing my points"
|
|
135 |
|
|
136 |
^ vertices
|
|
137 |
!
|
|
138 |
|
|
139 |
vertices:anArrayOfPoints
|
|
140 |
"set the array containing my points"
|
|
141 |
|
|
142 |
vertices := anArrayOfPoints
|
|
143 |
! !
|
|
144 |
|
|
145 |
!Polygon methodsFor: 'queries'!
|
|
146 |
|
0
|
147 |
top
|
14
|
148 |
"return the top boundary of the polygon,
|
|
149 |
that is the minimum y coordinate of all its points"
|
|
150 |
|
78
|
151 |
(vertices size == 0) ifTrue: [^ nil].
|
|
152 |
^ vertices inject:(vertices at:1) y into:[:minSoFar :p | minSoFar min:(p y)]
|
0
|
153 |
|
78
|
154 |
"
|
|
155 |
(Polygon vertices:(
|
|
156 |
Array
|
|
157 |
with:10@10
|
|
158 |
with:60@10
|
|
159 |
with:35@60)) top
|
|
160 |
"
|
|
161 |
|
0
|
162 |
!
|
|
163 |
|
|
164 |
bottom
|
14
|
165 |
"return the bottom boundary of the polygon,
|
|
166 |
that is the maximum y coordinate of all its points"
|
|
167 |
|
78
|
168 |
(vertices size == 0) ifTrue: [^ nil].
|
|
169 |
^ vertices inject:(vertices at:1) y into:[:maxSoFar :p | maxSoFar max:(p y)]
|
0
|
170 |
|
78
|
171 |
"
|
|
172 |
(Polygon vertices:(
|
|
173 |
Array
|
|
174 |
with:10@10
|
|
175 |
with:60@10
|
|
176 |
with:35@60)) bottom
|
|
177 |
"
|
0
|
178 |
!
|
|
179 |
|
|
180 |
left
|
14
|
181 |
"return the left boundary of the polygon,
|
|
182 |
that is the minimum x coordinate of all its points"
|
|
183 |
|
78
|
184 |
(vertices size == 0) ifTrue: [^ nil].
|
|
185 |
^ vertices inject:(vertices at:1) x into:[:minSoFar :p | minSoFar min:(p x)]
|
0
|
186 |
|
78
|
187 |
"
|
|
188 |
(Polygon vertices:(
|
|
189 |
Array
|
|
190 |
with:10@10
|
|
191 |
with:60@10
|
|
192 |
with:35@60)) left
|
|
193 |
"
|
|
194 |
|
0
|
195 |
!
|
|
196 |
|
|
197 |
right
|
14
|
198 |
"return the right boundary of the polygon,
|
|
199 |
that is the maximum y coordinate of all its points"
|
|
200 |
|
78
|
201 |
(vertices size == 0) ifTrue: [^ nil].
|
|
202 |
^ vertices inject:(vertices at:1) x into:[:maxSoFar :p | maxSoFar max:(p x)]
|
|
203 |
|
|
204 |
"
|
|
205 |
(Polygon vertices:(
|
|
206 |
Array
|
|
207 |
with:10@10
|
|
208 |
with:60@10
|
|
209 |
with:35@60)) right
|
|
210 |
"
|
|
211 |
|
|
212 |
! !
|
|
213 |
|
|
214 |
!Polygon methodsFor:'displaying'!
|
|
215 |
|
|
216 |
displayStrokedOn:aGC
|
|
217 |
aGC displayPolygon:vertices
|
|
218 |
|
|
219 |
"
|
|
220 |
|v|
|
|
221 |
|
|
222 |
v := View new open.
|
|
223 |
[v shown] whileFalse:[Processor yield].
|
|
224 |
|
|
225 |
(Polygon vertices:(
|
|
226 |
Array
|
|
227 |
with:10@10
|
|
228 |
with:60@10
|
|
229 |
with:35@60)) displayStrokedOn:v
|
|
230 |
|
|
231 |
|v|
|
|
232 |
|
|
233 |
v := View new open.
|
|
234 |
[v shown] whileFalse:[Processor yield].
|
0
|
235 |
|
78
|
236 |
(Polygon vertices:(
|
|
237 |
Array
|
|
238 |
with:10@10
|
|
239 |
with:60@10
|
|
240 |
with:35@60
|
|
241 |
with:10@10)) displayStrokedOn:v
|
|
242 |
"
|
|
243 |
|
|
244 |
!
|
|
245 |
|
|
246 |
displayFilledOn:aGC
|
|
247 |
aGC fillPolygon:vertices
|
|
248 |
|
|
249 |
"
|
|
250 |
|v|
|
|
251 |
|
|
252 |
v := View new open.
|
|
253 |
[v shown] whileFalse:[Processor yield].
|
|
254 |
|
|
255 |
(Polygon vertices:(
|
|
256 |
Array
|
|
257 |
with:10@10
|
|
258 |
with:60@10
|
|
259 |
with:35@60)) displayFilledOn:v
|
|
260 |
|
|
261 |
|v|
|
|
262 |
|
|
263 |
v := View new open.
|
|
264 |
[v shown] whileFalse:[Processor yield].
|
|
265 |
|
|
266 |
(Polygon vertices:(
|
|
267 |
Array
|
|
268 |
with:10@10
|
|
269 |
with:60@10
|
|
270 |
with:35@60
|
|
271 |
with:10@10)) displayFilledOn:v
|
|
272 |
"
|
0
|
273 |
! !
|