author | Claus Gittinger <cg@exept.de> |
Fri, 29 Aug 2003 19:32:22 +0200 | |
changeset 1303 | 6ca5f36cdc63 |
parent 490 | ef43c7a69346 |
child 1430 | baf6bfed6d56 |
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 |
||
1303 | 13 |
"{ Package: 'stx:libbasic2' }" |
14 |
||
78 | 15 |
Geometric subclass:#Polygon |
252 | 16 |
instanceVariableNames:'vertices' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
1303 | 19 |
category:'Graphics-Geometry-Objects' |
0 | 20 |
! |
21 |
||
135 | 22 |
!Polygon class methodsFor:'documentation'! |
31 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
68 | 27 |
All Rights Reserved |
0 | 28 |
|
31 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
Polygon - an array of points |
|
78 | 41 |
|
31 | 42 |
Adds simple boundary checking methods to Array. |
43 |
(needs much more - such as inside check, area computation etc.) |
|
255 | 44 |
|
45 |
[author:] |
|
46 |
Claus Gittinger |
|
267 | 47 |
|
48 |
[see also:] |
|
331 | 49 |
Rectangle EllipticalArc Spline Circle Point LineSegment Curve |
50 |
Arrow ArroedSpline |
|
51 |
GraphicsContext StrokingWrapper FillingWrapper |
|
280 | 52 |
" |
53 |
! |
|
54 |
||
55 |
examples |
|
56 |
" |
|
57 |
simple polygon; filled & unfilled: |
|
58 |
[exBegin] |
|
59 |
|v p| |
|
60 |
||
61 |
v := (View extent:200@200) openAndWait. |
|
62 |
||
63 |
p := Polygon vertices: |
|
64 |
(Array with:(10@10) |
|
65 |
with:(90@90) |
|
66 |
with:(10@90)). |
|
67 |
||
68 |
v scale:2. |
|
69 |
v paint:Color blue. |
|
70 |
p displayFilledOn:v. |
|
71 |
||
72 |
v paint:Color red. |
|
73 |
p displayStrokedOn:v. |
|
74 |
||
75 |
v scale:1; translation:100@0. |
|
76 |
v paint:Color green. |
|
77 |
p displayFilledOn:v. |
|
78 |
||
79 |
v paint:Color black. |
|
80 |
p displayStrokedOn:v. |
|
81 |
[exEnd] |
|
82 |
||
83 |
arbitrary polygon; filled & unfilled: |
|
84 |
[exBegin] |
|
85 |
|v p| |
|
86 |
||
87 |
v := (View extent:200@200) openAndWait. |
|
88 |
v scale:2. |
|
89 |
||
90 |
p := Polygon vertices: |
|
91 |
(Array with:(10@10) |
|
92 |
with:(90@90) |
|
93 |
with:(50@90) |
|
94 |
with:(90@10) |
|
95 |
with:(10@90) |
|
96 |
). |
|
97 |
||
98 |
v paint:Color blue. |
|
99 |
p displayFilledOn:v. |
|
100 |
||
101 |
v paint:Color red. |
|
102 |
p displayStrokedOn:v. |
|
103 |
[exEnd] |
|
31 | 104 |
" |
105 |
! ! |
|
0 | 106 |
|
135 | 107 |
!Polygon class methodsFor:'instance creation'! |
108 |
||
109 |
fromRectangle:aRectangle |
|
280 | 110 |
"return a new polygon, taking the rectangles vertices" |
111 |
||
112 |
^ self new vertices:(aRectangle asPointArray) |
|
135 | 113 |
|
114 |
" |
|
115 |
Polygon fromRectangle:(50@50 corner:100@100) |
|
116 |
" |
|
280 | 117 |
|
118 |
"Modified: 8.5.1996 / 20:15:18 / cg" |
|
135 | 119 |
! |
78 | 120 |
|
121 |
vertices:anArrayOfPoints |
|
280 | 122 |
"return a new polygon, given a collection of vertices" |
123 |
||
78 | 124 |
^ self new vertices:anArrayOfPoints |
125 |
||
126 |
" |
|
127 |
Polygon vertices:(Array with:10@10 |
|
280 | 128 |
with:20@20 |
129 |
with:30@30) |
|
78 | 130 |
|
131 |
Polygon vertices:(#(10 10 100 0 50 50) pairWiseCollect:[:x :y | x @ y]) |
|
132 |
" |
|
280 | 133 |
|
134 |
"Modified: 8.5.1996 / 20:11:31 / cg" |
|
135 | 135 |
! ! |
136 |
||
137 |
!Polygon methodsFor:'accessing'! |
|
138 |
||
139 |
vertices |
|
140 |
"return the array containing my points" |
|
78 | 141 |
|
135 | 142 |
^ vertices |
143 |
! |
|
78 | 144 |
|
135 | 145 |
vertices:anArrayOfPoints |
146 |
"set the array containing my points" |
|
147 |
||
148 |
vertices := anArrayOfPoints |
|
78 | 149 |
! ! |
150 |
||
283 | 151 |
!Polygon methodsFor:'converting'! |
152 |
||
153 |
asPointArray |
|
154 |
"return an array containing my vertex points. |
|
155 |
Notice, that no copy of my vertices is created - you should not |
|
156 |
modify the returned collections points (unless you want to affect |
|
157 |
the polygon ...)." |
|
158 |
||
159 |
^ vertices |
|
160 |
||
161 |
"Modified: 8.5.1996 / 20:43:39 / cg" |
|
162 |
! ! |
|
163 |
||
135 | 164 |
!Polygon methodsFor:'displaying'! |
78 | 165 |
|
135 | 166 |
displayFilledOn:aGC |
267 | 167 |
"display a filled polygin as represented by the receiver in |
168 |
the graphicsContext, aGC" |
|
169 |
||
135 | 170 |
aGC fillPolygon:vertices |
78 | 171 |
|
172 |
" |
|
135 | 173 |
|v| |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
174 |
v := View new openAndWait. |
135 | 175 |
|
176 |
(Polygon vertices:( |
|
267 | 177 |
Array |
178 |
with:10@10 |
|
179 |
with:60@10 |
|
180 |
with:35@60)) displayFilledOn:v |
|
135 | 181 |
|
182 |
|v| |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
183 |
v := View new openAndWait. |
78 | 184 |
|
135 | 185 |
(Polygon vertices:( |
267 | 186 |
Array |
187 |
with:10@10 |
|
188 |
with:60@10 |
|
189 |
with:35@60 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
190 |
with:10@10)) displayStrokedOn:v |
78 | 191 |
" |
267 | 192 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
193 |
"Modified: 8.5.1996 / 14:41:47 / cg" |
78 | 194 |
! |
195 |
||
135 | 196 |
displayStrokedOn:aGC |
267 | 197 |
"display an unfilled polygin as represented by the receiver in |
198 |
the graphicsContext, aGC" |
|
199 |
||
135 | 200 |
aGC displayPolygon:vertices |
201 |
||
202 |
" |
|
203 |
|v| |
|
204 |
||
205 |
v := View new open. |
|
206 |
[v shown] whileFalse:[Processor yield]. |
|
207 |
||
208 |
(Polygon vertices:( |
|
267 | 209 |
Array |
210 |
with:10@10 |
|
211 |
with:60@10 |
|
212 |
with:35@60)) displayStrokedOn:v |
|
135 | 213 |
|
214 |
|v| |
|
215 |
||
216 |
v := View new open. |
|
217 |
[v shown] whileFalse:[Processor yield]. |
|
218 |
||
219 |
(Polygon vertices:( |
|
267 | 220 |
Array |
221 |
with:10@10 |
|
222 |
with:60@10 |
|
223 |
with:35@60 |
|
224 |
with:10@10)) displayStrokedOn:v |
|
135 | 225 |
" |
226 |
||
267 | 227 |
"Modified: 27.4.1996 / 14:52:29 / cg" |
135 | 228 |
! ! |
229 |
||
230 |
!Polygon methodsFor:'enumerating'! |
|
231 |
||
78 | 232 |
edgesDo:aTwoArgBlock |
233 |
"evaluate aTwoArgBlock for each pair of vertices" |
|
234 |
||
235 |
1 to:vertices size-1 do:[:i | |
|
236 |
aTwoArgBlock value:(vertices at:i) value:(vertices at:i+1) |
|
237 |
]. |
|
238 |
||
239 |
" |
|
240 |
|v p| |
|
241 |
||
242 |
v := View new open. |
|
243 |
[v shown] whileFalse:[Processor yield]. |
|
244 |
||
245 |
p := Polygon vertices:(Array with:5@5 |
|
246 |
with:50@5 |
|
247 |
with:30@30 |
|
248 |
with:5@5). |
|
249 |
||
250 |
p displayOn:v. |
|
251 |
(Delay forSeconds:3) wait. |
|
252 |
||
253 |
p edgesDo:[:p1 :p2 | v lineWidth:3. v displayLineFrom:p1 to:p2] |
|
254 |
" |
|
255 |
! |
|
256 |
||
135 | 257 |
verticesDo:aBlock |
258 |
"evaluate aBlock for each point" |
|
259 |
||
260 |
vertices do:aBlock |
|
261 |
||
262 |
" |
|
263 |
|v p| |
|
264 |
||
265 |
v := View new open. |
|
266 |
[v shown] whileFalse:[Processor yield]. |
|
78 | 267 |
|
135 | 268 |
p := Polygon vertices:(Array with:5@5 |
269 |
with:50@5 |
|
270 |
with:30@30 |
|
271 |
with:5@5). |
|
272 |
||
273 |
p displayOn:v. |
|
274 |
(Delay forSeconds:3) wait. |
|
275 |
||
276 |
p verticesDo:[:p | v displayRectangleX:p x -3 y:p y -3 width:6 height:6] |
|
277 |
" |
|
78 | 278 |
! ! |
279 |
||
135 | 280 |
!Polygon methodsFor:'queries'! |
0 | 281 |
|
282 |
bottom |
|
14 | 283 |
"return the bottom boundary of the polygon, |
284 |
that is the maximum y coordinate of all its points" |
|
285 |
||
78 | 286 |
(vertices size == 0) ifTrue: [^ nil]. |
287 |
^ vertices inject:(vertices at:1) y into:[:maxSoFar :p | maxSoFar max:(p y)] |
|
0 | 288 |
|
78 | 289 |
" |
290 |
(Polygon vertices:( |
|
291 |
Array |
|
292 |
with:10@10 |
|
293 |
with:60@10 |
|
294 |
with:35@60)) bottom |
|
295 |
" |
|
0 | 296 |
! |
297 |
||
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
298 |
canBeFilled |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
299 |
"return true, if the receiver can be drawn as a filled geometric. |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
300 |
Always true here." |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
301 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
302 |
^ true |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
303 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
304 |
"Created: 8.5.1996 / 08:16:53 / cg" |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
305 |
! |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
306 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
307 |
computeBounds |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
308 |
"return the smallest enclosing rectangle" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
309 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
310 |
|minX maxX minY maxY t n "{ Class: SmallInteger }" | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
311 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
312 |
n := vertices size. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
313 |
n == 0 ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
314 |
^ nil "/ mhmh - should we return an empty rectangle here ? |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
315 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
316 |
t := vertices at:1. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
317 |
minX := maxX := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
318 |
minY := maxY := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
319 |
2 to:n do:[:i | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
320 |
|x y t| |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
321 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
322 |
t := vertices at:i. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
323 |
x := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
324 |
y := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
325 |
x < minX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
326 |
minX := x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
327 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
328 |
x > maxX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
329 |
maxX := x. |
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 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
332 |
y < minY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
333 |
minY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
334 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
335 |
y > maxY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
336 |
maxY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
337 |
] |
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 |
]. |
283 | 340 |
^ Rectangle left:minX top:minY right:maxX bottom:maxY. |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
341 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
342 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
343 |
|p| |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
344 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
345 |
p := (Polygon vertices:( |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
346 |
Array |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
347 |
with:10@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
348 |
with:60@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
349 |
with:35@60)). |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
350 |
p bounds |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
351 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
352 |
|
283 | 353 |
"Modified: 8.5.1996 / 20:51:42 / cg" |
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
354 |
"Created: 12.2.1997 / 11:44:11 / cg" |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
355 |
! |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
356 |
|
0 | 357 |
left |
14 | 358 |
"return the left boundary of the polygon, |
359 |
that is the minimum x coordinate of all its points" |
|
360 |
||
78 | 361 |
(vertices size == 0) ifTrue: [^ nil]. |
362 |
^ vertices inject:(vertices at:1) x into:[:minSoFar :p | minSoFar min:(p x)] |
|
0 | 363 |
|
78 | 364 |
" |
365 |
(Polygon vertices:( |
|
366 |
Array |
|
367 |
with:10@10 |
|
368 |
with:60@10 |
|
369 |
with:35@60)) left |
|
370 |
" |
|
371 |
||
0 | 372 |
! |
373 |
||
374 |
right |
|
14 | 375 |
"return the right boundary of the polygon, |
88 | 376 |
that is the maximum x coordinate of all its points" |
14 | 377 |
|
78 | 378 |
(vertices size == 0) ifTrue: [^ nil]. |
379 |
^ vertices inject:(vertices at:1) x into:[:maxSoFar :p | maxSoFar max:(p x)] |
|
380 |
||
381 |
" |
|
382 |
(Polygon vertices:( |
|
383 |
Array |
|
384 |
with:10@10 |
|
385 |
with:60@10 |
|
386 |
with:35@60)) right |
|
387 |
" |
|
388 |
||
135 | 389 |
! |
78 | 390 |
|
135 | 391 |
top |
392 |
"return the top boundary of the polygon, |
|
393 |
that is the minimum y coordinate of all its points" |
|
78 | 394 |
|
135 | 395 |
(vertices size == 0) ifTrue: [^ nil]. |
396 |
^ vertices inject:(vertices at:1) y into:[:minSoFar :p | minSoFar min:(p y)] |
|
78 | 397 |
|
398 |
" |
|
399 |
(Polygon vertices:( |
|
400 |
Array |
|
401 |
with:10@10 |
|
402 |
with:60@10 |
|
135 | 403 |
with:35@60)) top |
78 | 404 |
" |
405 |
||
135 | 406 |
! ! |
78 | 407 |
|
135 | 408 |
!Polygon class methodsFor:'documentation'! |
78 | 409 |
|
135 | 410 |
version |
1303 | 411 |
^ '$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.25 2003-08-29 17:32:14 cg Exp $' |
0 | 412 |
! ! |