Arrow.st
author Claus Gittinger <cg@exept.de>
Sun, 12 May 1996 23:41:46 +0200
changeset 321 49c7fd30093c
parent 320 14e3290c1c0a
child 322 38c88d4b3976
permissions -rw-r--r--
kludge for position of 0

"
 COPYRIGHT (c) 1996 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"





LineSegment subclass:#Arrow
	instanceVariableNames:'arrowHeadPosition arrowHeadLength arrowHeadAngle'
	classVariableNames:'DefaultLength DefaultAngle'
	poolDictionaries:''
	category:'Graphics-Geometry'
!

!Arrow class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"




!

documentation
"
    Arrows are just what the name says - a directed LineSegment, which
    draws itself with an arrowHead. The position of the arrowhead can
    be set to be anywhere along the lineSegement (default is at the end).

    [author:]
        Claus Gittinger

    [see also:]
        Rectangle Polygon EllipticalArc Circle Spline Curve Point 
        LineSegment
        GraphicsContext
        StrokingWrapper
"
!

examples
"
  low level use:
                                                                        [exBegin]
    |v a|

    v := (View extent:100@100) openAndWait.

    a := Arrow from:10@10 to:90@90. 

    v paint:Color red.
    a displayStrokedOn:v.

    a start:90@10 end:10@90.
    v paint:Color blue.
    a displayStrokedOn:v.
                                                                        [exEnd]

  as component (automatic redraw):
                                                                        [exBegin]
    |v a|

    v := View extent:100@100.

    a := Arrow from:10@10 to:90@90. 
    v addComponent:(StrokingWrapper on:a).

    a := Arrow from:10@10 to:90@10.
    a arrowHeadPosition:0.5.
    v addComponent:(StrokingWrapper on:a).

    a := Arrow from:90@10 to:10@90. 
    v addComponent:((StrokingWrapper on:a) foregroundColor:(Color red)).

    a := Arrow from:10@50 to:90@50. 
    a arrowHeadLength:20; arrowHeadAngle:130.

    v addComponent:((StrokingWrapper on:a) 
                        lineWidth:5;
                        foregroundColor:(Color red)).

    a := Arrow from:50@90 to:50@10. 
    a arrowHeadLength:10; arrowHeadAngle:170.

    v addComponent:((StrokingWrapper on:a) 
                        lineWidth:2;
                        lineStyle:#dashed;
                        foregroundColor:(Color red);
                        backgroundColor:(Color yellow)).

    v open.
                                                                        [exEnd]
  varying the position:
                                                                        [exBegin]
    |v a|

    v := View extent:100@100.

    a := Arrow from:10@20 to:90@20. 
    a arrowHeadPosition:0.
    v addComponent:(StrokingWrapper on:a).

    a := Arrow from:10@30 to:90@30. 
    a arrowHeadPosition:(1/3).
    v addComponent:(StrokingWrapper on:a).

    a := Arrow from:10@40 to:90@40. 
    a arrowHeadPosition:0.5.
    v addComponent:(StrokingWrapper on:a).

    a := Arrow from:10@50 to:90@50. 
    a arrowHeadPosition:(2/3).
    v addComponent:(StrokingWrapper on:a).

    a := Arrow from:10@60 to:90@60. 
    v addComponent:(StrokingWrapper on:a).

    v open.
                                                                        [exEnd]
"
! !

!Arrow class methodsFor:'initialization'!

initialize
    DefaultAngle := 150.
    DefaultLength := 8

    "
     Arrow initialize
    "
! !

!Arrow class methodsFor:'accessing defaults'!

defaultAngle
    ^ DefaultAngle
!

defaultLength
    ^ DefaultLength
! !

!Arrow methodsFor:'accessing'!

arrowHeadAngle
    "return the arrowHeads angle, in degrees.
     The default is 150 degrees"

    arrowHeadAngle isNil ifTrue:[^ DefaultAngle].
    ^ arrowHeadAngle

    "Modified: 12.5.1996 / 22:48:29 / cg"
    "Created: 12.5.1996 / 22:56:36 / cg"
!

arrowHeadAngle:angleInDegrees
    "set the arrowHeads angle, in degrees.
     The default is 150 degrees"

    arrowHeadAngle := angleInDegrees

    "Modified: 12.5.1996 / 22:48:29 / cg"
    "Created: 12.5.1996 / 22:57:47 / cg"
!

arrowHeadLength
    "return the arrowHeads length, in pixels.
     The default is 8 pixels"

    arrowHeadPosition isNil ifTrue:[^ DefaultLength].
    ^ arrowHeadLength

    "Created: 12.5.1996 / 22:47:32 / cg"
    "Modified: 12.5.1996 / 22:57:27 / cg"
!

arrowHeadLength:pixels
    "set the arrowHeads length, in pixels.
     The default is 8 pixels"

    arrowHeadLength := pixels

    "Created: 12.5.1996 / 22:47:48 / cg"
    "Modified: 12.5.1996 / 23:01:20 / cg"
!

arrowHeadPosition
    "return the arrowHeads position, as a fraction of the overall length;
     0 is at the beginning, 1 is at the end, 0.5 is in the center."

    arrowHeadPosition isNil ifTrue:[^ 1].
    ^ arrowHeadPosition

    "Created: 12.5.1996 / 22:46:21 / cg"
    "Modified: 12.5.1996 / 22:57:55 / cg"
!

arrowHeadPosition:aFractionOfTheLinesLength
    "set the arrowHeads position, as a fraction of the overall length;
     0 is at the beginning, 1 is at the end, 0.5 is in the center."

    arrowHeadPosition := aFractionOfTheLinesLength

    "Created: 12.5.1996 / 22:46:54 / cg"
    "Modified: 12.5.1996 / 23:01:09 / cg"
! !

!Arrow methodsFor:'displaying'!

displayStrokedOn:aGC
    "display the receiver in the graphicsContext, aGC"

    |arrowHeadPoint len pos angle point grad xDis yDis
     savedLineStyle|

    super displayStrokedOn:aGC.

    savedLineStyle := aGC lineStyle.
    aGC lineStyle:#solid.

    arrowHeadPosition isNil ifTrue:[
        pos := 1
    ] ifFalse:[
        pos := arrowHeadPosition max:0.0001.
    ].

    arrowHeadPoint := startPoint + ((endPoint - startPoint) * pos).

    len := arrowHeadLength.
    len isNil ifTrue:[
        len := DefaultLength
    ].

    angle := arrowHeadAngle.
    angle isNil ifTrue:[
        angle := DefaultAngle
    ].
    angle := angle degreesToRadians.

    point := arrowHeadPoint - startPoint.
    point x = 0
                ifTrue: [grad := 9999999]
                ifFalse: [grad := point y / point x].
    xDis := len * (angle - grad arcTan) cos.
    yDis := len * (angle - grad arcTan) sin.
    point x = 0
                ifTrue: [point y > 0
                        ifTrue: [pos := point x + xDis @ (point y - yDis)]
                        ifFalse: [pos := point x - xDis @ (point y + yDis)]]
                ifFalse: [point x > 0
                        ifTrue: [pos := point x + xDis @ (point y - yDis)]
                        ifFalse: [pos := point x - xDis @ (point y + yDis)]].

    aGC displayLineFrom:arrowHeadPoint to:startPoint + pos.

    xDis := len * (angle + grad arcTan) cos.
    yDis := len * (angle + grad arcTan) sin.
    point x = 0
            ifTrue: [point y > 0
                    ifTrue: [pos := point x + xDis @ (point y + yDis)]
                    ifFalse: [pos := point x - xDis @ (point y - yDis)]]
            ifFalse: [point x > 0
                    ifTrue: [pos := point x + xDis @ (point y + yDis)]
                    ifFalse: [pos := point x - xDis @ (point y - yDis)]].

    aGC displayLineFrom:arrowHeadPoint to:startPoint + pos.

    aGC lineStyle:savedLineStyle.

    "
     |v|

     v := View new openAndWait.

     (Arrow from:10@10 to:50@50) displayStrokedOn:v
    "

    "Created: 12.5.1996 / 22:54:11 / cg"
    "Modified: 12.5.1996 / 23:39:02 / cg"
! !

!Arrow class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/Arrow.st,v 1.3 1996-05-12 21:41:46 cg Exp $'
! !
Arrow initialize!