--- a/Rectangle.st Thu May 12 04:07:15 1994 +0200
+++ b/Rectangle.st Tue May 17 12:09:46 1994 +0200
@@ -22,27 +22,37 @@
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
-Rectangles represent a rectangular area in 2D space.
-
-notice, my implementation does not use origin/corner as instance objects
-but left/top/width/height to save space and allocations. This means, that my
-Rectangles cannot be used to represent Rectangles in a higher than 2D
-space. (i.e. only valid if origin/corner are 2D Points)
-
-(aside from that, you will not see any difference from the outside)
-
-Instance variables:
-
-left <Number> the left coordinate (i.e origin x)
-top <Number> the top coordinate (i.e origin y)
-width <Number> the width of the rectangle
-height <Number> the height of the rectangle
-
-$Header: /cvs/stx/stx/libbasic/Rectangle.st,v 1.7 1994-02-25 13:03:34 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Rectangle.st,v 1.8 1994-05-17 10:08:48 claus Exp $
written 89 by claus
'!
+!Rectangle class methodsFor:'documentation'!
+
+documentation
+"
+ Rectangles represent a rectangular area in 2D space.
+
+ notice, my implementation does not use origin/corner as instance objects
+ but left/top/width/height to save space and allocations. This means, that my
+ Rectangles cannot be used to represent Rectangles in a higher than 2D
+ space. (i.e. only valid if origin/corner are 2D Points)
+
+ (aside from that, you will not see any difference from the outside)
+
+ Instance variables:
+
+ left <Number> the left coordinate (i.e origin x)
+ top <Number> the top coordinate (i.e origin y)
+ width <Number> the width of the rectangle
+ height <Number> the height of the rectangle
+
+ I am not certain, if implementing Rectangle different was a good idea -
+ subclasses may expect things to be different ...
+ Therefore, this may change.
+"
+! !
+
!Rectangle class methodsFor:'instance creation'!
origin:origin corner:corner
@@ -137,32 +147,8 @@
!Rectangle methodsFor:'accessing'!
-top
- "return the y-coordinate of the top-left origin"
-
- ^ top
-!
-
-left
- "return the x-coordinate of the top-left origin"
-
- ^ left
-!
-
-width
- "return the width of the rectangle"
-
- ^ width
-!
-
-height
- "return the height of the rectangle"
-
- ^ height
-!
-
left:newLeft right:right top:newTop bottom:bottom
- "set the rectangle given left, top, right and bottom coordinates"
+ "set the rectangle given left, top, right and bottom coordinates."
left := newLeft.
top := newTop.
@@ -189,10 +175,16 @@
!
origin:aPoint
- "set the top-left origin"
+ "set the top-left origin. The corner remains unchanged."
+
+ |newTop newLeft|
- left := aPoint x.
- top := aPoint y
+ newTop := aPoint y.
+ newLeft := aPoint x.
+ height := height + (top - newTop).
+ width := width + (left - newLeft).
+ left := newLeft.
+ top := newTop
!
corner:aPoint
@@ -215,6 +207,43 @@
height := aNumber - top
!
+left:aNumber
+ "set the left edge"
+
+ width := width + (left - aNumber).
+ left := aNumber
+!
+
+right:aNumber
+ "set the right edge"
+
+ width := aNumber - left
+!
+
+topLeft:aPoint
+ "Set the top and left edges.
+ The bottom right remains unchanged."
+
+ |newTop|
+
+ newTop := aPoint y.
+ height := height + (top - newTop).
+ top := newTop.
+ width := aPoint x - left
+!
+
+topRight:aPoint
+ "Set the top and right edges.
+ The bottom left remains unchanged."
+
+ |newTop|
+
+ newTop := aPoint y.
+ height := height + (top - newTop).
+ top := newTop.
+ width := aPoint x - left
+!
+
origin:origin corner:corner
"set both origin and corner"
@@ -244,56 +273,67 @@
origin
"return the origin"
- ^ Point x:left y:top
-!
-
-left:aNumber
- "set the left edge"
-
- width := width + (left - aNumber).
- left := aNumber
-!
-
-right:aNumber
- "set the right edge"
-
- width := aNumber - left
+ ^ left @ top
!
corner
"return the corner"
- ^ Point x:(left + width) y:(top + height)
+ ^ (left + width) @ (top + height)
+!
+
+width
+ "return the width of the rectangle"
+
+ ^ width
+!
+
+height
+ "return the height of the rectangle"
+
+ ^ height
!
extent
"return the extent"
- ^ Point x:width y:height
+ ^ width @ height
+!
+
+left
+ "return the x-coordinate of the top-left origin"
+
+ ^ left
+!
+
+leftCenter
+ "return the left center point"
+
+ ^ left @ (top + (height // 2))
+!
+
+top
+ "return the y-coordinate of the top-left"
+
+ ^ top
!
topLeft
- "return the top-left point"
+ "return the top-left point - the same as origin"
- ^ Point x:left y:top
+ ^ left @ top
!
topRight
"return the top-right point"
- ^ Point x:(left + width) y:top
+ ^ (left + width) @ top
!
-bottomLeft
- "return the bottom-left point"
+topCenter
+ "return the top center point"
- ^ Point x:left y:(top + height)
-!
-
-bottomRight
- "return the bottom-right point"
-
- ^ Point x:(left + width) y:(top + height)
+ ^ (left + (width // 2)) @ top
!
bottom
@@ -302,33 +342,45 @@
^ (top + height)
!
+bottomLeft
+ "return the bottom-left point"
+
+ ^ left @ (top + height)
+!
+
+bottomRight
+ "return the bottom-right point"
+
+ ^ (left + width) @ (top + height)
+!
+
+bottomCenter
+ "return the bottom center point"
+
+ ^ (left + (width // 2)) @ (top + height)
+!
+
right
"return the x coordinate of the right"
^ (left + width)
!
+rightCenter
+ "return the right center point"
+
+ ^ (left + width) @ (top + (height // 2))
+!
+
center
"return the point in the center of the receiver"
- ^ Point x:(left + (width // 2)) y:(top + (height // 2))
-!
-
-leftCenter
- "return the left center point"
-
- ^ Point x:left y:(top + (height // 2))
-!
-
-rightCenter
- "return the right center point"
-
- ^ Point x:(left + width) y:(top + (height // 2))
+ ^ (left + (width // 2)) @ (top + (height // 2))
!
area
- "return the area (- for screen Rectangles this is the number
- of pixels)"
+ "return the area
+ - for screen Rectangles this is the number of pixels"
^ width * height
! !
@@ -376,6 +428,14 @@
+ ((width hash) bitXor:(height hash))
! !
+!Rectangle methodsFor:'queries'!
+
+isRectangle
+ "return true, if the receiver is some kind of rectangle"
+
+ ^ true
+! !
+
!Rectangle methodsFor:'testing'!
containsPoint:aPoint
@@ -474,27 +534,81 @@
width:(width rounded) height:(height rounded)
!
-expandBy:amount
+expandBy:delta
"return a new rectangle which is expanded in all directions
- by amount, aPoint or Number"
+ by amount, a Point, Rectangle or Number"
+
+ |amountPoint deltaLeft deltaTop deltaWidth deltaHeight|
- |amountPoint|
+ delta isNumber ifTrue:[
+ deltaLeft := deltaTop := delta.
+ deltaWidth := deltaHeight := delta * 2.
+ ] ifFalse:[
+ delta isRectangle ifTrue:[
+ deltaLeft := delta left.
+ deltaTop := delta top.
+ deltaWidth := deltaLeft + delta right.
+ deltaHeight := deltaTop + delta bottom
+ ] ifFalse:[
+ amountPoint := delta asPoint.
+ deltaLeft := amountPoint x.
+ deltaTop := amountPoint y.
+ deltaWidth := deltaLeft * 2.
+ deltaHeight := deltaTop * 2.
+ ]
+ ].
- amountPoint := amount asPoint.
- ^ Rectangle left:(left - amountPoint x) top:(top - amountPoint y)
- width:(width + (2 * amountPoint x))
- height:(height + (2 * amountPoint y))
+ ^ Rectangle left:(left - deltaLeft)
+ top:(top - deltaTop)
+ width:(width + deltaWidth)
+ height:(height + deltaHeight)
+
+ "
+ |r|
+ r := Rectangle origin:10@10 corner:100@100.
+ r expandBy:5.
+ r expandBy:(5 @ 0).
+ r expandBy:(10 @ 10).
+ r expandBy:( 10@10 corner:20@20 )
+ "
!
insetBy: delta
"return a new rectangle which is inset in all directions
- by delta, aPoint or Rectangle"
+ by delta, a Point, Rectangle or Number"
- | newrect |
+ |amountPoint deltaLeft deltaTop deltaWidth deltaHeight|
- newrect := delta asRectangle.
- ^Rectangle origin: (self origin + (newrect origin))
- corner: (self corner - (newrect corner))
+ delta isNumber ifTrue:[
+ deltaLeft := deltaTop := delta.
+ deltaWidth := deltaHeight := delta * 2.
+ ] ifFalse:[
+ delta isRectangle ifTrue:[
+ deltaLeft := delta left.
+ deltaTop := delta top.
+ deltaWidth := deltaLeft + delta right.
+ deltaHeight := deltaTop + delta bottom
+ ] ifFalse:[
+ amountPoint := delta asPoint.
+ deltaLeft := amountPoint x.
+ deltaTop := amountPoint y.
+ deltaWidth := deltaLeft * 2.
+ deltaHeight := deltaTop * 2.
+ ]
+ ].
+
+ ^ Rectangle left:(left + deltaLeft)
+ top:(top + deltaTop)
+ width:(width - deltaWidth)
+ height:(height - deltaHeight)
+ "
+ |r|
+ r := Rectangle origin:10@10 corner:100@100.
+ r insetBy:5.
+ r insetBy:(5 @ 0).
+ r insetBy:(10 @ 10).
+ r insetBy:( 10@10 corner:20@20 )
+ "
!
translateBy:amount
@@ -615,16 +729,21 @@
corner: (Point x: self right y: iRect bottom).
collect add: tmp.
iRect _ iRect merge: tmp].
- (((iRect left) ~= self left) || [(iRect top) ~= self top])
+ (((iRect left) ~= self left) or: [(iRect top) ~= self top])
ifTrue: "whole top part can be taken now"
[tmp _ Rectangle origin: self origin corner: iRect topRight.
collect add: tmp].
- (((iRect right) ~= self right) || [(iRect bottom) ~= self bottom])
+ (((iRect right) ~= self right) or: [(iRect bottom) ~= self bottom])
ifTrue: "whole bottom open and can be taken"
[tmp _ Rectangle origin: iRect bottomLeft corner: self corner.
collect add: tmp].
^collect
+!
+nonIntersections:aRectangle
+ "this is the same as areasOutside: - for ST/V compatibility only"
+
+ ^ self areasOutside:aRectangle
! !
!Rectangle methodsFor:'printing'!