author | Stefan Vogel <sv@exept.de> |
Fri, 20 Mar 2020 13:01:09 +0100 | |
changeset 5469 | d78065ee4cff |
parent 2154 | 0c5cc2edf99c |
permissions | -rw-r--r-- |
328 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
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 |
" |
|
1303 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
328 | 14 |
Spline subclass:#ArrowedSpline |
15 |
instanceVariableNames:'arrowHeadPositions arrowHeadLength arrowHeadAngle' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
1303 | 18 |
category:'Graphics-Geometry-Objects' |
328 | 19 |
! |
20 |
||
21 |
!ArrowedSpline class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
ArrowedSplines are like infilled splines, with arrowHeads. |
|
41 |
||
42 |
[see also:] |
|
43 |
Polygon LineSegment Circle EllipticalArc |
|
44 |
Rectangle Curve Arrow Spline |
|
45 |
GraphicsContext |
|
331 | 46 |
StrokingWrapper FillingWrapper |
328 | 47 |
|
48 |
[author:] |
|
49 |
Claus Gittinger |
|
50 |
" |
|
51 |
! |
|
52 |
||
53 |
examples |
|
54 |
" |
|
55 |
arrowedspline: |
|
56 |
[exBegin] |
|
57 |
|v a| |
|
58 |
||
59 |
v := View extent:100@100. |
|
60 |
||
61 |
a := ArrowedSpline controlPoints: |
|
62 |
(Array with:(20@20) |
|
63 |
with:(80@80) |
|
64 |
with:(20@80)). |
|
65 |
||
66 |
v addComponent:((StrokingWrapper on:a) foregroundColor:Color red). |
|
67 |
v open. |
|
68 |
[exEnd] |
|
69 |
||
70 |
filled arrow: |
|
71 |
[exBegin] |
|
72 |
|v a| |
|
73 |
||
74 |
v := View extent:100@100. |
|
75 |
||
76 |
a := ArrowedSpline controlPoints: |
|
77 |
(Array with:(20@20) |
|
78 |
with:(80@80) |
|
79 |
with:(20@50) |
|
80 |
with:(90@10)). |
|
81 |
||
82 |
v addComponent:((FillingWrapper on:a) foregroundColor:Color red). |
|
83 |
v open. |
|
84 |
[exEnd] |
|
85 |
||
86 |
more arrowHeads: |
|
87 |
[exBegin] |
|
88 |
|v a| |
|
89 |
||
90 |
v := View extent:100@100. |
|
91 |
||
92 |
a := ArrowedSpline controlPoints: |
|
93 |
(Array with:(20@20) |
|
94 |
with:(80@80) |
|
95 |
with:(20@50) |
|
96 |
with:(90@10)). |
|
97 |
a arrowHeadPositions:#(1 4). |
|
98 |
v addComponent:((FillingWrapper on:a) foregroundColor:Color red). |
|
99 |
v open. |
|
100 |
[exEnd] |
|
101 |
interactive: |
|
102 |
[exBegin] |
|
103 |
|v points eventCatcher| |
|
104 |
||
105 |
v := StandardSystemView extent:(450 @ 450). |
|
106 |
v label:'ArrowedSpline Example - (click left/middle)'. |
|
107 |
||
108 |
points := OrderedCollection new. |
|
109 |
v openAndWait. |
|
110 |
||
111 |
eventCatcher := Plug new. |
|
112 |
eventCatcher respondTo:#handlesButtonPress:inView: |
|
113 |
with:[:butt :view | true]. |
|
114 |
eventCatcher respondTo:#buttonPress:x:y:view: |
|
115 |
with:[:butt :x :y :view | |
|
116 |
v paint:(Color white). |
|
117 |
v fillCircle:(x @ y) radius:3. |
|
118 |
points add:(x @ y). |
|
119 |
||
120 |
(butt == 1 or:[butt == #select]) ifFalse:[ |
|
121 |
v paint:(Color white). |
|
122 |
v fillCircle:(x @ y) radius:3. |
|
123 |
||
329 | 124 |
((ArrowedSpline controlPoints:points) |
125 |
arrowHeadPositions:(1 to:points size); |
|
126 |
arrowHeadLength:15) |
|
127 |
displayStrokedOn:v. |
|
328 | 128 |
|
129 |
points := OrderedCollection new. |
|
130 |
] |
|
131 |
]. |
|
132 |
||
133 |
v delegate:(eventCatcher) |
|
134 |
[exEnd] |
|
135 |
" |
|
136 |
! ! |
|
137 |
||
138 |
!ArrowedSpline methodsFor:'accessing'! |
|
139 |
||
140 |
arrowHeadAngle |
|
141 |
"return the arrowHeads angle, in degrees. |
|
142 |
The default is defined in Arrow (150 degrees)" |
|
143 |
||
144 |
arrowHeadAngle isNil ifTrue:[^ Arrow defaultAngle]. |
|
145 |
^ arrowHeadAngle |
|
146 |
||
147 |
"Created: 12.5.1996 / 23:13:27 / cg" |
|
148 |
"Modified: 12.5.1996 / 23:14:15 / cg" |
|
149 |
! |
|
150 |
||
151 |
arrowHeadAngle:angleInDegrees |
|
152 |
"set the arrowHeads angle, in degrees. |
|
153 |
The default is defined in Arrow (150 degrees)" |
|
154 |
||
155 |
arrowHeadAngle := angleInDegrees |
|
156 |
||
157 |
"Created: 12.5.1996 / 23:13:34 / cg" |
|
158 |
"Modified: 12.5.1996 / 23:14:24 / cg" |
|
159 |
! |
|
160 |
||
161 |
arrowHeadLength |
|
162 |
"return the arrowHeads length, in pixels. |
|
163 |
The default is define in Arrow (8 pixels)" |
|
164 |
||
165 |
arrowHeadLength isNil ifTrue:[^ Arrow defaultLength]. |
|
166 |
^ arrowHeadLength |
|
167 |
||
168 |
"Modified: 13.5.1996 / 00:44:30 / cg" |
|
169 |
! |
|
170 |
||
171 |
arrowHeadLength:pixels |
|
172 |
"set the arrowHeads length, in pixels. |
|
173 |
The default is defined in Arrow (8 pixels)" |
|
174 |
||
175 |
arrowHeadLength := pixels |
|
176 |
||
177 |
"Created: 12.5.1996 / 23:15:34 / cg" |
|
178 |
! |
|
179 |
||
180 |
arrowHeadPositions:collectionOfPositions |
|
181 |
"set the arrowHeads positions. Each collections element gives |
|
182 |
theindex of a controlPoint, on which an arrowHead is drawn. |
|
183 |
The default is #(<controlPoints size>) i.e. a single arrowHead |
|
184 |
on the last controlPoint. |
|
185 |
To have arrowHeads on all controlPoints, define arrowHeadPositions |
|
186 |
as (1 to:controlPoints size)" |
|
187 |
||
188 |
arrowHeadPositions := collectionOfPositions |
|
189 |
||
190 |
"Modified: 12.5.1996 / 23:14:24 / cg" |
|
191 |
"Created: 13.5.1996 / 00:47:23 / cg" |
|
192 |
! ! |
|
193 |
||
194 |
!ArrowedSpline methodsFor:'displaying'! |
|
195 |
||
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
196 |
arrowPoints |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
197 |
"helper: return a collection of arrow-points" |
328 | 198 |
|
199 |
|arrowHeadPoint len pos positions angle point grad xDis yDis |
|
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
200 |
savedLineStyle atn x1 x2 y1 y2 p1 p2 points i| |
328 | 201 |
|
389 | 202 |
firstDerivative isNil ifTrue:[ |
203 |
self computeLineSegments |
|
204 |
]. |
|
205 |
||
328 | 206 |
positions := arrowHeadPositions. |
207 |
positions isNil ifTrue:[ |
|
208 |
positions := Array with:(controlPoints size). |
|
209 |
]. |
|
210 |
||
211 |
len := arrowHeadLength. |
|
212 |
len isNil ifTrue:[ |
|
213 |
len := Arrow defaultLength |
|
214 |
]. |
|
215 |
||
216 |
angle := arrowHeadAngle. |
|
217 |
angle isNil ifTrue:[ |
|
218 |
angle := Arrow defaultAngle |
|
219 |
]. |
|
220 |
angle := angle degreesToRadians. |
|
221 |
||
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
222 |
points := Array new:(positions size * 3). |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
223 |
i := 1. |
328 | 224 |
positions do:[:index | |
225 |
| p deriv| |
|
226 |
||
227 |
p := controlPoints at:index. |
|
228 |
deriv := (firstDerivative at:index). |
|
229 |
deriv isNil ifTrue:[ |
|
230 |
deriv := (controlPoints at:index) - (controlPoints at:index-1). |
|
231 |
]. |
|
232 |
||
233 |
deriv x = 0 |
|
234 |
ifTrue: [grad := 9999999] |
|
235 |
ifFalse: [grad := deriv y / deriv x]. |
|
236 |
||
237 |
atn := grad arcTan. |
|
238 |
||
239 |
x1 := len * (angle - atn) cos. |
|
240 |
y1 := len * (angle - atn) sin. |
|
241 |
||
242 |
x2 := len * (angle + atn) cos. |
|
243 |
y2 := len * (angle + atn) sin. |
|
244 |
||
245 |
deriv x = 0 ifTrue: [ |
|
246 |
deriv y > 0 ifTrue: [ |
|
247 |
p1 := x1 @ (0 - y1). |
|
248 |
p2 := x2 @ (y2). |
|
249 |
] ifFalse: [ |
|
250 |
p1 := 0 - x1 @ (y1). |
|
251 |
p2 := 0 - x2 @ (0 - y2). |
|
252 |
] |
|
253 |
] ifFalse: [ |
|
254 |
deriv x > 0 ifTrue:[ |
|
255 |
p1 := x1 @ (0 - y1). |
|
256 |
p2 := x2 @ (y2). |
|
257 |
] ifFalse: [ |
|
258 |
p1 := 0 - x1 @ (y1). |
|
259 |
p2 := 0 - x2 @ (0 - y2) |
|
260 |
] |
|
261 |
]. |
|
262 |
||
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
263 |
points at:i put:p+p1. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
264 |
points at:i+1 put:p. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
265 |
points at:i+2 put:p+p2. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
266 |
i := i + 3 |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
267 |
]. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
268 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
269 |
^ points |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
270 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
271 |
"Created: 13.5.1996 / 00:50:49 / cg" |
389 | 272 |
"Modified: 5.6.1996 / 20:29:17 / cg" |
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
273 |
! |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
274 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
275 |
displayArrowsOn:aGC filled:filled |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
276 |
"display the receiver in the graphicsContext, aGC" |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
277 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
278 |
|arrowPoints savedLineStyle nP "{Class: SmallInteger }" |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
279 |
p1 p p2| |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
280 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
281 |
savedLineStyle := aGC lineStyle. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
282 |
aGC lineStyle:#solid. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
283 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
284 |
arrowPoints := self arrowPoints. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
285 |
nP := arrowPoints size. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
286 |
1 to:nP by:3 do:[:i | |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
287 |
p1 := arrowPoints at:i. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
288 |
p := arrowPoints at:i+1. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
289 |
p2 := arrowPoints at:i+2. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
290 |
|
328 | 291 |
filled ifTrue:[ |
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
292 |
aGC fillPolygon:(Array with:p1 with:p with:p2). |
328 | 293 |
] ifFalse:[ |
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
294 |
aGC displayLineFrom:p to:p1. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
295 |
aGC displayLineFrom:p to:p2. |
328 | 296 |
]. |
297 |
]. |
|
298 |
||
299 |
aGC lineStyle:savedLineStyle. |
|
300 |
||
301 |
"Created: 13.5.1996 / 00:50:49 / cg" |
|
302 |
"Modified: 13.5.1996 / 00:52:17 / cg" |
|
303 |
! |
|
304 |
||
305 |
displayFilledOn:aGC |
|
306 |
"display the receiver in the graphicsContext, aGC" |
|
307 |
||
308 |
super displayStrokedOn:aGC. |
|
309 |
self displayArrowsOn:aGC filled:true |
|
310 |
||
311 |
"Created: 13.5.1996 / 00:51:14 / cg" |
|
312 |
! |
|
313 |
||
314 |
displayStrokedOn:aGC |
|
315 |
"display the receiver in the graphicsContext, aGC" |
|
316 |
||
317 |
super displayStrokedOn:aGC. |
|
318 |
self displayArrowsOn:aGC filled:false |
|
319 |
||
320 |
"Created: 13.5.1996 / 00:51:14 / cg" |
|
321 |
"Modified: 13.5.1996 / 00:52:34 / cg" |
|
322 |
! ! |
|
323 |
||
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
324 |
!ArrowedSpline methodsFor:'queries'! |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
325 |
|
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
326 |
computeBounds |
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
327 |
"return the smallest enclosing rectangle" |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
328 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
329 |
|minX maxX minY maxY| |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
330 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
331 |
minX := maxX := 0. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
332 |
minY := maxY := 0. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
333 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
334 |
self arrowPoints do:[:p | |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
335 |
|x y| |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
336 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
337 |
x := p x. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
338 |
y := p y. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
339 |
minX := minX min:x. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
340 |
maxX := maxX max:x. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
341 |
minY := minY min:y. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
342 |
maxY := maxY max:y. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
343 |
]. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
344 |
^ Rectangle left:minX right:maxX top:minY bottom:maxY |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
345 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
346 |
"Modified: 26.5.1996 / 13:08:48 / cg" |
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
347 |
"Created: 12.2.1997 / 11:42:41 / cg" |
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
348 |
! ! |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
349 |
|
2154 | 350 |
!ArrowedSpline methodsFor:'testing'! |
351 |
||
352 |
canBeFilled |
|
353 |
"return true, if the receiver can be drawn as a filled geometric. |
|
354 |
Always true here. Notice, that only the arrowHeads are filled." |
|
355 |
||
356 |
^ true |
|
357 |
||
358 |
! ! |
|
359 |
||
328 | 360 |
!ArrowedSpline class methodsFor:'documentation'! |
361 |
||
362 |
version |
|
2154 | 363 |
^ '$Header: /cvs/stx/stx/libbasic2/ArrowedSpline.st,v 1.10 2009-06-06 10:12:15 cg Exp $' |
328 | 364 |
! ! |