struct SF::Rect(T)

Overview

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:

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)

Defined in:

graphics/graphics.cr

Constructors

Instance Method Summary

Constructor Detail

def self.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]
def self.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]
def self.new #

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


[View source]

Instance Method Detail

def ==(other : self) : Bool #

Returns true if all corresponding coordinates of two rects are equal


[View source]
def contains?(x : Number, y : Number) : Bool #

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


[View source]
def contains?(point : Vector2 | Tuple) : Bool #

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


[View source]
def height : T #

Height of the rectangle


[View source]
def height=(height : T) #

Height of the rectangle


[View source]
def intersects?(other : Rect(T)) : Rect(T) | Nil #

Check the intersection between two rectangles

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


[View source]
def left : T #

Left coordinate of the rectangle


[View source]
def left=(left : T) #

Left coordinate of the rectangle


[View source]
def top : T #

Top coordinate of the rectangle


[View source]
def top=(top : T) #

Top coordinate of the rectangle


[View source]
def width : T #

Width of the rectangle


[View source]
def width=(width : T) #

Width of the rectangle


[View source]