Skip to content

struct SF::Rect(T)
inherits Struct #

Utility struct for manipulating 2D axis aligned rectangles

A rectangle is defined by its top-left corner and its size. It is a very simple struct defined for convenience, so its member variables (left, top, width and height) can be accessed directly, just like the vector classes (Vector2 and Vector3).

To keep things simple, SF::Rect doesn't define functions to emulate the properties that are not directly members (such as right, bottom, center, etc.), it rather only provides intersection functions.

SF::Rect uses the usual rules for its boundaries:

  • The left and top edges are included in the rectangle's area
  • The right (left + width) and bottom (top + height) edges are excluded from the rectangle's area

This means that SF::IntRect.new(0, 0, 1, 1) and SF::IntRect.new(1, 1, 1, 1) don't intersect.

SF::Rect is a generic and may be used with any numeric type, but for simplicity the instantiations used by SFML are aliased:

So that you don't have to care about the template syntax.

See also: SF.int_rect, SF.float_rect.

Usage example:

# Define a rectangle, located at (0, 0) with a size of 20x5
r1 = SF.int_rect(0, 0, 20, 5)

# Define another rectangle, located at (4, 2) with a size of 18x10
position = SF.vector2i(4, 2)
size = SF.vector2i(18, 10)
r2 = SF::IntRect.new(position, size)

# Test intersections with the point (3, 1)
r1.contains?(3, 1) #=> true
r2.contains?(3, 1) #=> false

# Test the intersection between r1 and r2
r1.intersects?(r2) #=> (4, 2, 16, 3)

Class methods#

.new(left : T, top : T, width : T, height : T) #

Construct the rectangle from its coordinates

Be careful, the last two parameters are the width and height, not the right and bottom coordinates!

View source

.new(position : Vector2(T), size : Vector2(T)) #

Construct the rectangle from position and size

Be careful, the last parameter is the size, not the bottom-right corner!

View source

.new #

Default constructor: equivalent to new(0, 0, 0, 0)

View source

Methods#

#==(other : self) : Bool #

Returns true if all corresponding coordinates of two rects are equal

View source

#contains?(x : Number, y : Number) : Bool #

Returns true if a point is inside the rectangle's area

View source

#contains?(point : Vector2 | Tuple) : Bool #

Returns true if a point is inside the rectangle's area

View source

#height : T #

Height of the rectangle

View source

#height=(height : T) #

Height of the rectangle

View source

#intersects?(other : Rect(T)) : Rect(T)? #

Check the intersection between two rectangles

Returns the overlapped rectangle or nil if there is no overlap.

View source

#left : T #

Left coordinate of the rectangle

View source

#left=(left : T) #

Left coordinate of the rectangle

View source

#top : T #

Top coordinate of the rectangle

View source

#top=(top : T) #

Top coordinate of the rectangle

View source

#width : T #

Width of the rectangle

View source

#width=(width : T) #

Width of the rectangle

View source