author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 4838 | e74d8a9cf15d |
permissions | -rw-r--r-- |
4080 | 1 |
"{ Encoding: utf8 }" |
2 |
||
0 | 3 |
" |
4 | 4 |
COPYRIGHT (c) 1988 by Claus Gittinger |
68 | 5 |
All Rights Reserved |
0 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1303 | 14 |
"{ Package: 'stx:libbasic2' }" |
15 |
||
4080 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
78 | 18 |
Geometric subclass:#Polygon |
252 | 19 |
instanceVariableNames:'vertices' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
1303 | 22 |
category:'Graphics-Geometry-Objects' |
0 | 23 |
! |
24 |
||
135 | 25 |
!Polygon class methodsFor:'documentation'! |
31 | 26 |
|
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
68 | 30 |
All Rights Reserved |
0 | 31 |
|
31 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
43 |
Polygon - an array of points |
|
78 | 44 |
|
31 | 45 |
Adds simple boundary checking methods to Array. |
46 |
(needs much more - such as inside check, area computation etc.) |
|
255 | 47 |
|
48 |
[author:] |
|
49 |
Claus Gittinger |
|
267 | 50 |
|
51 |
[see also:] |
|
331 | 52 |
Rectangle EllipticalArc Spline Circle Point LineSegment Curve |
53 |
Arrow ArroedSpline |
|
54 |
GraphicsContext StrokingWrapper FillingWrapper |
|
280 | 55 |
" |
56 |
! |
|
57 |
||
58 |
examples |
|
59 |
" |
|
60 |
simple polygon; filled & unfilled: |
|
61 |
[exBegin] |
|
62 |
|v p| |
|
63 |
||
64 |
v := (View extent:200@200) openAndWait. |
|
65 |
||
66 |
p := Polygon vertices: |
|
67 |
(Array with:(10@10) |
|
68 |
with:(90@90) |
|
69 |
with:(10@90)). |
|
70 |
||
71 |
v scale:2. |
|
72 |
v paint:Color blue. |
|
73 |
p displayFilledOn:v. |
|
74 |
||
75 |
v paint:Color red. |
|
76 |
p displayStrokedOn:v. |
|
77 |
||
78 |
v scale:1; translation:100@0. |
|
79 |
v paint:Color green. |
|
80 |
p displayFilledOn:v. |
|
81 |
||
82 |
v paint:Color black. |
|
83 |
p displayStrokedOn:v. |
|
84 |
[exEnd] |
|
85 |
||
86 |
arbitrary polygon; filled & unfilled: |
|
87 |
[exBegin] |
|
88 |
|v p| |
|
89 |
||
90 |
v := (View extent:200@200) openAndWait. |
|
91 |
v scale:2. |
|
92 |
||
93 |
p := Polygon vertices: |
|
94 |
(Array with:(10@10) |
|
95 |
with:(90@90) |
|
96 |
with:(50@90) |
|
97 |
with:(90@10) |
|
98 |
with:(10@90) |
|
99 |
). |
|
100 |
||
101 |
v paint:Color blue. |
|
102 |
p displayFilledOn:v. |
|
103 |
||
104 |
v paint:Color red. |
|
105 |
p displayStrokedOn:v. |
|
106 |
[exEnd] |
|
4837 | 107 |
chaikin curve fitting: |
108 |
[exBegin] |
|
109 |
|p p2 v| |
|
110 |
||
111 |
p := Polygon |
|
112 |
vertices:{ |
|
113 |
0 @ 0 . |
|
114 |
0 @ 100 . |
|
115 |
100 @ 100 . |
|
116 |
110 @ -10 . |
|
117 |
200 @ 150 |
|
118 |
}. |
|
119 |
||
120 |
v := (View extent:300@300) openAndWait. |
|
121 |
v translateBy:50@250. |
|
122 |
v scale:(1 @ -1). |
|
123 |
v paint:Color blue. |
|
124 |
p displayStrokedOn:v. |
|
125 |
||
126 |
p2 := p asChaikinCurve:10. |
|
127 |
v paint:Color red. |
|
128 |
p2 displayStrokedOn:v. |
|
129 |
[exEnd] |
|
31 | 130 |
" |
131 |
! ! |
|
0 | 132 |
|
135 | 133 |
!Polygon class methodsFor:'instance creation'! |
134 |
||
135 |
fromRectangle:aRectangle |
|
280 | 136 |
"return a new polygon, taking the rectangles vertices" |
137 |
||
138 |
^ self new vertices:(aRectangle asPointArray) |
|
135 | 139 |
|
140 |
" |
|
141 |
Polygon fromRectangle:(50@50 corner:100@100) |
|
142 |
" |
|
280 | 143 |
|
144 |
"Modified: 8.5.1996 / 20:15:18 / cg" |
|
135 | 145 |
! |
78 | 146 |
|
147 |
vertices:anArrayOfPoints |
|
280 | 148 |
"return a new polygon, given a collection of vertices" |
149 |
||
78 | 150 |
^ self new vertices:anArrayOfPoints |
151 |
||
152 |
" |
|
153 |
Polygon vertices:(Array with:10@10 |
|
280 | 154 |
with:20@20 |
155 |
with:30@30) |
|
78 | 156 |
|
1430 | 157 |
Polygon vertices:(#(10 10 100 0 50 50) pairWiseCollect:[:x :y | x @ y]). |
158 |
" |
|
159 |
||
160 |
" |
|
161 |
|p v| |
|
162 |
||
163 |
v := View new openAndWait. |
|
164 |
p := Polygon |
|
165 |
vertices:(Array with:10@10 |
|
166 |
with:20@10 |
|
167 |
with:20@30). |
|
168 |
p displayOn:v |
|
78 | 169 |
" |
280 | 170 |
|
171 |
"Modified: 8.5.1996 / 20:11:31 / cg" |
|
135 | 172 |
! ! |
173 |
||
174 |
!Polygon methodsFor:'accessing'! |
|
175 |
||
2317 | 176 |
add:aPoint |
177 |
vertices isNil ifTrue:[ |
|
178 |
vertices := OrderedCollection new |
|
179 |
]. |
|
180 |
vertices add:aPoint |
|
181 |
! |
|
182 |
||
135 | 183 |
vertices |
184 |
"return the array containing my points" |
|
78 | 185 |
|
135 | 186 |
^ vertices |
187 |
! |
|
78 | 188 |
|
135 | 189 |
vertices:anArrayOfPoints |
190 |
"set the array containing my points" |
|
191 |
||
192 |
vertices := anArrayOfPoints |
|
78 | 193 |
! ! |
194 |
||
283 | 195 |
!Polygon methodsFor:'converting'! |
196 |
||
4837 | 197 |
asChaikinCurve |
198 |
"return a new polygon, which is generated by applying |
|
4838 | 199 |
the chaikin corner cutting algorithm once |
200 |
(see the example on the class side)" |
|
4837 | 201 |
|
202 |
|out| |
|
203 |
||
4838 | 204 |
out := OrderedCollection new:(vertices size * 2). |
4837 | 205 |
|
4838 | 206 |
out add:vertices first. |
4837 | 207 |
1 to:(vertices size-1) do:[:i | |
208 |
| pI0 pI1 qI rI | |
|
209 |
pI0 := vertices at:i. |
|
4838 | 210 |
pI1 := vertices at:i+1. |
4837 | 211 |
qI := ( (3/4)*pI0 ) + ( (1/4) * pI1 ). |
4838 | 212 |
rI := ( (1/4)*pI0 ) + ( (3/4) * pI1 ). |
4837 | 213 |
out add:qI. |
214 |
out add:rI. |
|
4838 | 215 |
]. |
216 |
out add:vertices last. |
|
4837 | 217 |
^ self class vertices:out. |
218 |
||
219 |
"Created: / 06-03-2019 / 19:44:13 / Claus Gittinger" |
|
4838 | 220 |
"Modified (comment): / 06-03-2019 / 22:45:29 / Claus Gittinger" |
4837 | 221 |
! |
222 |
||
223 |
asChaikinCurve:level |
|
224 |
"return a new polygon, which is generated by applying |
|
4838 | 225 |
the corner cutting algorithm n-times. |
226 |
(see the example on the class side)" |
|
4837 | 227 |
|
228 |
|in out| |
|
229 |
||
230 |
in := self. |
|
231 |
level timesRepeat:[ |
|
232 |
out := in asChaikinCurve. |
|
233 |
in := out. |
|
234 |
]. |
|
235 |
^ out |
|
236 |
||
237 |
" |
|
238 |
|p p2 v| |
|
239 |
||
240 |
p := Polygon |
|
241 |
vertices:{ |
|
242 |
0 @ 0 . |
|
243 |
0 @ 100 . |
|
244 |
100 @ 100 . |
|
245 |
110 @ -10 . |
|
246 |
200 @ 150 |
|
247 |
}. |
|
248 |
||
249 |
v := (View extent:300@300) openAndWait. |
|
250 |
v translateBy:50@250. |
|
251 |
v scale:(1 @ -1). |
|
252 |
v paint:Color blue. |
|
253 |
p displayStrokedOn:v. |
|
254 |
||
255 |
p2 := p asChaikinCurve:10. |
|
256 |
v paint:Color red. |
|
257 |
p2 displayStrokedOn:v. |
|
258 |
" |
|
259 |
||
260 |
"Created: / 06-03-2019 / 19:28:08 / Claus Gittinger" |
|
4838 | 261 |
"Modified (comment): / 06-03-2019 / 22:45:36 / Claus Gittinger" |
4837 | 262 |
! |
263 |
||
283 | 264 |
asPointArray |
265 |
"return an array containing my vertex points. |
|
266 |
Notice, that no copy of my vertices is created - you should not |
|
267 |
modify the returned collections points (unless you want to affect |
|
268 |
the polygon ...)." |
|
269 |
||
270 |
^ vertices |
|
271 |
||
272 |
"Modified: 8.5.1996 / 20:43:39 / cg" |
|
273 |
! ! |
|
274 |
||
135 | 275 |
!Polygon methodsFor:'displaying'! |
78 | 276 |
|
135 | 277 |
displayFilledOn:aGC |
267 | 278 |
"display a filled polygin as represented by the receiver in |
279 |
the graphicsContext, aGC" |
|
280 |
||
135 | 281 |
aGC fillPolygon:vertices |
78 | 282 |
|
283 |
" |
|
135 | 284 |
|v| |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
285 |
v := View new openAndWait. |
135 | 286 |
|
287 |
(Polygon vertices:( |
|
267 | 288 |
Array |
289 |
with:10@10 |
|
290 |
with:60@10 |
|
291 |
with:35@60)) displayFilledOn:v |
|
135 | 292 |
|
293 |
|v| |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
294 |
v := View new openAndWait. |
78 | 295 |
|
135 | 296 |
(Polygon vertices:( |
267 | 297 |
Array |
298 |
with:10@10 |
|
299 |
with:60@10 |
|
300 |
with:35@60 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
301 |
with:10@10)) displayStrokedOn:v |
78 | 302 |
" |
267 | 303 |
|
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
304 |
"Modified: 8.5.1996 / 14:41:47 / cg" |
78 | 305 |
! |
306 |
||
135 | 307 |
displayStrokedOn:aGC |
267 | 308 |
"display an unfilled polygin as represented by the receiver in |
309 |
the graphicsContext, aGC" |
|
310 |
||
135 | 311 |
aGC displayPolygon:vertices |
312 |
||
313 |
" |
|
314 |
|v| |
|
315 |
||
316 |
v := View new open. |
|
317 |
[v shown] whileFalse:[Processor yield]. |
|
318 |
||
319 |
(Polygon vertices:( |
|
267 | 320 |
Array |
321 |
with:10@10 |
|
322 |
with:60@10 |
|
323 |
with:35@60)) displayStrokedOn:v |
|
135 | 324 |
|
325 |
|v| |
|
326 |
||
327 |
v := View new open. |
|
328 |
[v shown] whileFalse:[Processor yield]. |
|
329 |
||
330 |
(Polygon vertices:( |
|
267 | 331 |
Array |
332 |
with:10@10 |
|
333 |
with:60@10 |
|
334 |
with:35@60 |
|
335 |
with:10@10)) displayStrokedOn:v |
|
135 | 336 |
" |
337 |
||
267 | 338 |
"Modified: 27.4.1996 / 14:52:29 / cg" |
135 | 339 |
! ! |
340 |
||
341 |
!Polygon methodsFor:'enumerating'! |
|
342 |
||
78 | 343 |
edgesDo:aTwoArgBlock |
344 |
"evaluate aTwoArgBlock for each pair of vertices" |
|
345 |
||
346 |
1 to:vertices size-1 do:[:i | |
|
347 |
aTwoArgBlock value:(vertices at:i) value:(vertices at:i+1) |
|
348 |
]. |
|
349 |
||
350 |
" |
|
351 |
|v p| |
|
352 |
||
353 |
v := View new open. |
|
354 |
[v shown] whileFalse:[Processor yield]. |
|
355 |
||
356 |
p := Polygon vertices:(Array with:5@5 |
|
357 |
with:50@5 |
|
358 |
with:30@30 |
|
359 |
with:5@5). |
|
360 |
||
361 |
p displayOn:v. |
|
362 |
(Delay forSeconds:3) wait. |
|
363 |
||
364 |
p edgesDo:[:p1 :p2 | v lineWidth:3. v displayLineFrom:p1 to:p2] |
|
365 |
" |
|
366 |
! |
|
367 |
||
135 | 368 |
verticesDo:aBlock |
369 |
"evaluate aBlock for each point" |
|
370 |
||
371 |
vertices do:aBlock |
|
372 |
||
373 |
" |
|
374 |
|v p| |
|
375 |
||
376 |
v := View new open. |
|
377 |
[v shown] whileFalse:[Processor yield]. |
|
78 | 378 |
|
135 | 379 |
p := Polygon vertices:(Array with:5@5 |
380 |
with:50@5 |
|
381 |
with:30@30 |
|
382 |
with:5@5). |
|
383 |
||
384 |
p displayOn:v. |
|
385 |
(Delay forSeconds:3) wait. |
|
386 |
||
387 |
p verticesDo:[:p | v displayRectangleX:p x -3 y:p y -3 width:6 height:6] |
|
388 |
" |
|
78 | 389 |
! ! |
390 |
||
135 | 391 |
!Polygon methodsFor:'queries'! |
0 | 392 |
|
393 |
bottom |
|
14 | 394 |
"return the bottom boundary of the polygon, |
395 |
that is the maximum y coordinate of all its points" |
|
396 |
||
78 | 397 |
(vertices size == 0) ifTrue: [^ nil]. |
398 |
^ vertices inject:(vertices at:1) y into:[:maxSoFar :p | maxSoFar max:(p y)] |
|
0 | 399 |
|
78 | 400 |
" |
401 |
(Polygon vertices:( |
|
402 |
Array |
|
403 |
with:10@10 |
|
404 |
with:60@10 |
|
405 |
with:35@60)) bottom |
|
406 |
" |
|
0 | 407 |
! |
408 |
||
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
409 |
computeBounds |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
410 |
"return the smallest enclosing rectangle" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
411 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
412 |
|minX maxX minY maxY t n "{ Class: SmallInteger }" | |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
413 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
414 |
n := vertices size. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
415 |
n == 0 ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
416 |
^ nil "/ mhmh - should we return an empty rectangle here ? |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
417 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
418 |
t := vertices at:1. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
419 |
minX := maxX := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
420 |
minY := maxY := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
421 |
2 to:n do:[:i | |
4080 | 422 |
|x y| |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
423 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
424 |
t := vertices at:i. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
425 |
x := t x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
426 |
y := t y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
427 |
x < minX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
428 |
minX := x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
429 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
430 |
x > maxX ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
431 |
maxX := x. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
432 |
] |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
433 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
434 |
y < minY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
435 |
minY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
436 |
] ifFalse:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
437 |
y > maxY ifTrue:[ |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
438 |
maxY := y. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
439 |
] |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
440 |
]. |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
441 |
]. |
283 | 442 |
^ Rectangle left:minX top:minY right:maxX bottom:maxY. |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
443 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
444 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
445 |
|p| |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
446 |
|
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
447 |
p := (Polygon vertices:( |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
448 |
Array |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
449 |
with:10@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
450 |
with:60@10 |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
451 |
with:35@60)). |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
452 |
p bounds |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
453 |
" |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
454 |
|
283 | 455 |
"Modified: 8.5.1996 / 20:51:42 / cg" |
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
456 |
"Created: 12.2.1997 / 11:44:11 / cg" |
276
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
457 |
! |
e104a0359515
comments & more Geometric functionality
Claus Gittinger <cg@exept.de>
parents:
267
diff
changeset
|
458 |
|
2091 | 459 |
containsPoint:aPoint |
460 |
"return true, if the argument, aPoint is contained in the receiver" |
|
461 |
||
462 |
|angle pPrev p1 p2 angle2D| |
|
463 |
||
464 |
angle2D := [:p1 :p2 | |
|
465 |
"/ Return the angle between two vectors on a plane |
|
466 |
"/ The angle is from vector 1 to vector 2, positive anticlockwise |
|
467 |
"/ The result is between -pi -> pi |
|
468 |
|theta theta1 theta2| |
|
469 |
||
470 |
theta1 := p1 x arcTan2:p1 y. |
|
471 |
theta2 := p2 x arcTan2:p2 y. |
|
472 |
theta := theta2 - theta1. |
|
473 |
[theta > Float pi] whileTrue:[ |
|
474 |
theta := theta - (Float pi * 2) |
|
475 |
]. |
|
476 |
[ theta < (Float pi negated) ] whileTrue:[ |
|
477 |
theta := theta + (Float pi * 2) |
|
478 |
]. |
|
479 |
theta |
|
480 |
]. |
|
481 |
||
482 |
angle := 0. |
|
483 |
||
484 |
pPrev := vertices last. |
|
485 |
vertices do:[:pI | |
|
486 |
p1 := pPrev - aPoint. |
|
487 |
p2 := pI - aPoint. |
|
488 |
angle := angle + (angle2D value:p1 value:p2). |
|
489 |
pPrev := pI. |
|
490 |
]. |
|
491 |
||
492 |
^ angle abs >= Float pi |
|
493 |
||
494 |
" |
|
495 |
|p| |
|
496 |
||
497 |
p := Polygon vertices:(Array |
|
498 |
with:10@10 |
|
499 |
with:30@10 |
|
500 |
with:20@20). |
|
501 |
TestCase assert:(p containsPoint:14@11). |
|
502 |
TestCase assert:(p containsPoint:15@15). |
|
503 |
TestCase assert:(p containsPoint:5@15) not. |
|
504 |
TestCase assert:(p containsPoint:15@5) not. |
|
505 |
TestCase assert:(p containsPoint:30@15) not. |
|
506 |
TestCase assert:(p containsPoint:20@15). |
|
507 |
" |
|
508 |
! |
|
509 |
||
0 | 510 |
left |
14 | 511 |
"return the left boundary of the polygon, |
512 |
that is the minimum x coordinate of all its points" |
|
513 |
||
78 | 514 |
(vertices size == 0) ifTrue: [^ nil]. |
515 |
^ vertices inject:(vertices at:1) x into:[:minSoFar :p | minSoFar min:(p x)] |
|
0 | 516 |
|
78 | 517 |
" |
518 |
(Polygon vertices:( |
|
519 |
Array |
|
520 |
with:10@10 |
|
521 |
with:60@10 |
|
522 |
with:35@60)) left |
|
523 |
" |
|
524 |
||
0 | 525 |
! |
526 |
||
527 |
right |
|
14 | 528 |
"return the right boundary of the polygon, |
88 | 529 |
that is the maximum x coordinate of all its points" |
14 | 530 |
|
78 | 531 |
(vertices size == 0) ifTrue: [^ nil]. |
532 |
^ vertices inject:(vertices at:1) x into:[:maxSoFar :p | maxSoFar max:(p x)] |
|
533 |
||
534 |
" |
|
535 |
(Polygon vertices:( |
|
536 |
Array |
|
537 |
with:10@10 |
|
538 |
with:60@10 |
|
539 |
with:35@60)) right |
|
540 |
" |
|
541 |
||
135 | 542 |
! |
78 | 543 |
|
135 | 544 |
top |
545 |
"return the top boundary of the polygon, |
|
546 |
that is the minimum y coordinate of all its points" |
|
78 | 547 |
|
135 | 548 |
(vertices size == 0) ifTrue: [^ nil]. |
549 |
^ vertices inject:(vertices at:1) y into:[:minSoFar :p | minSoFar min:(p y)] |
|
78 | 550 |
|
551 |
" |
|
552 |
(Polygon vertices:( |
|
553 |
Array |
|
554 |
with:10@10 |
|
555 |
with:60@10 |
|
135 | 556 |
with:35@60)) top |
78 | 557 |
" |
558 |
||
135 | 559 |
! ! |
78 | 560 |
|
2158 | 561 |
!Polygon methodsFor:'testing'! |
562 |
||
563 |
canBeFilled |
|
564 |
"return true, if the receiver can be drawn as a filled geometric. |
|
565 |
Always true here." |
|
566 |
||
567 |
^ true |
|
568 |
||
569 |
"Created: 8.5.1996 / 08:16:53 / cg" |
|
570 |
! ! |
|
571 |
||
135 | 572 |
!Polygon class methodsFor:'documentation'! |
78 | 573 |
|
135 | 574 |
version |
4080 | 575 |
^ '$Header$' |
2317 | 576 |
! |
577 |
||
578 |
version_CVS |
|
4080 | 579 |
^ '$Header$' |
0 | 580 |
! ! |
4080 | 581 |