author | sr |
Thu, 18 Oct 2007 12:16:14 +0200 | |
changeset 1901 | 5e6771205713 |
parent 1430 | baf6bfed6d56 |
child 2091 | 24965fa2be83 |
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 |
|
1430 | 131 |
Polygon vertices:(#(10 10 100 0 50 50) pairWiseCollect:[:x :y | x @ y]). |
132 |
" |
|
133 |
||
134 |
" |
|
135 |
|p v| |
|
136 |
||
137 |
v := View new openAndWait. |
|
138 |
p := Polygon |
|
139 |
vertices:(Array with:10@10 |
|
140 |
with:20@10 |
|
141 |
with:20@30). |
|
142 |
p displayOn:v |
|
78 | 143 |
" |
280 | 144 |
|
145 |
"Modified: 8.5.1996 / 20:11:31 / cg" |
|
135 | 146 |
! ! |
147 |
||
148 |
!Polygon methodsFor:'accessing'! |
|
149 |
||
150 |
vertices |
|
151 |
"return the array containing my points" |
|
78 | 152 |
|
135 | 153 |
^ vertices |
154 |
! |
|
78 | 155 |
|
135 | 156 |
vertices:anArrayOfPoints |
157 |
"set the array containing my points" |
|
158 |
||
159 |
vertices := anArrayOfPoints |
|
78 | 160 |
! ! |
161 |
||
283 | 162 |
!Polygon methodsFor:'converting'! |
163 |
||
164 |
asPointArray |
|
165 |
"return an array containing my vertex points. |
|
166 |
Notice, that no copy of my vertices is created - you should not |
|
167 |
modify the returned collections points (unless you want to affect |
|
168 |
the polygon ...)." |
|
169 |
||
170 |
^ vertices |
|
171 |
||
172 |
"Modified: 8.5.1996 / 20:43:39 / cg" |
|
173 |
! ! |
|
174 |
||
135 | 175 |
!Polygon methodsFor:'displaying'! |
78 | 176 |
|
135 | 177 |
displayFilledOn:aGC |
267 | 178 |
"display a filled polygin as represented by the receiver in |
179 |
the graphicsContext, aGC" |
|
180 |
||
135 | 181 |
aGC fillPolygon:vertices |
78 | 182 |
|
183 |
" |
|
135 | 184 |
|v| |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
185 |
v := View new openAndWait. |
135 | 186 |
|
187 |
(Polygon vertices:( |
|
267 | 188 |
Array |
189 |
with:10@10 |
|
190 |
with:60@10 |
|
191 |
with:35@60)) displayFilledOn:v |
|
135 | 192 |
|
193 |
|v| |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
194 |
v := View new openAndWait. |
78 | 195 |
|
135 | 196 |
(Polygon vertices:( |
267 | 197 |
Array |
198 |
with:10@10 |
|
199 |
with:60@10 |
|
200 |
with:35@60 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
201 |
with:10@10)) displayStrokedOn:v |
78 | 202 |
" |
267 | 203 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
204 |
"Modified: 8.5.1996 / 14:41:47 / cg" |
78 | 205 |
! |
206 |
||
135 | 207 |
displayStrokedOn:aGC |
267 | 208 |
"display an unfilled polygin as represented by the receiver in |
209 |
the graphicsContext, aGC" |
|
210 |
||
135 | 211 |
aGC displayPolygon:vertices |
212 |
||
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)) displayStrokedOn:v |
|
135 | 224 |
|
225 |
|v| |
|
226 |
||
227 |
v := View new open. |
|
228 |
[v shown] whileFalse:[Processor yield]. |
|
229 |
||
230 |
(Polygon vertices:( |
|
267 | 231 |
Array |
232 |
with:10@10 |
|
233 |
with:60@10 |
|
234 |
with:35@60 |
|
235 |
with:10@10)) displayStrokedOn:v |
|
135 | 236 |
" |
237 |
||
267 | 238 |
"Modified: 27.4.1996 / 14:52:29 / cg" |
135 | 239 |
! ! |
240 |
||
241 |
!Polygon methodsFor:'enumerating'! |
|
242 |
||
78 | 243 |
edgesDo:aTwoArgBlock |
244 |
"evaluate aTwoArgBlock for each pair of vertices" |
|
245 |
||
246 |
1 to:vertices size-1 do:[:i | |
|
247 |
aTwoArgBlock value:(vertices at:i) value:(vertices at:i+1) |
|
248 |
]. |
|
249 |
||
250 |
" |
|
251 |
|v p| |
|
252 |
||
253 |
v := View new open. |
|
254 |
[v shown] whileFalse:[Processor yield]. |
|
255 |
||
256 |
p := Polygon vertices:(Array with:5@5 |
|
257 |
with:50@5 |
|
258 |
with:30@30 |
|
259 |
with:5@5). |
|
260 |
||
261 |
p displayOn:v. |
|
262 |
(Delay forSeconds:3) wait. |
|
263 |
||
264 |
p edgesDo:[:p1 :p2 | v lineWidth:3. v displayLineFrom:p1 to:p2] |
|
265 |
" |
|
266 |
! |
|
267 |
||
135 | 268 |
verticesDo:aBlock |
269 |
"evaluate aBlock for each point" |
|
270 |
||
271 |
vertices do:aBlock |
|
272 |
||
273 |
" |
|
274 |
|v p| |
|
275 |
||
276 |
v := View new open. |
|
277 |
[v shown] whileFalse:[Processor yield]. |
|
78 | 278 |
|
135 | 279 |
p := Polygon vertices:(Array with:5@5 |
280 |
with:50@5 |
|
281 |
with:30@30 |
|
282 |
with:5@5). |
|
283 |
||
284 |
p displayOn:v. |
|
285 |
(Delay forSeconds:3) wait. |
|
286 |
||
287 |
p verticesDo:[:p | v displayRectangleX:p x -3 y:p y -3 width:6 height:6] |
|
288 |
" |
|
78 | 289 |
! ! |
290 |
||
135 | 291 |
!Polygon methodsFor:'queries'! |
0 | 292 |
|
293 |
bottom |
|
14 | 294 |
"return the bottom boundary of the polygon, |
295 |
that is the maximum y coordinate of all its points" |
|
296 |
||
78 | 297 |
(vertices size == 0) ifTrue: [^ nil]. |
298 |
^ vertices inject:(vertices at:1) y into:[:maxSoFar :p | maxSoFar max:(p y)] |
|
0 | 299 |
|
78 | 300 |
" |
301 |
(Polygon vertices:( |
|
302 |
Array |
|
303 |
with:10@10 |
|
304 |
with:60@10 |
|
305 |
with:35@60)) bottom |
|
306 |
" |
|
0 | 307 |
! |
308 |
||
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
309 |
canBeFilled |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
310 |
"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
|
311 |
Always true here." |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
312 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
313 |
^ true |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
314 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
315 |
"Created: 8.5.1996 / 08:16:53 / cg" |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
316 |
! |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
317 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
318 |
computeBounds |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
319 |
"return the smallest enclosing rectangle" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
320 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
321 |
|minX maxX minY maxY t n "{ Class: SmallInteger }" | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
322 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
323 |
n := vertices size. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
324 |
n == 0 ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
325 |
^ nil "/ mhmh - should we return an empty rectangle here ? |
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 |
t := vertices at:1. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
328 |
minX := maxX := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
329 |
minY := maxY := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
330 |
2 to:n do:[:i | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
331 |
|x y t| |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
332 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
333 |
t := vertices at:i. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
334 |
x := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
335 |
y := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
336 |
x < minX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
337 |
minX := x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
338 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
339 |
x > maxX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
340 |
maxX := x. |
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 |
y < minY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
344 |
minY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
345 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
346 |
y > maxY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
347 |
maxY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
348 |
] |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
349 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
350 |
]. |
283 | 351 |
^ Rectangle left:minX top:minY right:maxX bottom:maxY. |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
352 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
353 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
354 |
|p| |
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 |
p := (Polygon vertices:( |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
357 |
Array |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
358 |
with:10@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
359 |
with:60@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
360 |
with:35@60)). |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
361 |
p bounds |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
362 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
363 |
|
283 | 364 |
"Modified: 8.5.1996 / 20:51:42 / cg" |
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
365 |
"Created: 12.2.1997 / 11:44:11 / cg" |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
366 |
! |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
367 |
|
0 | 368 |
left |
14 | 369 |
"return the left boundary of the polygon, |
370 |
that is the minimum x coordinate of all its points" |
|
371 |
||
78 | 372 |
(vertices size == 0) ifTrue: [^ nil]. |
373 |
^ vertices inject:(vertices at:1) x into:[:minSoFar :p | minSoFar min:(p x)] |
|
0 | 374 |
|
78 | 375 |
" |
376 |
(Polygon vertices:( |
|
377 |
Array |
|
378 |
with:10@10 |
|
379 |
with:60@10 |
|
380 |
with:35@60)) left |
|
381 |
" |
|
382 |
||
0 | 383 |
! |
384 |
||
385 |
right |
|
14 | 386 |
"return the right boundary of the polygon, |
88 | 387 |
that is the maximum x coordinate of all its points" |
14 | 388 |
|
78 | 389 |
(vertices size == 0) ifTrue: [^ nil]. |
390 |
^ vertices inject:(vertices at:1) x into:[:maxSoFar :p | maxSoFar max:(p x)] |
|
391 |
||
392 |
" |
|
393 |
(Polygon vertices:( |
|
394 |
Array |
|
395 |
with:10@10 |
|
396 |
with:60@10 |
|
397 |
with:35@60)) right |
|
398 |
" |
|
399 |
||
135 | 400 |
! |
78 | 401 |
|
135 | 402 |
top |
403 |
"return the top boundary of the polygon, |
|
404 |
that is the minimum y coordinate of all its points" |
|
78 | 405 |
|
135 | 406 |
(vertices size == 0) ifTrue: [^ nil]. |
407 |
^ vertices inject:(vertices at:1) y into:[:minSoFar :p | minSoFar min:(p y)] |
|
78 | 408 |
|
409 |
" |
|
410 |
(Polygon vertices:( |
|
411 |
Array |
|
412 |
with:10@10 |
|
413 |
with:60@10 |
|
135 | 414 |
with:35@60)) top |
78 | 415 |
" |
416 |
||
135 | 417 |
! ! |
78 | 418 |
|
135 | 419 |
!Polygon class methodsFor:'documentation'! |
78 | 420 |
|
135 | 421 |
version |
1430 | 422 |
^ '$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.26 2004-03-20 13:19:25 cg Exp $' |
0 | 423 |
! ! |