struct

CrImage::Rectangle

Inherits Struct / Value / Object

A Rectangle is an axis-aligned rectangle on the integer grid.

Defined by two points:

  • min : Top-left corner (inclusive)
  • max : Bottom-right corner (exclusive)

Properties:

  • A Rectangle has no intrinsic color
  • Adding a Point translates the Rectangle
  • Rectangles can exist in any quadrant (not restricted to bottom-right)
  • Intersecting two Rectangles yields another Rectangle (possibly empty)
  • Passed and returned by value for efficiency

Example:

r = CrImage.rect(10, 20, 100, 200) # x0, y0, x1, y1
r2 = r + CrImage.point(50, 50)     # Translate
intersection = r.intersect(r2)

Constructors

Class methods

zero
Source

Instance methods

+(p : Point)
Source
-(p : Point)
Source
at(x : Int32, y : Int32) : Color::Color

at implements the Image module

Source
bounds

bounds implements the Image module

Source
canon

Returns the canonical (well-formed) version of this rectangle.

Swaps minimum and maximum coordinates if necessary to ensure min < max. This is useful when creating rectangles from user input where the order of coordinates might be reversed.

Source
clone
Source
empty

Reports whether the rectangle contains no points.

A rectangle is empty if min.x >= max.x or min.y >= max.y.

Source
eq(s : Rectangle)

Reports whether this and Rectangle s contain the same set of points.

All empty rectangles are considered equal.

Source
height
Source
in(s : Rectangle)

Reports whether every point in this rectangle is contained in s.

Note: max is an exclusive bound, so self.in(s) does not require that self.max.in(s).

Source
inset(n : Int32)

Returns a rectangle inset by n pixels on all sides.

The parameter n can be:

  • Positive: Shrinks the rectangle inward
  • Negative: Expands the rectangle outward

If either dimension is less than 2*n, returns an empty rectangle near the center of the original.

Example:

r = CrImage.rect(10, 10, 100, 100)
inner = r.inset(5)  # Shrink by 5 pixels on each side
outer = r.inset(-5) # Expand by 5 pixels on each side
Source
intersect(s : Rectangle)

Returns the largest rectangle contained by both this and s.

If the two rectangles do not overlap, returns a zero rectangle. This is useful for clipping operations.

Example:

r1 = CrImage.rect(0, 0, 100, 100)
r2 = CrImage.rect(50, 50, 150, 150)
overlap = r1.intersect(r2) # rect(50, 50, 100, 100)
Source
max=(max : Point)
Source
min=(min : Point)
Source
overlaps(s : Rectangle)

Reports whether this and s have a non-empty intersection.

Returns true if the rectangles overlap in any way.

Source
size

size returns width and height

Source
to_s(io : IO) : Nil

Same as #inspect(io).

Source
to_s

Returns a nicely readable and concise string representation of this object, typically intended for users.

This method should usually not be overridden. It delegates to #to_s(IO) which can be overridden for custom implementations.

Also see #inspect.

Source
union(s : Rectangle) : Rectangle

Returns the smallest rectangle that contains both this and s.

This is the bounding box of the two rectangles.

Example:

r1 = CrImage.rect(0, 0, 50, 50)
r2 = CrImage.rect(100, 100, 150, 150)
bounding = r1.union(r2) # rect(0, 0, 150, 150)
Source
width
Source