struct
Eagle::Rect
- Eagle::Rect
- Struct
- Value
- Object
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.crConstructors
-
.centered(center : Vec2, size : Vec2) : Rect
Creates a rect of size centered on center.
-
.from_bounds(min : Vec2, max : Vec2) : Rect
Creates a rect spanning from the min corner to the max corner.
-
.new(x : Number, y : Number, w : Number, h : Number)
Creates a rect from its top-left corner and size.
-
.new(pos : Vec2, size : Vec2)
Creates a rect from a position and a size.
-
.new
Creates an empty rect at the origin.
Instance Method Summary
-
#==(o : Rect) : Bool
Exact equality.
-
#area : Float32
w * h. -
#bottom : Float32
The y of the bottom edge (
y + h). -
#center : Vec2
The center point.
-
#contains?(p : Vec2) : Bool
True when the point is inside.
-
#contains?(o : Rect) : Bool
True when o lies entirely inside this rect.
-
#empty? : Bool
True when the width or height is zero or negative.
-
#grow(amount : Number) : Rect
The rect expanded by amount on every side.
-
#h : Float32
Height.
-
#h=(h : Float32)
Height.
-
#intersection(o : Rect) : Rect
The overlapping area, or an empty rect when they don't overlap.
-
#intersects?(o : Rect) : Bool
True when the rects overlap.
-
#left : Float32
The x of the left edge.
-
#max : Vec2
The bottom-right corner.
-
#min : Vec2
The top-left corner.
-
#position : Vec2
The top-left corner.
-
#right : Float32
The x of the right edge (
x + w). -
#scale(s : Number) : Rect
Position and size multiplied by s.
-
#size : Vec2
(w, h). -
#to_s(io : IO) : Nil
Same as
#inspect(io). -
#top : Float32
The y of the top edge.
-
#translate(v : Vec2) : Rect
The rect moved by v.
-
#union(o : Rect) : Rect
The smallest rect containing both.
-
#w : Float32
Width.
-
#w=(w : Float32)
Width.
-
#x : Float32
Left edge.
-
#x=(x : Float32)
Left edge.
-
#y : Float32
Top edge.
-
#y=(y : Float32)
Top edge.
Constructor Detail
Creates a rect spanning from the min corner to the max corner.
Creates a rect from its top-left corner and size.
Instance Method Detail
True when the point is inside. Left and top edges count as inside; right and bottom don't.
The rect expanded by amount on every side. A negative amount shrinks it.