author | Claus Gittinger <cg@exept.de> |
Wed, 12 Feb 1997 13:23:57 +0100 | |
changeset 490 | ef43c7a69346 |
parent 455 | 9789991dd799 |
child 728 | c7a227ec2897 |
permissions | -rw-r--r-- |
318 | 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 |
" |
|
455
9789991dd799
do not draw the line in the filled part
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
12 |
|
318 | 13 |
LineSegment subclass:#Arrow |
14 |
instanceVariableNames:'arrowHeadPosition arrowHeadLength arrowHeadAngle' |
|
15 |
classVariableNames:'DefaultLength DefaultAngle' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Graphics-Geometry' |
|
18 |
! |
|
19 |
||
20 |
!Arrow class methodsFor:'documentation'! |
|
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
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 |
documentation |
|
37 |
" |
|
38 |
Arrows are just what the name says - a directed LineSegment, which |
|
39 |
draws itself with an arrowHead. The position of the arrowhead can |
|
40 |
be set to be anywhere along the lineSegement (default is at the end). |
|
41 |
||
323 | 42 |
Arrows can be drawn stroked or filled - when filled, only the arrowHead |
43 |
is filled. |
|
44 |
||
318 | 45 |
[author:] |
46 |
Claus Gittinger |
|
47 |
||
48 |
[see also:] |
|
320 | 49 |
Rectangle Polygon EllipticalArc Circle Spline Curve Point |
331 | 50 |
LineSegment ArrowedSpline |
318 | 51 |
GraphicsContext |
331 | 52 |
StrokingWrapper FillingWrapper |
318 | 53 |
" |
54 |
! |
|
55 |
||
56 |
examples |
|
57 |
" |
|
58 |
low level use: |
|
59 |
[exBegin] |
|
60 |
|v a| |
|
61 |
||
62 |
v := (View extent:100@100) openAndWait. |
|
63 |
||
64 |
a := Arrow from:10@10 to:90@90. |
|
65 |
||
66 |
v paint:Color red. |
|
67 |
a displayStrokedOn:v. |
|
68 |
||
69 |
a start:90@10 end:10@90. |
|
70 |
v paint:Color blue. |
|
71 |
a displayStrokedOn:v. |
|
72 |
[exEnd] |
|
73 |
||
325 | 74 |
as component (automatic redraw): |
75 |
[exBegin] |
|
76 |
|v a| |
|
77 |
||
78 |
v := View extent:100@100. |
|
79 |
||
80 |
a := Arrow from:50@50 to:10@10. |
|
81 |
v addComponent:(StrokingWrapper on:a). |
|
82 |
||
83 |
a := Arrow from:50@50 to:90@10. |
|
84 |
v addComponent:(StrokingWrapper on:a). |
|
85 |
||
86 |
a := Arrow from:50@50 to:10@90. |
|
87 |
v addComponent:(StrokingWrapper on:a). |
|
88 |
||
89 |
a := Arrow from:50@50 to:90@90. |
|
90 |
v addComponent:(StrokingWrapper on:a). |
|
91 |
||
92 |
v open |
|
93 |
[exEnd] |
|
94 |
||
95 |
as component filled vs. stroked: |
|
323 | 96 |
[exBegin] |
97 |
|v a| |
|
98 |
||
99 |
v := View extent:100@100. |
|
100 |
||
101 |
a := Arrow from:10@10 to:90@10. |
|
102 |
v addComponent:(StrokingWrapper on:a). |
|
103 |
||
104 |
a := Arrow from:90@20 to:10@20. |
|
105 |
v addComponent:(StrokingWrapper on:a). |
|
106 |
||
107 |
a := Arrow from:10@50 to:90@50. |
|
108 |
v addComponent:(FillingWrapper on:a). |
|
109 |
||
110 |
a := Arrow from:90@60 to:10@60. |
|
111 |
v addComponent:(FillingWrapper on:a). |
|
112 |
||
113 |
v open |
|
114 |
[exEnd] |
|
115 |
||
324 | 116 |
as component (varying lineStyles): |
318 | 117 |
[exBegin] |
118 |
|v a| |
|
119 |
||
120 |
v := View extent:100@100. |
|
121 |
||
122 |
a := Arrow from:10@10 to:90@90. |
|
123 |
v addComponent:(StrokingWrapper on:a). |
|
124 |
||
125 |
a := Arrow from:10@10 to:90@10. |
|
126 |
a arrowHeadPosition:0.5. |
|
127 |
v addComponent:(StrokingWrapper on:a). |
|
128 |
||
129 |
a := Arrow from:90@10 to:10@90. |
|
130 |
v addComponent:((StrokingWrapper on:a) foregroundColor:(Color red)). |
|
131 |
||
132 |
a := Arrow from:10@50 to:90@50. |
|
133 |
a arrowHeadLength:20; arrowHeadAngle:130. |
|
134 |
||
135 |
v addComponent:((StrokingWrapper on:a) |
|
136 |
lineWidth:5; |
|
137 |
foregroundColor:(Color red)). |
|
138 |
||
139 |
a := Arrow from:50@90 to:50@10. |
|
140 |
a arrowHeadLength:10; arrowHeadAngle:170. |
|
141 |
||
142 |
v addComponent:((StrokingWrapper on:a) |
|
143 |
lineWidth:2; |
|
144 |
lineStyle:#dashed; |
|
145 |
foregroundColor:(Color red); |
|
146 |
backgroundColor:(Color yellow)). |
|
147 |
||
148 |
v open. |
|
149 |
[exEnd] |
|
321 | 150 |
varying the position: |
151 |
[exBegin] |
|
152 |
|v a| |
|
153 |
||
323 | 154 |
v := View extent:200@100. |
321 | 155 |
|
323 | 156 |
a := Arrow from:10@10 to:90@10. |
321 | 157 |
a arrowHeadPosition:0. |
158 |
v addComponent:(StrokingWrapper on:a). |
|
159 |
||
160 |
a := Arrow from:10@30 to:90@30. |
|
161 |
a arrowHeadPosition:(1/3). |
|
162 |
v addComponent:(StrokingWrapper on:a). |
|
163 |
||
164 |
a := Arrow from:10@40 to:90@40. |
|
165 |
a arrowHeadPosition:0.5. |
|
166 |
v addComponent:(StrokingWrapper on:a). |
|
167 |
||
168 |
a := Arrow from:10@50 to:90@50. |
|
169 |
a arrowHeadPosition:(2/3). |
|
170 |
v addComponent:(StrokingWrapper on:a). |
|
171 |
||
323 | 172 |
a := Arrow from:10@70 to:90@70. |
173 |
v addComponent:(StrokingWrapper on:a). |
|
174 |
||
175 |
||
176 |
a := Arrow from:100@10 to:150@90. |
|
177 |
a arrowHeadPosition:(1/3). |
|
326 | 178 |
v addComponent:(FillingWrapper on:a). |
323 | 179 |
|
180 |
a := Arrow from:110@10 to:160@90. |
|
181 |
a arrowHeadPosition:(2/3). |
|
326 | 182 |
v addComponent:(FillingWrapper on:a). |
323 | 183 |
|
184 |
a := Arrow from:120@10 to:170@90. |
|
326 | 185 |
v addComponent:(FillingWrapper on:a). |
321 | 186 |
|
187 |
v open. |
|
188 |
[exEnd] |
|
318 | 189 |
" |
190 |
! ! |
|
191 |
||
192 |
!Arrow class methodsFor:'initialization'! |
|
193 |
||
194 |
initialize |
|
195 |
DefaultAngle := 150. |
|
196 |
DefaultLength := 8 |
|
197 |
||
198 |
" |
|
199 |
Arrow initialize |
|
200 |
" |
|
201 |
! ! |
|
202 |
||
321 | 203 |
!Arrow class methodsFor:'accessing defaults'! |
204 |
||
205 |
defaultAngle |
|
206 |
^ DefaultAngle |
|
207 |
! |
|
208 |
||
209 |
defaultLength |
|
210 |
^ DefaultLength |
|
211 |
! ! |
|
212 |
||
318 | 213 |
!Arrow methodsFor:'accessing'! |
214 |
||
215 |
arrowHeadAngle |
|
216 |
"return the arrowHeads angle, in degrees. |
|
217 |
The default is 150 degrees" |
|
218 |
||
219 |
arrowHeadAngle isNil ifTrue:[^ DefaultAngle]. |
|
220 |
^ arrowHeadAngle |
|
221 |
||
222 |
"Modified: 12.5.1996 / 22:48:29 / cg" |
|
223 |
"Created: 12.5.1996 / 22:56:36 / cg" |
|
224 |
! |
|
225 |
||
226 |
arrowHeadAngle:angleInDegrees |
|
227 |
"set the arrowHeads angle, in degrees. |
|
228 |
The default is 150 degrees" |
|
229 |
||
230 |
arrowHeadAngle := angleInDegrees |
|
231 |
||
232 |
"Modified: 12.5.1996 / 22:48:29 / cg" |
|
233 |
"Created: 12.5.1996 / 22:57:47 / cg" |
|
234 |
! |
|
235 |
||
236 |
arrowHeadLength |
|
237 |
"return the arrowHeads length, in pixels. |
|
238 |
The default is 8 pixels" |
|
239 |
||
240 |
arrowHeadPosition isNil ifTrue:[^ DefaultLength]. |
|
241 |
^ arrowHeadLength |
|
242 |
||
243 |
"Created: 12.5.1996 / 22:47:32 / cg" |
|
244 |
"Modified: 12.5.1996 / 22:57:27 / cg" |
|
245 |
! |
|
246 |
||
247 |
arrowHeadLength:pixels |
|
248 |
"set the arrowHeads length, in pixels. |
|
249 |
The default is 8 pixels" |
|
250 |
||
251 |
arrowHeadLength := pixels |
|
252 |
||
253 |
"Created: 12.5.1996 / 22:47:48 / cg" |
|
254 |
"Modified: 12.5.1996 / 23:01:20 / cg" |
|
255 |
! |
|
256 |
||
257 |
arrowHeadPosition |
|
258 |
"return the arrowHeads position, as a fraction of the overall length; |
|
259 |
0 is at the beginning, 1 is at the end, 0.5 is in the center." |
|
260 |
||
261 |
arrowHeadPosition isNil ifTrue:[^ 1]. |
|
262 |
^ arrowHeadPosition |
|
263 |
||
264 |
"Created: 12.5.1996 / 22:46:21 / cg" |
|
265 |
"Modified: 12.5.1996 / 22:57:55 / cg" |
|
266 |
! |
|
267 |
||
268 |
arrowHeadPosition:aFractionOfTheLinesLength |
|
269 |
"set the arrowHeads position, as a fraction of the overall length; |
|
270 |
0 is at the beginning, 1 is at the end, 0.5 is in the center." |
|
271 |
||
272 |
arrowHeadPosition := aFractionOfTheLinesLength |
|
273 |
||
274 |
"Created: 12.5.1996 / 22:46:54 / cg" |
|
275 |
"Modified: 12.5.1996 / 23:01:09 / cg" |
|
276 |
! ! |
|
277 |
||
278 |
!Arrow methodsFor:'displaying'! |
|
279 |
||
354 | 280 |
arrowPoints |
281 |
"return the arrowPoints in a collection" |
|
318 | 282 |
|
333 | 283 |
|arrowHeadPoint len pos angle point grad x1 y1 x2 y2 |
284 |
savedLineStyle atn p1 p2 deriv dX dY| |
|
318 | 285 |
|
286 |
arrowHeadPosition isNil ifTrue:[ |
|
321 | 287 |
pos := 1 |
318 | 288 |
] ifFalse:[ |
321 | 289 |
pos := arrowHeadPosition max:0.0001. |
318 | 290 |
]. |
291 |
||
321 | 292 |
arrowHeadPoint := startPoint + ((endPoint - startPoint) * pos). |
293 |
||
318 | 294 |
len := arrowHeadLength. |
295 |
len isNil ifTrue:[ |
|
296 |
len := DefaultLength |
|
297 |
]. |
|
298 |
||
299 |
angle := arrowHeadAngle. |
|
300 |
angle isNil ifTrue:[ |
|
301 |
angle := DefaultAngle |
|
302 |
]. |
|
303 |
angle := angle degreesToRadians. |
|
304 |
||
333 | 305 |
deriv := arrowHeadPoint - startPoint. |
306 |
dX := deriv x. |
|
307 |
dY := deriv y. |
|
308 |
||
309 |
dX = 0 ifTrue: [ |
|
310 |
grad := 99999999.9 |
|
311 |
] ifFalse: [ |
|
312 |
grad := dY / dX |
|
313 |
]. |
|
322
38c88d4b3976
avoid calling arcTan too often
Claus Gittinger <cg@exept.de>
parents:
321
diff
changeset
|
314 |
|
38c88d4b3976
avoid calling arcTan too often
Claus Gittinger <cg@exept.de>
parents:
321
diff
changeset
|
315 |
atn := grad arcTan. |
333 | 316 |
x1 := len * (angle - atn) cos. |
317 |
y1 := len * (angle - atn) sin. |
|
318 | 318 |
|
333 | 319 |
x2 := len * (angle + atn) cos. |
320 |
y2 := len * (angle + atn) sin. |
|
318 | 321 |
|
333 | 322 |
dX = 0 ifTrue:[ |
323 |
dY > 0 ifTrue:[ |
|
324 |
p1 := (dX + x1) @ (dY - y1). |
|
325 |
p2 := (dX + x2) @ (dY + y2). |
|
326 |
] ifFalse:[ |
|
327 |
p1 := (dX - x1) @ (dY + y1). |
|
328 |
p2 := (dX - x2) @ (dY - y2). |
|
329 |
] |
|
330 |
] ifFalse:[ |
|
331 |
dX > 0 ifTrue:[ |
|
332 |
p1 := (dX + x1) @ (dY - y1). |
|
333 |
p2 := (dX + x2) @ (dY + y2). |
|
334 |
] ifFalse:[ |
|
335 |
p1 := (dX - x1) @ (dY + y1). |
|
336 |
p2 := (dX - x2) @ (dY - y2). |
|
337 |
] |
|
338 |
]. |
|
318 | 339 |
|
333 | 340 |
p1 := startPoint + p1. |
341 |
p2 := startPoint + p2. |
|
323 | 342 |
|
354 | 343 |
^ Array with:p1 with:arrowHeadPoint with:p2. |
344 |
||
345 |
"Created: 12.5.1996 / 23:54:35 / cg" |
|
346 |
"Modified: 13.5.1996 / 20:55:51 / cg" |
|
347 |
! |
|
348 |
||
387 | 349 |
displayFilledOn:aGC |
350 |
"display the receiver in the graphicsContext, aGC" |
|
351 |
||
352 |
self displayOn:aGC filled:true |
|
353 |
||
354 |
"Created: 12.5.1996 / 23:58:42 / cg" |
|
355 |
"Modified: 4.6.1996 / 18:25:11 / cg" |
|
356 |
! |
|
357 |
||
358 |
displayOn:aGC filled:filled |
|
354 | 359 |
"display the receivers arrow in the graphicsContext, aGC" |
360 |
||
455
9789991dd799
do not draw the line in the filled part
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
361 |
|arrowHeadPoint savedLineStyle p1 p2 points middle oldCap| |
354 | 362 |
|
363 |
savedLineStyle := aGC lineStyle. |
|
364 |
aGC lineStyle:#solid. |
|
365 |
||
366 |
points := self arrowPoints. |
|
367 |
||
368 |
p1 := points at:1. |
|
369 |
arrowHeadPoint := points at:2. |
|
370 |
p2 := points at:3. |
|
371 |
||
323 | 372 |
filled ifFalse:[ |
455
9789991dd799
do not draw the line in the filled part
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
373 |
aGC displayLineFrom:startPoint to:endPoint. |
323 | 374 |
aGC displayLineFrom:arrowHeadPoint to:p1. |
375 |
aGC displayLineFrom:arrowHeadPoint to:p2. |
|
376 |
] ifTrue:[ |
|
455
9789991dd799
do not draw the line in the filled part
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
377 |
middle := (p1 + p2 / 2) rounded. |
9789991dd799
do not draw the line in the filled part
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
378 |
oldCap := aGC capStyle. |
9789991dd799
do not draw the line in the filled part
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
379 |
aGC capStyle:#notLast. |
9789991dd799
do not draw the line in the filled part
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
380 |
aGC displayLineFrom:startPoint to:middle. |
323 | 381 |
aGC fillPolygon:(Array with:p1 with:arrowHeadPoint with:p2). |
455
9789991dd799
do not draw the line in the filled part
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
382 |
aGC capStyle:oldCap. |
323 | 383 |
]. |
318 | 384 |
|
385 |
aGC lineStyle:savedLineStyle. |
|
386 |
||
387 |
" |
|
388 |
|v| |
|
389 |
||
390 |
v := View new openAndWait. |
|
391 |
||
392 |
(Arrow from:10@10 to:50@50) displayStrokedOn:v |
|
393 |
" |
|
394 |
||
387 | 395 |
"Modified: 4.6.1996 / 18:24:53 / cg" |
396 |
"Created: 4.6.1996 / 18:25:15 / cg" |
|
323 | 397 |
! |
398 |
||
399 |
displayStrokedOn:aGC |
|
400 |
"display the receiver in the graphicsContext, aGC" |
|
401 |
||
387 | 402 |
self displayOn:aGC filled:false |
323 | 403 |
|
387 | 404 |
"Modified: 4.6.1996 / 18:25:07 / cg" |
318 | 405 |
! ! |
406 |
||
357 | 407 |
!Arrow methodsFor:'queries'! |
408 |
||
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
409 |
canBeFilled |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
410 |
"return true, if the receiver can be drawn as a filled geometric. |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
411 |
Always true here. Notice, that Arrows only fill the arrowHeads." |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
412 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
413 |
^ true |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
414 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
415 |
! |
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
416 |
|
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
417 |
computeBounds |
357 | 418 |
"return the smallest enclosing rectangle" |
419 |
||
358 | 420 |
|x y minX maxX minY maxY| |
357 | 421 |
|
422 |
minX := maxX := startPoint x. |
|
423 |
x := endPoint x. |
|
424 |
minX := minX min:x. |
|
425 |
maxX := maxX max:x. |
|
426 |
||
427 |
minY := maxY := startPoint y. |
|
428 |
y := endPoint y. |
|
429 |
minY := minY min:y. |
|
430 |
maxY := maxY max:y. |
|
431 |
||
432 |
self arrowPoints do:[:p | |
|
358 | 433 |
|x y| |
434 |
||
435 |
x := p x. |
|
436 |
y := p y. |
|
437 |
minX := minX min:x. |
|
438 |
maxX := maxX max:x. |
|
439 |
minY := minY min:y. |
|
440 |
maxY := maxY max:y. |
|
357 | 441 |
]. |
442 |
^ Rectangle left:minX right:maxX top:minY bottom:maxY |
|
443 |
||
444 |
" |
|
445 |
(Arrow from:(10@10) to:(90@90)) bounds |
|
446 |
(Arrow from:(10@10) to:(10@90)) bounds |
|
447 |
" |
|
448 |
||
358 | 449 |
"Modified: 26.5.1996 / 16:31:39 / cg" |
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
450 |
"Created: 12.2.1997 / 11:42:22 / cg" |
357 | 451 |
! ! |
452 |
||
318 | 453 |
!Arrow class methodsFor:'documentation'! |
454 |
||
455 |
version |
|
490
ef43c7a69346
added dummy Scale & InverseScale classVars.
Claus Gittinger <cg@exept.de>
parents:
455
diff
changeset
|
456 |
^ '$Header: /cvs/stx/stx/libbasic2/Arrow.st,v 1.18 1997-02-12 12:23:19 cg Exp $' |
318 | 457 |
! ! |
458 |
Arrow initialize! |