struct Eagle::Rect

Overview

An axis-aligned 2D rectangle: #x, #y for the top-left corner, then #w and #h.

Rects are used for hit areas, UI layout, texture regions, camera limits and quick overlap tests. The right and bottom edges are exclusive, so #contains? treats a point on the right edge as outside, just as pixels work.

player = Rect.new(100, 100, 32, 48)
coin = Rect.centered(v2(120, 110), v2(16, 16))
if player.intersects?(coin)
  # pick it up
end

button = Rect.new(20, 20, 120, 40)
hovered = button.contains?(Input.mouse)
g.rect(button, color: hovered ? Color::YELLOW : Color::GRAY)

Defined in:

eagle/math/rect.cr

Constructors

Instance Method Summary

Constructor Detail

def self.centered(center : Vec2, size : Vec2) : Rect #

Creates a rect of size centered on center.


def self.from_bounds(min : Vec2, max : Vec2) : Rect #

Creates a rect spanning from the min corner to the max corner.


def self.new(x : Number, y : Number, w : Number, h : Number) #

Creates a rect from its top-left corner and size.


def self.new(pos : Vec2, size : Vec2) #

Creates a rect from a position and a size.


def self.new #

Creates an empty rect at the origin.


Instance Method Detail

def ==(o : Rect) : Bool #

Exact equality.


def area : Float32 #

w * h.


def bottom : Float32 #

The y of the bottom edge (y + h).


def center : Vec2 #

The center point.


def contains?(p : Vec2) : Bool #

True when the point is inside. Left and top edges count as inside; right and bottom don't.


def contains?(o : Rect) : Bool #

True when o lies entirely inside this rect.


def empty? : Bool #

True when the width or height is zero or negative.


def grow(amount : Number) : Rect #

The rect expanded by amount on every side. A negative amount shrinks it.


def h : Float32 #

Height.


def h=(h : Float32) #

Height.


def intersection(o : Rect) : Rect #

The overlapping area, or an empty rect when they don't overlap.


def intersects?(o : Rect) : Bool #

True when the rects overlap. Touching edges don't count.


def left : Float32 #

The x of the left edge.


def max : Vec2 #

The bottom-right corner.


def min : Vec2 #

The top-left corner.


def position : Vec2 #

The top-left corner.


def right : Float32 #

The x of the right edge (x + w).


def scale(s : Number) : Rect #

Position and size multiplied by s.


def size : Vec2 #

(w, h).


def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


def top : Float32 #

The y of the top edge.


def translate(v : Vec2) : Rect #

The rect moved by v.


def union(o : Rect) : Rect #

The smallest rect containing both.


def w : Float32 #

Width.


def w=(w : Float32) #

Width.


def x : Float32 #

Left edge.


def x=(x : Float32) #

Left edge.


def y : Float32 #

Top edge.


def y=(y : Float32) #

Top edge.