Struct Rect

2D rectangle

struct Rect;

Constructors

NameDescription
this (x0, y0, x1, y1) constructs rectangle using left, top, right, bottom coordinates
this (pt0, pt1) constructs rectangle using two points - (left, top), (right, bottom) coordinates

Fields

NameTypeDescription
bottom inty coordinate of bottom right corner (non-inclusive)
left intx coordinate of top left corner
right intx coordinate of bottom right corner (non-inclusive)
top inty coordinate of top left corner

Properties

NameTypeDescription
bottomLeft[get] Pointreturns bottom left point of rectangle
bottomRight[get] Pointreturns bottom right point of rectangle
empty[get] boolreturns true if rectangle is empty (right <= left || bottom <= top)
height[get] intreturns height of rectangle (bottom - top)
middle[get] Pointreturns middle point
middlex[get] intreturns average of left, right
middley[get] intreturns average of top, bottom
size[get] Pointreturns size (width, height) in Point
topLeft[get] Pointreturns top left point of rectangle
topRight[get] Pointreturns top right point of rectangel
width[get] intreturns width of rectangle (right - left)

Methods

NameDescription
expand (dx, dy) expand rectangle dimensions
intersect (rc) updates this rect to intersection with rc, returns true if result is non empty
intersects (rc) returns true if this rect has nonempty intersection with rc
isInsideOf (rc) this rectangle is completely inside rc
isPointInside (pt) returns true if point is inside of this rectangle
moveToFit (rc) moves this rect to fit rc bounds, retaining the same size
offset (dx, dy) add offset to horizontal and vertical coordinates
opEquals (rc)
setMax (rc) for all fields, sets this.field to rc.field if rc.field > this.field
shrink (dx, dy) shrink rectangle dimensions

Aliases

NameDescription
moveBy translate rectangle coordinates by (x,y) - add deltax to x coordinates, and deltay to y coordinates

Note

Rect(0,0,20,10) size is 20x10 -- right and bottom sides are non-inclusive -- if you draw such rect, rightmost drawn pixel will be x=19 and bottom pixel y=9