author | Claus Gittinger <cg@exept.de> |
Wed, 08 May 1996 14:46:09 +0200 | |
changeset 276 | e104a0359515 |
parent 267 | 1941121ff1c3 |
child 280 | 126dfc9dc409 |
permissions | -rw-r--r-- |
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 |
252 | 14 |
instanceVariableNames:'vertices' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Geometry' |
|
0 | 18 |
! |
19 |
||
135 | 20 |
!Polygon class methodsFor:'documentation'! |
31 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
68 | 25 |
All Rights Reserved |
0 | 26 |
|
31 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
Polygon - an array of points |
|
78 | 39 |
|
31 | 40 |
Adds simple boundary checking methods to Array. |
41 |
(needs much more - such as inside check, area computation etc.) |
|
255 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
267 | 45 |
|
46 |
[see also:] |
|
47 |
Point Polygon |
|
31 | 48 |
" |
49 |
! ! |
|
0 | 50 |
|
135 | 51 |
!Polygon class methodsFor:'instance creation'! |
52 |
||
53 |
fromRectangle:aRectangle |
|
54 |
^ self new vertices:(aRectangle asArrayOfPoints) |
|
55 |
||
56 |
" |
|
57 |
Polygon fromRectangle:(50@50 corner:100@100) |
|
58 |
" |
|
59 |
! |
|
78 | 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 |
" |
|
135 | 71 |
! ! |
72 |
||
73 |
!Polygon methodsFor:'accessing'! |
|
74 |
||
75 |
vertices |
|
76 |
"return the array containing my points" |
|
78 | 77 |
|
135 | 78 |
^ vertices |
79 |
! |
|
78 | 80 |
|
135 | 81 |
vertices:anArrayOfPoints |
82 |
"set the array containing my points" |
|
83 |
||
84 |
vertices := anArrayOfPoints |
|
78 | 85 |
! ! |
86 |
||
135 | 87 |
!Polygon methodsFor:'displaying'! |
78 | 88 |
|
135 | 89 |
displayFilledOn:aGC |
267 | 90 |
"display a filled polygin as represented by the receiver in |
91 |
the graphicsContext, aGC" |
|
92 |
||
135 | 93 |
aGC fillPolygon:vertices |
78 | 94 |
|
95 |
" |
|
135 | 96 |
|v| |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
97 |
v := View new openAndWait. |
135 | 98 |
|
99 |
(Polygon vertices:( |
|
267 | 100 |
Array |
101 |
with:10@10 |
|
102 |
with:60@10 |
|
103 |
with:35@60)) displayFilledOn:v |
|
135 | 104 |
|
105 |
|v| |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
106 |
v := View new openAndWait. |
78 | 107 |
|
135 | 108 |
(Polygon vertices:( |
267 | 109 |
Array |
110 |
with:10@10 |
|
111 |
with:60@10 |
|
112 |
with:35@60 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
113 |
with:10@10)) displayStrokedOn:v |
78 | 114 |
" |
267 | 115 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
116 |
"Modified: 8.5.1996 / 14:41:47 / cg" |
78 | 117 |
! |
118 |
||
135 | 119 |
displayStrokedOn:aGC |
267 | 120 |
"display an unfilled polygin as represented by the receiver in |
121 |
the graphicsContext, aGC" |
|
122 |
||
135 | 123 |
aGC displayPolygon:vertices |
124 |
||
125 |
" |
|
126 |
|v| |
|
127 |
||
128 |
v := View new open. |
|
129 |
[v shown] whileFalse:[Processor yield]. |
|
130 |
||
131 |
(Polygon vertices:( |
|
267 | 132 |
Array |
133 |
with:10@10 |
|
134 |
with:60@10 |
|
135 |
with:35@60)) displayStrokedOn:v |
|
135 | 136 |
|
137 |
|v| |
|
138 |
||
139 |
v := View new open. |
|
140 |
[v shown] whileFalse:[Processor yield]. |
|
141 |
||
142 |
(Polygon vertices:( |
|
267 | 143 |
Array |
144 |
with:10@10 |
|
145 |
with:60@10 |
|
146 |
with:35@60 |
|
147 |
with:10@10)) displayStrokedOn:v |
|
135 | 148 |
" |
149 |
||
267 | 150 |
"Modified: 27.4.1996 / 14:52:29 / cg" |
135 | 151 |
! ! |
152 |
||
153 |
!Polygon methodsFor:'enumerating'! |
|
154 |
||
78 | 155 |
edgesDo:aTwoArgBlock |
156 |
"evaluate aTwoArgBlock for each pair of vertices" |
|
157 |
||
158 |
1 to:vertices size-1 do:[:i | |
|
159 |
aTwoArgBlock value:(vertices at:i) value:(vertices at:i+1) |
|
160 |
]. |
|
161 |
||
162 |
" |
|
163 |
|v p| |
|
164 |
||
165 |
v := View new open. |
|
166 |
[v shown] whileFalse:[Processor yield]. |
|
167 |
||
168 |
p := Polygon vertices:(Array with:5@5 |
|
169 |
with:50@5 |
|
170 |
with:30@30 |
|
171 |
with:5@5). |
|
172 |
||
173 |
p displayOn:v. |
|
174 |
(Delay forSeconds:3) wait. |
|
175 |
||
176 |
p edgesDo:[:p1 :p2 | v lineWidth:3. v displayLineFrom:p1 to:p2] |
|
177 |
" |
|
178 |
! |
|
179 |
||
135 | 180 |
verticesDo:aBlock |
181 |
"evaluate aBlock for each point" |
|
182 |
||
183 |
vertices do:aBlock |
|
184 |
||
185 |
" |
|
186 |
|v p| |
|
187 |
||
188 |
v := View new open. |
|
189 |
[v shown] whileFalse:[Processor yield]. |
|
78 | 190 |
|
135 | 191 |
p := Polygon vertices:(Array with:5@5 |
192 |
with:50@5 |
|
193 |
with:30@30 |
|
194 |
with:5@5). |
|
195 |
||
196 |
p displayOn:v. |
|
197 |
(Delay forSeconds:3) wait. |
|
198 |
||
199 |
p verticesDo:[:p | v displayRectangleX:p x -3 y:p y -3 width:6 height:6] |
|
200 |
" |
|
78 | 201 |
! ! |
202 |
||
135 | 203 |
!Polygon methodsFor:'queries'! |
0 | 204 |
|
205 |
bottom |
|
14 | 206 |
"return the bottom boundary of the polygon, |
207 |
that is the maximum y coordinate of all its points" |
|
208 |
||
78 | 209 |
(vertices size == 0) ifTrue: [^ nil]. |
210 |
^ vertices inject:(vertices at:1) y into:[:maxSoFar :p | maxSoFar max:(p y)] |
|
0 | 211 |
|
78 | 212 |
" |
213 |
(Polygon vertices:( |
|
214 |
Array |
|
215 |
with:10@10 |
|
216 |
with:60@10 |
|
217 |
with:35@60)) bottom |
|
218 |
" |
|
0 | 219 |
! |
220 |
||
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
221 |
bounds |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
222 |
"return the smallest enclosing rectangle" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
223 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
224 |
|minX maxX minY maxY t n "{ Class: SmallInteger }" | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
225 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
226 |
n := vertices size. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
227 |
n == 0 ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
228 |
^ nil "/ mhmh - should we return an empty rectangle here ? |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
229 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
230 |
t := vertices at:1. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
231 |
minX := maxX := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
232 |
minY := maxY := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
233 |
2 to:n do:[:i | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
234 |
|x y t| |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
235 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
236 |
t := vertices at:i. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
237 |
x := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
238 |
y := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
239 |
x < minX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
240 |
minX := x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
241 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
242 |
x > maxX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
243 |
maxX := x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
244 |
] |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
245 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
246 |
y < minY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
247 |
minY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
248 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
249 |
y > maxY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
250 |
maxY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
251 |
] |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
252 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
253 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
254 |
^ Rectangle origin:(minX @ minY) corner:(maxX @ maxY). |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
255 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
256 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
257 |
|p| |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
258 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
259 |
p := (Polygon vertices:( |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
260 |
Array |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
261 |
with:10@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
262 |
with:60@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
263 |
with:35@60)). |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
264 |
p bounds |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
265 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
266 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
267 |
"Modified: 8.5.1996 / 14:06:35 / cg" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
268 |
! |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
269 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
270 |
canBeFilled |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
271 |
"return true, if the receiver can be drawn as a filled geometric. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
272 |
Always true here." |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
273 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
274 |
^ true |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
275 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
276 |
"Created: 8.5.1996 / 08:16:53 / cg" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
277 |
! |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
278 |
|
0 | 279 |
left |
14 | 280 |
"return the left boundary of the polygon, |
281 |
that is the minimum x coordinate of all its points" |
|
282 |
||
78 | 283 |
(vertices size == 0) ifTrue: [^ nil]. |
284 |
^ vertices inject:(vertices at:1) x into:[:minSoFar :p | minSoFar min:(p x)] |
|
0 | 285 |
|
78 | 286 |
" |
287 |
(Polygon vertices:( |
|
288 |
Array |
|
289 |
with:10@10 |
|
290 |
with:60@10 |
|
291 |
with:35@60)) left |
|
292 |
" |
|
293 |
||
0 | 294 |
! |
295 |
||
296 |
right |
|
14 | 297 |
"return the right boundary of the polygon, |
88 | 298 |
that is the maximum x coordinate of all its points" |
14 | 299 |
|
78 | 300 |
(vertices size == 0) ifTrue: [^ nil]. |
301 |
^ vertices inject:(vertices at:1) x into:[:maxSoFar :p | maxSoFar max:(p x)] |
|
302 |
||
303 |
" |
|
304 |
(Polygon vertices:( |
|
305 |
Array |
|
306 |
with:10@10 |
|
307 |
with:60@10 |
|
308 |
with:35@60)) right |
|
309 |
" |
|
310 |
||
135 | 311 |
! |
78 | 312 |
|
135 | 313 |
top |
314 |
"return the top boundary of the polygon, |
|
315 |
that is the minimum y coordinate of all its points" |
|
78 | 316 |
|
135 | 317 |
(vertices size == 0) ifTrue: [^ nil]. |
318 |
^ vertices inject:(vertices at:1) y into:[:minSoFar :p | minSoFar min:(p y)] |
|
78 | 319 |
|
320 |
" |
|
321 |
(Polygon vertices:( |
|
322 |
Array |
|
323 |
with:10@10 |
|
324 |
with:60@10 |
|
135 | 325 |
with:35@60)) top |
78 | 326 |
" |
327 |
||
135 | 328 |
! ! |
78 | 329 |
|
135 | 330 |
!Polygon class methodsFor:'documentation'! |
78 | 331 |
|
135 | 332 |
version |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
333 |
^ '$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.18 1996-05-08 12:46:09 cg Exp $' |
0 | 334 |
! ! |