author | Claus Gittinger <cg@exept.de> |
Sun, 26 May 1996 16:32:07 +0200 | |
changeset 359 | 43a2b6857bc6 |
parent 331 | 9ad8e3f94da5 |
child 374 | a77f804c605e |
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 |
" |
|
12 |
||
13 |
||
14 |
Spline subclass:#ArrowedSpline |
|
15 |
instanceVariableNames:'arrowHeadPositions arrowHeadLength arrowHeadAngle' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Graphics-Geometry' |
|
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 |
||
54 |
examples |
|
55 |
" |
|
56 |
arrowedspline: |
|
57 |
[exBegin] |
|
58 |
|v a| |
|
59 |
||
60 |
v := View extent:100@100. |
|
61 |
||
62 |
a := ArrowedSpline controlPoints: |
|
63 |
(Array with:(20@20) |
|
64 |
with:(80@80) |
|
65 |
with:(20@80)). |
|
66 |
||
67 |
v addComponent:((StrokingWrapper on:a) foregroundColor:Color red). |
|
68 |
v open. |
|
69 |
[exEnd] |
|
70 |
||
71 |
filled arrow: |
|
72 |
[exBegin] |
|
73 |
|v a| |
|
74 |
||
75 |
v := View extent:100@100. |
|
76 |
||
77 |
a := ArrowedSpline controlPoints: |
|
78 |
(Array with:(20@20) |
|
79 |
with:(80@80) |
|
80 |
with:(20@50) |
|
81 |
with:(90@10)). |
|
82 |
||
83 |
v addComponent:((FillingWrapper on:a) foregroundColor:Color red). |
|
84 |
v open. |
|
85 |
[exEnd] |
|
86 |
||
87 |
more arrowHeads: |
|
88 |
[exBegin] |
|
89 |
|v a| |
|
90 |
||
91 |
v := View extent:100@100. |
|
92 |
||
93 |
a := ArrowedSpline controlPoints: |
|
94 |
(Array with:(20@20) |
|
95 |
with:(80@80) |
|
96 |
with:(20@50) |
|
97 |
with:(90@10)). |
|
98 |
a arrowHeadPositions:#(1 4). |
|
99 |
v addComponent:((FillingWrapper on:a) foregroundColor:Color red). |
|
100 |
v open. |
|
101 |
[exEnd] |
|
102 |
interactive: |
|
103 |
[exBegin] |
|
104 |
|v points eventCatcher| |
|
105 |
||
106 |
v := StandardSystemView extent:(450 @ 450). |
|
107 |
v label:'ArrowedSpline Example - (click left/middle)'. |
|
108 |
||
109 |
points := OrderedCollection new. |
|
110 |
v openAndWait. |
|
111 |
||
112 |
eventCatcher := Plug new. |
|
113 |
eventCatcher respondTo:#handlesButtonPress:inView: |
|
114 |
with:[:butt :view | true]. |
|
115 |
eventCatcher respondTo:#buttonPress:x:y:view: |
|
116 |
with:[:butt :x :y :view | |
|
117 |
v paint:(Color white). |
|
118 |
v fillCircle:(x @ y) radius:3. |
|
119 |
points add:(x @ y). |
|
120 |
||
121 |
(butt == 1 or:[butt == #select]) ifFalse:[ |
|
122 |
v paint:(Color white). |
|
123 |
v fillCircle:(x @ y) radius:3. |
|
124 |
||
329 | 125 |
((ArrowedSpline controlPoints:points) |
126 |
arrowHeadPositions:(1 to:points size); |
|
127 |
arrowHeadLength:15) |
|
128 |
displayStrokedOn:v. |
|
328 | 129 |
|
130 |
points := OrderedCollection new. |
|
131 |
] |
|
132 |
]. |
|
133 |
||
134 |
v delegate:(eventCatcher) |
|
135 |
[exEnd] |
|
136 |
" |
|
137 |
! ! |
|
138 |
||
139 |
!ArrowedSpline methodsFor:'accessing'! |
|
140 |
||
141 |
arrowHeadAngle |
|
142 |
"return the arrowHeads angle, in degrees. |
|
143 |
The default is defined in Arrow (150 degrees)" |
|
144 |
||
145 |
arrowHeadAngle isNil ifTrue:[^ Arrow defaultAngle]. |
|
146 |
^ arrowHeadAngle |
|
147 |
||
148 |
"Created: 12.5.1996 / 23:13:27 / cg" |
|
149 |
"Modified: 12.5.1996 / 23:14:15 / cg" |
|
150 |
! |
|
151 |
||
152 |
arrowHeadAngle:angleInDegrees |
|
153 |
"set the arrowHeads angle, in degrees. |
|
154 |
The default is defined in Arrow (150 degrees)" |
|
155 |
||
156 |
arrowHeadAngle := angleInDegrees |
|
157 |
||
158 |
"Created: 12.5.1996 / 23:13:34 / cg" |
|
159 |
"Modified: 12.5.1996 / 23:14:24 / cg" |
|
160 |
! |
|
161 |
||
162 |
arrowHeadLength |
|
163 |
"return the arrowHeads length, in pixels. |
|
164 |
The default is define in Arrow (8 pixels)" |
|
165 |
||
166 |
arrowHeadLength isNil ifTrue:[^ Arrow defaultLength]. |
|
167 |
^ arrowHeadLength |
|
168 |
||
169 |
"Modified: 13.5.1996 / 00:44:30 / cg" |
|
170 |
! |
|
171 |
||
172 |
arrowHeadLength:pixels |
|
173 |
"set the arrowHeads length, in pixels. |
|
174 |
The default is defined in Arrow (8 pixels)" |
|
175 |
||
176 |
arrowHeadLength := pixels |
|
177 |
||
178 |
"Created: 12.5.1996 / 23:15:34 / cg" |
|
179 |
! |
|
180 |
||
181 |
arrowHeadPositions:collectionOfPositions |
|
182 |
"set the arrowHeads positions. Each collections element gives |
|
183 |
theindex of a controlPoint, on which an arrowHead is drawn. |
|
184 |
The default is #(<controlPoints size>) i.e. a single arrowHead |
|
185 |
on the last controlPoint. |
|
186 |
To have arrowHeads on all controlPoints, define arrowHeadPositions |
|
187 |
as (1 to:controlPoints size)" |
|
188 |
||
189 |
arrowHeadPositions := collectionOfPositions |
|
190 |
||
191 |
"Modified: 12.5.1996 / 23:14:24 / cg" |
|
192 |
"Created: 13.5.1996 / 00:47:23 / cg" |
|
193 |
! ! |
|
194 |
||
195 |
!ArrowedSpline methodsFor:'displaying'! |
|
196 |
||
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
197 |
arrowPoints |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
198 |
"helper: return a collection of arrow-points" |
328 | 199 |
|
200 |
|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
|
201 |
savedLineStyle atn x1 x2 y1 y2 p1 p2 points i| |
328 | 202 |
|
203 |
positions := arrowHeadPositions. |
|
204 |
positions isNil ifTrue:[ |
|
205 |
positions := Array with:(controlPoints size). |
|
206 |
]. |
|
207 |
||
208 |
len := arrowHeadLength. |
|
209 |
len isNil ifTrue:[ |
|
210 |
len := Arrow defaultLength |
|
211 |
]. |
|
212 |
||
213 |
angle := arrowHeadAngle. |
|
214 |
angle isNil ifTrue:[ |
|
215 |
angle := Arrow defaultAngle |
|
216 |
]. |
|
217 |
angle := angle degreesToRadians. |
|
218 |
||
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
219 |
points := Array new:(positions size * 3). |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
220 |
i := 1. |
328 | 221 |
positions do:[:index | |
222 |
| p deriv| |
|
223 |
||
224 |
p := controlPoints at:index. |
|
225 |
deriv := (firstDerivative at:index). |
|
226 |
deriv isNil ifTrue:[ |
|
227 |
deriv := (controlPoints at:index) - (controlPoints at:index-1). |
|
228 |
]. |
|
229 |
||
230 |
deriv x = 0 |
|
231 |
ifTrue: [grad := 9999999] |
|
232 |
ifFalse: [grad := deriv y / deriv x]. |
|
233 |
||
234 |
atn := grad arcTan. |
|
235 |
||
236 |
x1 := len * (angle - atn) cos. |
|
237 |
y1 := len * (angle - atn) sin. |
|
238 |
||
239 |
x2 := len * (angle + atn) cos. |
|
240 |
y2 := len * (angle + atn) sin. |
|
241 |
||
242 |
deriv x = 0 ifTrue: [ |
|
243 |
deriv y > 0 ifTrue: [ |
|
244 |
p1 := x1 @ (0 - y1). |
|
245 |
p2 := x2 @ (y2). |
|
246 |
] ifFalse: [ |
|
247 |
p1 := 0 - x1 @ (y1). |
|
248 |
p2 := 0 - x2 @ (0 - y2). |
|
249 |
] |
|
250 |
] ifFalse: [ |
|
251 |
deriv x > 0 ifTrue:[ |
|
252 |
p1 := x1 @ (0 - y1). |
|
253 |
p2 := x2 @ (y2). |
|
254 |
] ifFalse: [ |
|
255 |
p1 := 0 - x1 @ (y1). |
|
256 |
p2 := 0 - x2 @ (0 - y2) |
|
257 |
] |
|
258 |
]. |
|
259 |
||
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
260 |
points at:i put:p+p1. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
261 |
points at:i+1 put:p. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
262 |
points at:i+2 put:p+p2. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
263 |
i := i + 3 |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
264 |
]. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
265 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
266 |
^ points |
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 |
"Created: 13.5.1996 / 00:50:49 / cg" |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
269 |
"Modified: 13.5.1996 / 00:52:17 / cg" |
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 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
272 |
displayArrowsOn:aGC filled:filled |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
273 |
"display the receiver in the graphicsContext, aGC" |
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 |
|arrowPoints savedLineStyle nP "{Class: SmallInteger }" |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
276 |
p1 p p2| |
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 |
savedLineStyle := aGC lineStyle. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
279 |
aGC lineStyle:#solid. |
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 |
arrowPoints := self arrowPoints. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
282 |
nP := arrowPoints size. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
283 |
1 to:nP by:3 do:[:i | |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
284 |
p1 := arrowPoints at:i. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
285 |
p := arrowPoints at:i+1. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
286 |
p2 := arrowPoints at:i+2. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
287 |
|
328 | 288 |
filled ifTrue:[ |
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
289 |
aGC fillPolygon:(Array with:p1 with:p with:p2). |
328 | 290 |
] ifFalse:[ |
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
291 |
aGC displayLineFrom:p to:p1. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
292 |
aGC displayLineFrom:p to:p2. |
328 | 293 |
]. |
294 |
]. |
|
295 |
||
296 |
aGC lineStyle:savedLineStyle. |
|
297 |
||
298 |
"Created: 13.5.1996 / 00:50:49 / cg" |
|
299 |
"Modified: 13.5.1996 / 00:52:17 / cg" |
|
300 |
! |
|
301 |
||
302 |
displayFilledOn:aGC |
|
303 |
"display the receiver in the graphicsContext, aGC" |
|
304 |
||
305 |
super displayStrokedOn:aGC. |
|
306 |
self displayArrowsOn:aGC filled:true |
|
307 |
||
308 |
"Created: 13.5.1996 / 00:51:14 / cg" |
|
309 |
! |
|
310 |
||
311 |
displayStrokedOn:aGC |
|
312 |
"display the receiver in the graphicsContext, aGC" |
|
313 |
||
314 |
super displayStrokedOn:aGC. |
|
315 |
self displayArrowsOn:aGC filled:false |
|
316 |
||
317 |
"Created: 13.5.1996 / 00:51:14 / cg" |
|
318 |
"Modified: 13.5.1996 / 00:52:34 / cg" |
|
319 |
! ! |
|
320 |
||
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
321 |
!ArrowedSpline methodsFor:'queries'! |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
322 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
323 |
bounds |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
324 |
"return the smallest enclosing rectangle" |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
325 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
326 |
|minX maxX minY maxY| |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
327 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
328 |
minX := maxX := 0. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
329 |
minY := maxY := 0. |
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 |
self arrowPoints do:[:p | |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
332 |
|x y| |
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 |
x := p x. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
335 |
y := p y. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
336 |
minX := minX min:x. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
337 |
maxX := maxX max:x. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
338 |
minY := minY min:y. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
339 |
maxY := maxY max:y. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
340 |
]. |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
341 |
^ 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
|
342 |
|
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 |
"Created: 26.5.1996 / 13:06:48 / cg" |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
345 |
"Modified: 26.5.1996 / 13:08:48 / cg" |
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
346 |
|
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
347 |
|
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 |
|
328 | 350 |
!ArrowedSpline class methodsFor:'documentation'! |
351 |
||
352 |
version |
|
359
43a2b6857bc6
fixed bounds computation (care for arrows)
Claus Gittinger <cg@exept.de>
parents:
331
diff
changeset
|
353 |
^ '$Header: /cvs/stx/stx/libbasic2/ArrowedSpline.st,v 1.4 1996-05-26 14:32:07 cg Exp $' |
328 | 354 |
! ! |