author | Claus Gittinger <cg@exept.de> |
Wed, 08 May 1996 20:33:44 +0200 | |
changeset 281 | 344857a33c20 |
parent 280 | 126dfc9dc409 |
child 283 | 59a8cfbc1e5e |
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:] |
|
281 | 47 |
Rectangle EllipticalArc Spline Circle Point |
48 |
GraphicsContext |
|
280 | 49 |
" |
50 |
! |
|
51 |
||
52 |
examples |
|
53 |
" |
|
54 |
simple polygon; filled & unfilled: |
|
55 |
[exBegin] |
|
56 |
|v p| |
|
57 |
||
58 |
v := (View extent:200@200) openAndWait. |
|
59 |
||
60 |
p := Polygon vertices: |
|
61 |
(Array with:(10@10) |
|
62 |
with:(90@90) |
|
63 |
with:(10@90)). |
|
64 |
||
65 |
v scale:2. |
|
66 |
v paint:Color blue. |
|
67 |
p displayFilledOn:v. |
|
68 |
||
69 |
v paint:Color red. |
|
70 |
p displayStrokedOn:v. |
|
71 |
||
72 |
v scale:1; translation:100@0. |
|
73 |
v paint:Color green. |
|
74 |
p displayFilledOn:v. |
|
75 |
||
76 |
v paint:Color black. |
|
77 |
p displayStrokedOn:v. |
|
78 |
[exEnd] |
|
79 |
||
80 |
arbitrary polygon; filled & unfilled: |
|
81 |
[exBegin] |
|
82 |
|v p| |
|
83 |
||
84 |
v := (View extent:200@200) openAndWait. |
|
85 |
v scale:2. |
|
86 |
||
87 |
p := Polygon vertices: |
|
88 |
(Array with:(10@10) |
|
89 |
with:(90@90) |
|
90 |
with:(50@90) |
|
91 |
with:(90@10) |
|
92 |
with:(10@90) |
|
93 |
). |
|
94 |
||
95 |
v paint:Color blue. |
|
96 |
p displayFilledOn:v. |
|
97 |
||
98 |
v paint:Color red. |
|
99 |
p displayStrokedOn:v. |
|
100 |
[exEnd] |
|
31 | 101 |
" |
102 |
! ! |
|
0 | 103 |
|
135 | 104 |
!Polygon class methodsFor:'instance creation'! |
105 |
||
106 |
fromRectangle:aRectangle |
|
280 | 107 |
"return a new polygon, taking the rectangles vertices" |
108 |
||
109 |
^ self new vertices:(aRectangle asPointArray) |
|
135 | 110 |
|
111 |
" |
|
112 |
Polygon fromRectangle:(50@50 corner:100@100) |
|
113 |
" |
|
280 | 114 |
|
115 |
"Modified: 8.5.1996 / 20:15:18 / cg" |
|
135 | 116 |
! |
78 | 117 |
|
118 |
vertices:anArrayOfPoints |
|
280 | 119 |
"return a new polygon, given a collection of vertices" |
120 |
||
78 | 121 |
^ self new vertices:anArrayOfPoints |
122 |
||
123 |
" |
|
124 |
Polygon vertices:(Array with:10@10 |
|
280 | 125 |
with:20@20 |
126 |
with:30@30) |
|
78 | 127 |
|
128 |
Polygon vertices:(#(10 10 100 0 50 50) pairWiseCollect:[:x :y | x @ y]) |
|
129 |
" |
|
280 | 130 |
|
131 |
"Modified: 8.5.1996 / 20:11:31 / cg" |
|
135 | 132 |
! ! |
133 |
||
134 |
!Polygon methodsFor:'accessing'! |
|
135 |
||
136 |
vertices |
|
137 |
"return the array containing my points" |
|
78 | 138 |
|
135 | 139 |
^ vertices |
140 |
! |
|
78 | 141 |
|
135 | 142 |
vertices:anArrayOfPoints |
143 |
"set the array containing my points" |
|
144 |
||
145 |
vertices := anArrayOfPoints |
|
78 | 146 |
! ! |
147 |
||
135 | 148 |
!Polygon methodsFor:'displaying'! |
78 | 149 |
|
135 | 150 |
displayFilledOn:aGC |
267 | 151 |
"display a filled polygin as represented by the receiver in |
152 |
the graphicsContext, aGC" |
|
153 |
||
135 | 154 |
aGC fillPolygon:vertices |
78 | 155 |
|
156 |
" |
|
135 | 157 |
|v| |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
158 |
v := View new openAndWait. |
135 | 159 |
|
160 |
(Polygon vertices:( |
|
267 | 161 |
Array |
162 |
with:10@10 |
|
163 |
with:60@10 |
|
164 |
with:35@60)) displayFilledOn:v |
|
135 | 165 |
|
166 |
|v| |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
167 |
v := View new openAndWait. |
78 | 168 |
|
135 | 169 |
(Polygon vertices:( |
267 | 170 |
Array |
171 |
with:10@10 |
|
172 |
with:60@10 |
|
173 |
with:35@60 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
174 |
with:10@10)) displayStrokedOn:v |
78 | 175 |
" |
267 | 176 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
177 |
"Modified: 8.5.1996 / 14:41:47 / cg" |
78 | 178 |
! |
179 |
||
135 | 180 |
displayStrokedOn:aGC |
267 | 181 |
"display an unfilled polygin as represented by the receiver in |
182 |
the graphicsContext, aGC" |
|
183 |
||
135 | 184 |
aGC displayPolygon:vertices |
185 |
||
186 |
" |
|
187 |
|v| |
|
188 |
||
189 |
v := View new open. |
|
190 |
[v shown] whileFalse:[Processor yield]. |
|
191 |
||
192 |
(Polygon vertices:( |
|
267 | 193 |
Array |
194 |
with:10@10 |
|
195 |
with:60@10 |
|
196 |
with:35@60)) displayStrokedOn:v |
|
135 | 197 |
|
198 |
|v| |
|
199 |
||
200 |
v := View new open. |
|
201 |
[v shown] whileFalse:[Processor yield]. |
|
202 |
||
203 |
(Polygon vertices:( |
|
267 | 204 |
Array |
205 |
with:10@10 |
|
206 |
with:60@10 |
|
207 |
with:35@60 |
|
208 |
with:10@10)) displayStrokedOn:v |
|
135 | 209 |
" |
210 |
||
267 | 211 |
"Modified: 27.4.1996 / 14:52:29 / cg" |
135 | 212 |
! ! |
213 |
||
214 |
!Polygon methodsFor:'enumerating'! |
|
215 |
||
78 | 216 |
edgesDo:aTwoArgBlock |
217 |
"evaluate aTwoArgBlock for each pair of vertices" |
|
218 |
||
219 |
1 to:vertices size-1 do:[:i | |
|
220 |
aTwoArgBlock value:(vertices at:i) value:(vertices at:i+1) |
|
221 |
]. |
|
222 |
||
223 |
" |
|
224 |
|v p| |
|
225 |
||
226 |
v := View new open. |
|
227 |
[v shown] whileFalse:[Processor yield]. |
|
228 |
||
229 |
p := Polygon vertices:(Array with:5@5 |
|
230 |
with:50@5 |
|
231 |
with:30@30 |
|
232 |
with:5@5). |
|
233 |
||
234 |
p displayOn:v. |
|
235 |
(Delay forSeconds:3) wait. |
|
236 |
||
237 |
p edgesDo:[:p1 :p2 | v lineWidth:3. v displayLineFrom:p1 to:p2] |
|
238 |
" |
|
239 |
! |
|
240 |
||
135 | 241 |
verticesDo:aBlock |
242 |
"evaluate aBlock for each point" |
|
243 |
||
244 |
vertices do:aBlock |
|
245 |
||
246 |
" |
|
247 |
|v p| |
|
248 |
||
249 |
v := View new open. |
|
250 |
[v shown] whileFalse:[Processor yield]. |
|
78 | 251 |
|
135 | 252 |
p := Polygon vertices:(Array with:5@5 |
253 |
with:50@5 |
|
254 |
with:30@30 |
|
255 |
with:5@5). |
|
256 |
||
257 |
p displayOn:v. |
|
258 |
(Delay forSeconds:3) wait. |
|
259 |
||
260 |
p verticesDo:[:p | v displayRectangleX:p x -3 y:p y -3 width:6 height:6] |
|
261 |
" |
|
78 | 262 |
! ! |
263 |
||
135 | 264 |
!Polygon methodsFor:'queries'! |
0 | 265 |
|
266 |
bottom |
|
14 | 267 |
"return the bottom boundary of the polygon, |
268 |
that is the maximum y coordinate of all its points" |
|
269 |
||
78 | 270 |
(vertices size == 0) ifTrue: [^ nil]. |
271 |
^ vertices inject:(vertices at:1) y into:[:maxSoFar :p | maxSoFar max:(p y)] |
|
0 | 272 |
|
78 | 273 |
" |
274 |
(Polygon vertices:( |
|
275 |
Array |
|
276 |
with:10@10 |
|
277 |
with:60@10 |
|
278 |
with:35@60)) bottom |
|
279 |
" |
|
0 | 280 |
! |
281 |
||
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
282 |
bounds |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
283 |
"return the smallest enclosing rectangle" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
284 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
285 |
|minX maxX minY maxY t n "{ Class: SmallInteger }" | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
286 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
287 |
n := vertices size. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
288 |
n == 0 ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
289 |
^ nil "/ mhmh - should we return an empty rectangle here ? |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
290 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
291 |
t := vertices at:1. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
292 |
minX := maxX := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
293 |
minY := maxY := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
294 |
2 to:n do:[:i | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
295 |
|x y t| |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
296 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
297 |
t := vertices at:i. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
298 |
x := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
299 |
y := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
300 |
x < minX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
301 |
minX := x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
302 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
303 |
x > maxX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
304 |
maxX := x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
305 |
] |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
306 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
307 |
y < minY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
308 |
minY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
309 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
310 |
y > maxY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
311 |
maxY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
312 |
] |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
313 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
314 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
315 |
^ Rectangle origin:(minX @ minY) corner:(maxX @ maxY). |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
316 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
317 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
318 |
|p| |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
319 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
320 |
p := (Polygon vertices:( |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
321 |
Array |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
322 |
with:10@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
323 |
with:60@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
324 |
with:35@60)). |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
325 |
p bounds |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
326 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
327 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
328 |
"Modified: 8.5.1996 / 14:06:35 / cg" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
329 |
! |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
330 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
331 |
canBeFilled |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
332 |
"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
|
333 |
Always true here." |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
334 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
335 |
^ true |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
336 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
337 |
"Created: 8.5.1996 / 08:16:53 / cg" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
338 |
! |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
339 |
|
0 | 340 |
left |
14 | 341 |
"return the left boundary of the polygon, |
342 |
that is the minimum x coordinate of all its points" |
|
343 |
||
78 | 344 |
(vertices size == 0) ifTrue: [^ nil]. |
345 |
^ vertices inject:(vertices at:1) x into:[:minSoFar :p | minSoFar min:(p x)] |
|
0 | 346 |
|
78 | 347 |
" |
348 |
(Polygon vertices:( |
|
349 |
Array |
|
350 |
with:10@10 |
|
351 |
with:60@10 |
|
352 |
with:35@60)) left |
|
353 |
" |
|
354 |
||
0 | 355 |
! |
356 |
||
357 |
right |
|
14 | 358 |
"return the right boundary of the polygon, |
88 | 359 |
that is the maximum x coordinate of all its points" |
14 | 360 |
|
78 | 361 |
(vertices size == 0) ifTrue: [^ nil]. |
362 |
^ vertices inject:(vertices at:1) x into:[:maxSoFar :p | maxSoFar max:(p x)] |
|
363 |
||
364 |
" |
|
365 |
(Polygon vertices:( |
|
366 |
Array |
|
367 |
with:10@10 |
|
368 |
with:60@10 |
|
369 |
with:35@60)) right |
|
370 |
" |
|
371 |
||
135 | 372 |
! |
78 | 373 |
|
135 | 374 |
top |
375 |
"return the top boundary of the polygon, |
|
376 |
that is the minimum y coordinate of all its points" |
|
78 | 377 |
|
135 | 378 |
(vertices size == 0) ifTrue: [^ nil]. |
379 |
^ vertices inject:(vertices at:1) y into:[:minSoFar :p | minSoFar min:(p y)] |
|
78 | 380 |
|
381 |
" |
|
382 |
(Polygon vertices:( |
|
383 |
Array |
|
384 |
with:10@10 |
|
385 |
with:60@10 |
|
135 | 386 |
with:35@60)) top |
78 | 387 |
" |
388 |
||
135 | 389 |
! ! |
78 | 390 |
|
135 | 391 |
!Polygon class methodsFor:'documentation'! |
78 | 392 |
|
135 | 393 |
version |
281 | 394 |
^ '$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.20 1996-05-08 18:33:44 cg Exp $' |
0 | 395 |
! ! |