|
Class: Rectangle
Object
|
+--Geometric
|
+--Rectangle
- Package:
- stx:libbasic
- Category:
- Graphics-Geometry-Objects
- Version:
- rev:
1.134
date: 2024/03/11 14:24:28
- user: cg
- file: Rectangle.st directory: libbasic
- module: stx stc-classLibrary: libbasic
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.
copyrightCOPYRIGHT (c) 1989 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.
instance creation
-
center: centerPoint extent: extentPoint
-
return an instance of me whose center is centerPoint and width
by height is extentPoint.
-
decodeFromLiteralArray: anArray
-
create & return a new instance from information encoded in anArray.
Redefined for faster creation.
Usage example(s):
Rectangle decodeFromLiteralArray:#(Rectangle 100 200 300 500)
|
-
encompassing: listOfPoints
-
Return a rectangle, which encompasses all of the given points.
Usage example(s):
Rectangle encompassing:(Array with:10@10 with:50@0 with:0@50)
|
-
left: left right: right top: top bottom: bottom
-
create and return a new Rectangle giving left, top, right and bottom coordinates
-
left: left top: top extent: extent
-
create and return a new Rectangle giving left, top, and
an extent point.
-
left: left top: top right: right bottom: bottom
-
create and return a new Rectangle giving left, top, right and bottom coordinates
-
left: left top: top width: w height: h
-
create and return a new Rectangle giving left and top coordinates
and width, height
-
merging: listOfRects
-
return the merge of a number of rectangles.
That is a rectangle, which encompasses all of the given rectangles.
Avoids creation of temp garbage.
-
origin: origin corner: corner
-
create and return a new Rectangle giving top-left and bottom-right points
-
origin: origin extent: extent
-
create and return a new Rectangle giving top-left point and extent point
-
origin: origin width: w height: h
-
create and return a new Rectangle giving top-left and extent
as individual width/height
-
vertex: vertex1Point vertex: vertex2Point
-
create and return a new instance of the receiver,
given two diagonally opposite vertices.
instance creation-interactive
-
fromUser
-
let user specify a rectangle on the screen, return it.
If the user presses ESC, an AbortOperationRequest is raised.
Usage example(s):
-
originFromUser: extent
-
let user specify an origin on the screen, return it
Usage example(s):
Rectangle originFromUser:50@50
|
Compatibility-Squeak
-
amountToTranslateWithin: aRectangle
-
Answer a Point, delta, such that self + delta is forced within aRectangle.
-
isTall
-
return true, if the receiver is higher then its width
-
isWide
-
return true, if the receiver is wider then its height
-
translatedToBeWithin: aRectangle
-
Answer a copy of the receiver that does not extend beyond aRectangle. 7/8/96 sw
-
withHeight: newHeight
-
Return a copy of me with a different height
-
withWidth: newWidth
-
Return a copy of me with a different width
accessing
-
area
-
return the area
- for screen Rectangles this is the number of pixels
-
bottom
-
return the y coordinate of the bottom.
For integer-coordinate rectangles (view areas),
that is the y-coordinate of the corner point,
which is the y coordinate just outside the rectangle
-
bottom: aNumber
-
set the bottom edge.
Warning: this is a destructive operation
-
bottomCenter
-
return the bottom center point
-
bottomLeft
-
return the bottom-left point
-
bottomRight
-
return the bottom-right point
-
center
-
return the point in the center of the receiver
-
corner
-
return the corner
Usage example(s):
(self origin:0@0 corner:100@100) corner
(self origin:0@0 corner:100@100) origin
(self origin:5@5 corner:100@100) extent
|
-
corner: aPoint
-
set the bottom-right corner.
Warning: this is a destructive operation
-
extent
-
return the extent
-
extent: aPoint
-
set the extent from the argument, aPoint
with width taken from aPoint x and height taken from aPoint y.
Warning: this is a destructive operation
-
height
-
return the height of the rectangle
-
height: aNumber
-
change the height of the rectangle.
logically, this changes the corner to get the given height.
Warning: this is a destructive operation
-
left
-
return the x-coordinate of the top-left origin
-
left: aNumber
-
set the left edge, adjust width.
Warning: this is a destructive operation
-
left: newLeft right: right top: newTop bottom: bottom
-
set the rectangle given left, top, right and bottom coordinates.
Warning: this is a destructive operation
-
left: newLeft top: newTop extent: extent
-
set the rectangle given left, top, coordinates and an extent.
Warning: this is a destructive operation
-
left: newLeft top: newTop right: right bottom: bottom
-
set the rectangle given left, top, right and bottom coordinates.
Warning: this is a destructive operation
-
left: newLeft top: newTop width: newWidth height: newHeight
-
set the rectangle given left, top coordinates and width, height.
Warning: this is a destructive operation
-
leftCenter
-
return the left center point
-
origin
-
return the origin
-
origin: aPoint
-
set the top-left origin. The corner remains unchanged.
Warning: this is a destructive operation
-
origin: origin corner: corner
-
set both origin and corner.
Warning: this is a destructive operation
-
origin: origin extent: extent
-
set both origin and extent.
Warning: this is a destructive operation
-
origin: origin width: w height: h
-
set both origin and extent;
the extent is given as individual width and height.
Warning: this is a destructive operation
-
right
-
return the x coordinate of the right.
For integer-coordinate rectangles (view areas),
that is the x-coordinate of the corner point,
which is the x coordinate just outside the rectangle
-
right: aNumber
-
set the right edge.
Warning: this is a destructive operation
-
rightCenter
-
return the right center point
-
setLeft: newLeft
-
set left without adjusting width.
Warning: this is a destructive operation
-
setOrigin: newOrigin corner: newCorner
-
set the rectangles dimensions.
Warning: this is a destructive operation
-
setTop: newTop
-
set top without adjusting height.
Warning: this is a destructive operation
-
top
-
return the y-coordinate of the top-left
-
top: aNumber
-
set the top edge, adjust height.
Warning: this is a destructive operation
-
topCenter
-
return the top center point
-
topLeft
-
return the top-left point - the same as origin
-
topLeft: aPoint
-
Set the top and left edges.
The bottom right remains unchanged.
Warning: this is a destructive operation
Usage example(s):
((self origin:0@0 corner:100@100) topLeft:(10@20))
corner
|
-
topRight
-
return the top-right point
-
topRight: aPoint
-
Set the top and right edges.
The bottom left remains unchanged.
Warning: this is a destructive operation
Usage example(s):
((self origin:0@0 corner:100@100) topRight:(10@20))
corner
|
-
vertices
-
return the array containing my points as a closed polygon (for Polygon compatibility)
Usage example(s):
(Rectangle origin:100@100 extent:20@30) vertices
(Rectangle origin:100@100 extent:20@30) asPolygon vertices
|
-
width
-
return the width of the rectangle
-
width: aNumber
-
change the width of the rectangle.
logically, this changes the corner to get the given width.
Warning: this is a destructive operation
-
width: newWidth height: newHeight
-
change the width and height of the rectangle.
logically, this changes the corner to get the given width.
Warning: this is a destructive operation
comparing
-
= aRectangle
-
return true, if the argument aRectangle represents the same
rectangle as the receiver
-
hash
-
return an integer useful for hashing -
redefined since = is redefined here
converting
-
asFractionalLayout
( an extension from the stx:libview2 package )
-
return a layoutFrame in which fractions (top, left, bottom, right)
are taken from corresponding edges of the receiver.
You have to make certain that those are in 0..1.
Usage example(s):
(0.5@0.5 corner:0.75@0.75) asFractionalLayout
(0.5@0.5 corner:0.75@0.75) asOffsetLayout
(0.5@0.5 corner:0.75@0.75) asLayout
(0@0 corner:1@1) asLayout
(0@0 corner:1@1) asFractionalLayout
(0@0 corner:1@1) asOffsetLayout
|
-
asLayout
( an extension from the stx:libview2 package )
-
return a layoutFrame in which offsets (top, left, bottom, right)
are taken from corresponding edges of the receiver.
If all values are between 0.0 .. 1.0, a fractionalLayout is created,
otherwise, an offsetLayout
Usage example(s):
(0.5@0.5 corner:0.75@0.75) asFractionalLayout
(0.5@0.5 corner:0.75@0.75) asOffsetLayout
(0.5@0.5 corner:0.75@0.75) asLayout
(0@0 corner:1@1) asLayout
(0@0 corner:1@1) asFractionalLayout
(0@0 corner:1@1) asOffsetLayout
|
-
asOffsetLayout
( an extension from the stx:libview2 package )
-
return a layoutFrame in which offsets (top, left, bottom, right)
are taken from corresponding edges of the receiver.
You have to make certain that those are in 0..1.
Usage example(s):
(0.5@0.5 corner:0.75@0.75) asFractionalLayout
(0.5@0.5 corner:0.75@0.75) asOffsetLayout
(0.5@0.5 corner:0.75@0.75) asLayout
(10@10 corner:20@20) asFractionalLayout
(10@10 corner:20@20) asOffsetLayout
(10@10 corner:20@20) asLayout
|
-
asPointArray
-
return an array containing my corners (clockwise) and
the origin again as 5th element.
Can be used to convert a rectangle into a polygon.
Usage example(s):
(10@10 corner:100@100) asPointArray
|
-
asPolygon
-
return a polygon from the receiver
Usage example(s):
(10@10 corner:100@100) asPolygon
|
-
asRectangle
-
-
fromLiteralArrayEncoding: encoding
-
read my values from an encoding.
The encoding is supposed to be of the form: (Rectangle orgX orgY cornX cornY)
Usage example(s):
Rectangle new fromLiteralArrayEncoding:#(Rectangle 100 200 300 500)
|
-
literalArrayEncoding
-
encode myself as an array, from which a copy of the receiver
can be reconstructed with #decodeAsLiteralArray.
The encoding is: (Rectangle orgX orgY cornX cornY)
Usage example(s):
Rectangle new fromLiteralArrayEncoding:#(Rectangle 100 200 300 500)
(100@200 corner:300@500) literalArrayEncoding
|
-
rectangleRelativeTo: aRectangle preferred: prefRectHolder
-
compute a displayRectangle, treating the receiver like a
layoutFrame.
This allows rectangles to be used interchangable with Layouts.
Usage example(s):
(10@20 corner:20@30) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50)
(0.5@0.5) rectangleRelativeTo:(0@0 corner:100@100) preferred:(0@0 corner:50@50)
|
destructive rectangle operations
-
expandBy: delta
-
destructively expanded the receiver in all directions
by amount, a Point, Rectangle or Number.
Warning: this is a destructive operation,
it modifying the receiver NOT returning a copy.
You have to be certain to be the exclusive
owner of the receiver to avoid side effects. See also: #expandedBy:
Usage example(s):
|r|
r := Rectangle origin:10@10 corner:100@100.
r expandBy:5.
r := Rectangle origin:10@10 corner:100@100.
r expandBy:(5 @ 0).
r := Rectangle origin:10@10 corner:100@100.
r expandBy:(10 @ 10).
r := Rectangle origin:10@10 corner:100@100.
r expandBy:( 10@10 corner:20@20 )
|
-
moveBy: aPoint
-
translate the rectangle by some distance (keeping extent unchanged).
sorry for the name inconsistency - but GNU-ST named it that way.
Warning: this is a destructive operation
-
moveTo: aPoint
-
translate the rectangle to have its origin at aPoint (keeping extent unchanged).
Warning: this is a destructive operation
-
scaleBy: scale
-
scale the receiver rectangle by scale (a Number or Point).
Warning: this is a destructive operation
it modifies the receiver, not a copy
and should only be used if you know, that you are the exclusive owner
of the receiver. (use scaledBy if in doubt)
Usage example(s):
(Rectangle origin:10@10 corner:50@50) scaleBy:2
|
Usage example(s):
|r1 r2|
r1 := Rectangle origin:10@10 corner:50@50.
r2 := r1 scaleBy:2.
r1
|
-
translateBy: amount
-
translate (i.e. move) the receiver rectangle
by amount, a Point or Number (keeping extent unchanged).
This is destructive (modifies the receiver, not a copy) and
should only be used if you know, that you are the exclusive owner
of the receiver. (use translatedBy if in doubt)
Usage example(s):
(Rectangle origin:10@10 corner:50@50) translateBy:10
|
Usage example(s):
|r1 r2|
r1 := Rectangle origin:10@10 corner:50@50.
r2 := r1 translateBy:10.
r1
|
displaying
-
ascentOn: aGC
-
displayOn: does draw above baseline
-
displayFilledOn: aGC
-
display a filled rectangle as represented by the receiver in
the graphicsContext, aGC
Usage example(s):
|v|
v := View new openAndWaitUntilVisible.
(Rectangle origin:10@10 corner:50@50) displayFilledOn:v
|
-
displayStrokedOn: aGC
-
display an unfilled rectangle as represented by the receiver in
the graphicsContext, aGC
Usage example(s):
|v|
v := View new openAndWaitUntilVisible.
(Rectangle origin:10@10 corner:50@50) displayStrokedOn:v
|
inspecting
-
inspectorValueStringInListFor: anInspector
( an extension from the stx:libtool package )
-
returns a string to be shown in the inspector's list
printing & storing
-
printOn: aStream
-
print the receiver on aStream
-
storeOn: aStream
-
store the receiver on aStream; i.e. print an expression which will
reconstruct the receiver
queries
-
bounds
-
return the smallest enclosing rectangle
-
computeBounds
-
return the smallest enclosing rectangle
-
contains: aRectangle
-
return true, if the argument, aRectangle is equal to or
is contained fully within the receiver
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
containsPoint: aPoint
-
return true, if the argument, aPoint is contained in the receiver
-
containsPointX: x y: y
-
return true, if the point defined by x@y is contained in the receiver.
This is the same as containsPoint:, but can be used if the coordinates
are already available as separate numbers to avoid useless creation of a
temporary point.
-
containsRect: aRectangle
-
return true, if the argument, aRectangle is equal to or
is contained fully within the receiver
-
corners
-
Return an array of corner points
-
dist: aPoint
-
the distance of aPoint to this rectangle.
That is the shortest distance to any of my boundary lines
Usage example(s):
(Rectangle origin:(0@0) corner:(100@100)) dist:110@110 => 14.142135623731
(Rectangle origin:(0@0) corner:(100@100)) dist:110@100 => 10.0
(Rectangle origin:(0@0) corner:(100@100)) dist:100@110 => 10.0
(Rectangle origin:(0@0) corner:(100@100)) dist:50@50 => 10.0
|
-
innerCorners
-
Return an array of inner corner points,
ie, the most extreme pixels included.
Added for Aqueak compatibility.
Usage example(s):
(10@10 corner:100@100) corners
(10@10 corner:100@100) innerCorners
|
-
intersects: aRectangle
-
return true, if the intersection between the argument, aRectangle
and the receiver is not empty
-
isContainedIn: aRectangle
-
return true, if the receiver is fully contained within
the argument, aRectangle
Usage example(s):
|r|
r := Rectangle origin:10@10 corner:100@100.
r isContainedIn: (Rectangle origin:10@10 corner:100@100).
r isContainedIn: (Rectangle origin:11@10 corner:100@100).
r isContainedIn: (Rectangle origin:9@10 corner:100@100).
|
rectangle operations
-
+ aPoint
-
return a new rectangle with same extent as receiver but
origin translated by the argument, aPoint
-
- aPoint
-
return a new rectangle with same extent as receiver but
origin translated by the argument, aPoint
Usage example(s):
Modified (format): / 24-08-2017 / 12:23:53 / cg
|
-
areasOutside: aRectangle
-
Answer an Array of Rectangles comprising the parts of the receiver
not intersecting aRectangle.
That is all areas with no common pixels.
-
areasOutside: aRectangle do: aBlock
-
evaluate aBlock for Rectangles comprising the parts of the receiver
not intersecting aRectangle.
That is all areas with no common pixels.
Usage example(s):
self assert:
((0@0 corner:100@100) areasOutside:(0@0 corner:50@50)) asArray
= { (Rectangle origin:0@50 extent:100@50) . (Rectangle origin:50@0 extent:50@50) }
self assert:
((0@0 corner:150@100) areasOutside:(0@0 corner:100@100)) asArray
= { (Rectangle origin:100@0 extent:50@100) }
self assert:
((0@0 corner:100@150) areasOutside:(0@0 corner:100@100)) asArray
= { (Rectangle origin:0@100 extent:100@50) }
|
-
encompass: aPoint
-
return a Rectangle that contains both the receiver and aPoint.
-
expandedBy: delta
-
return a new rectangle which is expanded in all directions
by amount, a Point, Rectangle or Number
Usage example(s):
|r|
r := Rectangle origin:10@10 corner:100@100.
r expandedBy:5.
r expandedBy:(5 @ 0).
r expandedBy:(10 @ 10).
r expandedBy:( 10@10 corner:20@20 )
|
-
insetBy: delta
-
return a new rectangle which is inset in all directions
by delta, a Point, Rectangle or Number.
With a negative delta, the new rectangle is larger;
otherwise, it will be smaller
Usage example(s):
|r|
r := Rectangle origin:10@10 corner:100@100.
r insetBy:5.
r insetBy:-1.
r insetBy:(5 @ 0).
r insetBy:(10 @ 10).
r insetBy:( 10@10 corner:20@20 ).
r insetBy:( 0@0 corner:-10@0 ).
|
-
insetOriginBy: originDelta cornerBy: cornerDelta
-
return a new rectangle which is inset by originDelta
and cornerDelta; both may be instances of Point or Number
Usage example(s):
|r|
r := Rectangle origin:10@10 corner:100@100.
r insetOriginBy:5 cornerBy:10.
r insetOriginBy:10@5 cornerBy:10.
r insetOriginBy:10 cornerBy:10@5.
r insetOriginBy:10@10 cornerBy:20@20.
|
-
intersect: aRectangle
-
return a new rectangle covering the intersection of the receiver
and the argument, aRectangle (i.e. the area shared by both).
The rectangles must intersect for a valid return
Usage example(s):
|r1 r2|
r1 := Rectangle origin:10@10 corner:100@100.
r2 := Rectangle origin:50@50 corner:150@75.
r1 intersect:r2
|
-
merge: aRectangle
-
return a new rectangle covering both the receiver
and the argument, aRectangle.
That is the smallest rectangle which includes both.
Usage example(s):
(Rectangle origin:10@10 corner:100@100)
merge:(Rectangle origin:20@20 corner:110@110)
(Rectangle origin:10@10 corner:100@100)
merge:(Rectangle origin:20@20 corner:100@100)
|
-
nonIntersections: aRectangle
-
this is the same as areasOutside: - for ST/V compatibility only
-
quickMerge: aRectangle
-
return the receiver if it encloses the given rectangle,
or the merge of the two rectangles if it doesn't.
This method is an optimized version of merge: to reduce extra rectangle creations.
Usage example(s):
(Rectangle origin:10@10 corner:100@100)
quickMerge:(Rectangle origin:20@20 corner:110@110)
(Rectangle origin:10@10 corner:100@100)
quickMerge:(Rectangle origin:20@20 corner:100@100)
|
testing
-
canBeFilled
-
return true, if the receiver can be drawn as a filled geometric.
Always true here.
-
isFinite
-
return true, if all coordinates of the receiver are finite
I.e. neither nil, NaN and not +/-INF.
Compatibility with Point.
-
isRectangle
-
return true, if the receiver is some kind of rectangle
transformations
-
align: offset with: someCoordinate
-
return a new rectangle which is translated (i.e. moved)
such that the point offset in mySelf is placed on someCoordinate.
Usage example(s):
|r|
r := Rectangle origin:10@10 corner:50@50.
r align:(r center) with:100@100.
|
-
normalized
-
return the same rectangle as myself
but ensuring that both width and height are positive
(i.e. possibly with exchanged corner points).
This may be needed by some graphics operations,
especially when drawing to a Windows or X-display
Usage example(s):
(Rectangle origin:50@10 corner:10@50)
(Rectangle origin:50@10 corner:10@50) normalized
(Rectangle origin:50@40 corner:10@10) normalized
|
-
roundedToNearestIntegerPoint
-
return a copy of the receiver, where all points are rounded to ly on the nearest integer coordinate
-
scaledBy: scale
-
return a new rectangle which is the receiver
scaled by scale
Usage example(s):
(Rectangle origin:10@10 corner:50@50) scaledBy:2
|
Usage example(s):
|r1 r2|
r1 := Rectangle origin:10@10 corner:50@50.
r2 := r1 scaledBy:2.
r1
|
-
scaledBy: scale translatedBy: translation
-
return a new rectangle which is translated (i.e. moved)
by translation, aPoint or Number and scaled by scale, aPoint or number.
Usage example(s):
(Rectangle origin:10@10 corner:50@50) scaledBy:2 translatedBy:10
|
-
transformedBy: aMatrix
-
return a new rectangle (or polygon) which is me transformed by aMatrix
-
translatedBy: amount
-
return a new rectangle which is translated (i.e. moved)
by amount, aPoint or Number
Usage example(s):
(Rectangle origin:10@10 corner:50@50) translatedBy:10
|
Usage example(s):
|r1 r2|
r1 := Rectangle origin:10@10 corner:50@50.
r2 := r1 translatedBy:10.
r1
|
truncation & rounding
-
rounded
-
return a copy of the receiver with rounded coordinates.
Return the receiver if its coordinates are already integral.
-
truncated
-
return a Rectangle whose origin and corner have any fractional parts removed.
Return the receiver if its coordinates are already integral.
|