struct SF::Rect(T)
- SF::Rect(T)
- Struct
- Value
- Object
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:
- 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:
SF::Rect(Int32)
isSF::IntRect
SF::Rect(Float32)
isSF::FloatRect
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.crConstructors
-
.new(left : T, top : T, width : T, height : T)
Construct the rectangle from its coordinates
-
.new(position : Vector2(T), size : Vector2(T))
Construct the rectangle from position and size
-
.new
Default constructor: equivalent to
.new(0, 0, 0, 0)
Instance Method Summary
-
#==(other : self) : Bool
Returns true if all corresponding coordinates of two rects are equal
-
#contains?(x : Number, y : Number) : Bool
Returns true if a point is inside the rectangle's area
-
#contains?(point : Vector2 | Tuple) : Bool
Returns true if a point is inside the rectangle's area
-
#height : T
Height of the rectangle
-
#height=(height : T)
Height of the rectangle
-
#intersects?(other : Rect(T)) : Rect(T) | Nil
Check the intersection between two rectangles
-
#left : T
Left coordinate of the rectangle
-
#left=(left : T)
Left coordinate of the rectangle
-
#top : T
Top coordinate of the rectangle
-
#top=(top : T)
Top coordinate of the rectangle
-
#width : T
Width of the rectangle
-
#width=(width : T)
Width of the rectangle
Constructor Detail
Construct the rectangle from its coordinates
Be careful, the last two parameters are the width and height, not the right and bottom coordinates!
Construct the rectangle from position and size
Be careful, the last parameter is the size, not the bottom-right corner!
Instance Method Detail
Returns true if a point is inside the rectangle's area
Returns true if a point is inside the rectangle's area
Check the intersection between two rectangles
Returns the overlapped rectangle or nil if there is no overlap.