LineSegment.st
author Claus Gittinger <cg@exept.de>
Wed, 08 May 1996 22:16:09 +0200
changeset 287 779c959ab8f4
parent 286 09da374b5438
child 317 7213ea91be61
permissions -rw-r--r--
added #= & hash

"
 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.
"



Geometric subclass:#LineSegment
	instanceVariableNames:'startPoint endPoint'
	classVariableNames:''
	poolDictionaries:''
	category:'Graphics-Geometry'
!

!LineSegment 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
"
    LineSegments represent a line consisting of start and endPoint
    (actually, its a vector, since the direction makes a difference when
     instances are compared using #=).

    [author:]
        Claus Gittinger

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

! !

!LineSegment class methodsFor:'instance creation'!

from:start to:end
    "return a new lineSegment."

    ^ self new start:start end:end

    "Created: 8.5.1996 / 20:40:13 / cg"
!

with:p1 with:p2
    "return a new lineSegment; the smaller point is taken as startPoint."

    p1 < p2 ifTrue:[
        self new start:p1 end:p2
    ].
    ^ self new start:p2 end:p1

    "Created: 8.5.1996 / 20:41:03 / cg"
! !

!LineSegment methodsFor:'accessing'!

end
    "return the endPoint"

    ^ endPoint

    "Created: 8.5.1996 / 20:41:43 / cg"
!

end:aPoint
    "set the endPoint"

    endPoint := aPoint

    "Created: 8.5.1996 / 20:41:54 / cg"
!

start
    "return the startPoint"

    ^ startPoint

    "Created: 8.5.1996 / 20:41:35 / cg"
!

start:aPoint
    "set the startPoint"

    startPoint := aPoint

    "Created: 8.5.1996 / 20:42:07 / cg"
!

start:p1 end:p2
    "set both the startPoint and the endPoint"

    startPoint := p1.
    endPoint := p2.

    "Created: 8.5.1996 / 20:48:32 / cg"
! !

!LineSegment methodsFor:'comparing'!

= aLineSegment
    "return true, if the receiver represents the same lineSegment
     as the argument, aLineSegment"

    aLineSegment species ~~ self species ifTrue:[^ false].
    ^ (startPoint = aLineSegment start
      and:[endPoint = aLineSegment end])

    "Created: 8.5.1996 / 22:13:02 / cg"
    "Modified: 8.5.1996 / 22:14:34 / cg"
!

hash
    "return a number useul for hashing.
     Redefined, since #= is redefined."

    ^ (startPoint hash bitShift:12) bitOr:(endPoint hash)

    "Modified: 8.5.1996 / 22:14:12 / cg"
! !

!LineSegment methodsFor:'converting'!

asPointArray
    "return an array containing my points."

    ^ Array with:startPoint with:endPoint

    "Created: 8.5.1996 / 20:46:08 / cg"
! !

!LineSegment methodsFor:'displaying'!

displayFilledOn:aGC
    "raise an error - a lineSegment cannot be drawn filled"

    self shouldNotImplement

    "Created: 8.5.1996 / 21:04:27 / cg"
!

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

    aGC displayLineFrom:startPoint to:endPoint

    "
     |v|

     v := View new openAndWait.

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

    "Modified: 8.5.1996 / 14:40:53 / cg"
    "Created: 8.5.1996 / 21:05:16 / cg"
! !

!LineSegment methodsFor:'queries'!

bounds
    "return the smallest enclosing rectangle"

    |minX maxX minY maxY|

    minX := startPoint x min:endPoint x.
    maxX := startPoint x max:endPoint x.
    minY := startPoint y min:endPoint y.
    maxY := startPoint y max:endPoint y.

    ^ Rectangle left:minX right:maxX top:minY bottom:maxY

    "
     (LineSegment from:(10@10) to:(90@90)) bounds 
    "

    "Modified: 8.5.1996 / 20:50:03 / cg"
! !

!LineSegment class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/LineSegment.st,v 1.4 1996-05-08 20:16:09 cg Exp $'
! !