author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 16:22:01 +0100 | |
changeset 112 | 3e18f2cfe430 |
parent 88 | 09fca886e594 |
child 135 | 64428d5e8d06 |
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 |
14 |
instanceVariableNames:'vertices' |
|
0 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
68 | 17 |
category:'Graphics-Geometry' |
0 | 18 |
! |
19 |
||
31 | 20 |
!Polygon class methodsFor: 'documentation'! |
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 |
version |
|
112
3e18f2cfe430
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
88
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.13 1995-11-11 15:21:28 cg Exp $' |
31 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
Polygon - an array of points |
|
78 | 43 |
|
31 | 44 |
Adds simple boundary checking methods to Array. |
45 |
(needs much more - such as inside check, area computation etc.) |
|
46 |
" |
|
47 |
! ! |
|
0 | 48 |
|
78 | 49 |
!Polygon class methodsFor: 'instance creation'! |
50 |
||
51 |
vertices:anArrayOfPoints |
|
52 |
^ self new vertices:anArrayOfPoints |
|
53 |
||
54 |
" |
|
55 |
Polygon vertices:(Array with:10@10 |
|
56 |
with:20@20 |
|
57 |
with:30@30) |
|
58 |
||
59 |
Polygon vertices:(#(10 10 100 0 50 50) pairWiseCollect:[:x :y | x @ y]) |
|
60 |
" |
|
61 |
! |
|
62 |
||
63 |
fromRectangle:aRectangle |
|
64 |
^ self new vertices:(aRectangle asArrayOfPoints) |
|
65 |
||
66 |
" |
|
67 |
Polygon fromRectangle:(50@50 corner:100@100) |
|
68 |
" |
|
69 |
! ! |
|
70 |
||
71 |
!Polygon methodsFor: 'enumerating'! |
|
72 |
||
73 |
verticesDo:aBlock |
|
74 |
"evaluate aBlock for each point" |
|
75 |
||
76 |
vertices do:aBlock |
|
77 |
||
78 |
" |
|
79 |
|v p| |
|
80 |
||
81 |
v := View new open. |
|
82 |
[v shown] whileFalse:[Processor yield]. |
|
83 |
||
84 |
p := Polygon vertices:(Array with:5@5 |
|
85 |
with:50@5 |
|
86 |
with:30@30 |
|
87 |
with:5@5). |
|
88 |
||
89 |
p displayOn:v. |
|
90 |
(Delay forSeconds:3) wait. |
|
91 |
||
92 |
p verticesDo:[:p | v displayRectangleX:p x -3 y:p y -3 width:6 height:6] |
|
93 |
" |
|
94 |
! |
|
95 |
||
96 |
edgesDo:aTwoArgBlock |
|
97 |
"evaluate aTwoArgBlock for each pair of vertices" |
|
98 |
||
99 |
1 to:vertices size-1 do:[:i | |
|
100 |
aTwoArgBlock value:(vertices at:i) value:(vertices at:i+1) |
|
101 |
]. |
|
102 |
||
103 |
" |
|
104 |
|v p| |
|
105 |
||
106 |
v := View new open. |
|
107 |
[v shown] whileFalse:[Processor yield]. |
|
108 |
||
109 |
p := Polygon vertices:(Array with:5@5 |
|
110 |
with:50@5 |
|
111 |
with:30@30 |
|
112 |
with:5@5). |
|
113 |
||
114 |
p displayOn:v. |
|
115 |
(Delay forSeconds:3) wait. |
|
116 |
||
117 |
p edgesDo:[:p1 :p2 | v lineWidth:3. v displayLineFrom:p1 to:p2] |
|
118 |
" |
|
119 |
! ! |
|
120 |
||
0 | 121 |
!Polygon methodsFor: 'accessing'! |
122 |
||
78 | 123 |
vertices |
124 |
"return the array containing my points" |
|
125 |
||
126 |
^ vertices |
|
127 |
! |
|
128 |
||
129 |
vertices:anArrayOfPoints |
|
130 |
"set the array containing my points" |
|
131 |
||
132 |
vertices := anArrayOfPoints |
|
133 |
! ! |
|
134 |
||
135 |
!Polygon methodsFor: 'queries'! |
|
136 |
||
0 | 137 |
top |
14 | 138 |
"return the top boundary of the polygon, |
139 |
that is the minimum y coordinate of all its points" |
|
140 |
||
78 | 141 |
(vertices size == 0) ifTrue: [^ nil]. |
142 |
^ vertices inject:(vertices at:1) y into:[:minSoFar :p | minSoFar min:(p y)] |
|
0 | 143 |
|
78 | 144 |
" |
145 |
(Polygon vertices:( |
|
146 |
Array |
|
147 |
with:10@10 |
|
148 |
with:60@10 |
|
149 |
with:35@60)) top |
|
150 |
" |
|
151 |
||
0 | 152 |
! |
153 |
||
154 |
bottom |
|
14 | 155 |
"return the bottom boundary of the polygon, |
156 |
that is the maximum y coordinate of all its points" |
|
157 |
||
78 | 158 |
(vertices size == 0) ifTrue: [^ nil]. |
159 |
^ vertices inject:(vertices at:1) y into:[:maxSoFar :p | maxSoFar max:(p y)] |
|
0 | 160 |
|
78 | 161 |
" |
162 |
(Polygon vertices:( |
|
163 |
Array |
|
164 |
with:10@10 |
|
165 |
with:60@10 |
|
166 |
with:35@60)) bottom |
|
167 |
" |
|
0 | 168 |
! |
169 |
||
170 |
left |
|
14 | 171 |
"return the left boundary of the polygon, |
172 |
that is the minimum x coordinate of all its points" |
|
173 |
||
78 | 174 |
(vertices size == 0) ifTrue: [^ nil]. |
175 |
^ vertices inject:(vertices at:1) x into:[:minSoFar :p | minSoFar min:(p x)] |
|
0 | 176 |
|
78 | 177 |
" |
178 |
(Polygon vertices:( |
|
179 |
Array |
|
180 |
with:10@10 |
|
181 |
with:60@10 |
|
182 |
with:35@60)) left |
|
183 |
" |
|
184 |
||
0 | 185 |
! |
186 |
||
187 |
right |
|
14 | 188 |
"return the right boundary of the polygon, |
88 | 189 |
that is the maximum x coordinate of all its points" |
14 | 190 |
|
78 | 191 |
(vertices size == 0) ifTrue: [^ nil]. |
192 |
^ vertices inject:(vertices at:1) x into:[:maxSoFar :p | maxSoFar max:(p x)] |
|
193 |
||
194 |
" |
|
195 |
(Polygon vertices:( |
|
196 |
Array |
|
197 |
with:10@10 |
|
198 |
with:60@10 |
|
199 |
with:35@60)) right |
|
200 |
" |
|
201 |
||
202 |
! ! |
|
203 |
||
204 |
!Polygon methodsFor:'displaying'! |
|
205 |
||
206 |
displayStrokedOn:aGC |
|
207 |
aGC displayPolygon:vertices |
|
208 |
||
209 |
" |
|
210 |
|v| |
|
211 |
||
212 |
v := View new open. |
|
213 |
[v shown] whileFalse:[Processor yield]. |
|
214 |
||
215 |
(Polygon vertices:( |
|
216 |
Array |
|
217 |
with:10@10 |
|
218 |
with:60@10 |
|
219 |
with:35@60)) displayStrokedOn:v |
|
220 |
||
221 |
|v| |
|
222 |
||
223 |
v := View new open. |
|
224 |
[v shown] whileFalse:[Processor yield]. |
|
0 | 225 |
|
78 | 226 |
(Polygon vertices:( |
227 |
Array |
|
228 |
with:10@10 |
|
229 |
with:60@10 |
|
230 |
with:35@60 |
|
231 |
with:10@10)) displayStrokedOn:v |
|
232 |
" |
|
233 |
||
234 |
! |
|
235 |
||
236 |
displayFilledOn:aGC |
|
237 |
aGC fillPolygon:vertices |
|
238 |
||
239 |
" |
|
240 |
|v| |
|
241 |
||
242 |
v := View new open. |
|
243 |
[v shown] whileFalse:[Processor yield]. |
|
244 |
||
245 |
(Polygon vertices:( |
|
246 |
Array |
|
247 |
with:10@10 |
|
248 |
with:60@10 |
|
249 |
with:35@60)) displayFilledOn:v |
|
250 |
||
251 |
|v| |
|
252 |
||
253 |
v := View new open. |
|
254 |
[v shown] whileFalse:[Processor yield]. |
|
255 |
||
256 |
(Polygon vertices:( |
|
257 |
Array |
|
258 |
with:10@10 |
|
259 |
with:60@10 |
|
260 |
with:35@60 |
|
261 |
with:10@10)) displayFilledOn:v |
|
262 |
" |
|
0 | 263 |
! ! |