Rect2i
A 2D axis-aligned bounding box using integer coordinates.
Rect2i.swift:15struct Rect2i
The Rect2i
built-in Variant
type represents an axis-aligned rectangle in a 2D space, using integer coordinates. It is defined by its position
and size
, which are Vector2i
. Because it does not rotate, it is frequently used for fast overlap tests (see intersects(b:)
).
For floating-point coordinates, see Rect2
.
Citizens in SwiftGodot
Conformances
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol SelfVariantRepresentable
Structs and scalar types use their own layout for storage.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
init(
) Constructs a
Rect2i
with itsposition
andsize
set toVector2i.ZERO
.init(from: Rect2i
) init(from: Rect2
) Constructs a
Rect2i
from aRect2
. The floating-point coordinates are truncated.init(position: Vector2i, size: Vector2i
) Constructs a
Rect2i
byposition
andsize
.init(x: Int32, y: Int32, width: Int32, height: Int32
) Constructs a
Rect2i
by setting itsposition
to (x
,y
), and itssize
to (width
,height
).static var godotType: Variant.GType
static func != (lhs: Rect2i, rhs: Rect2i
) -> Bool Returns
true
if theposition
orsize
of both rectangles are not equal.static func == (lhs: Rect2i, rhs: Rect2i
) -> Bool Returns
true
if bothposition
andsize
of the rectangles are equal, respectively.
Instance members
var end: Vector2i
The ending point. This is usually the bottom-right corner of the rectangle, and is equivalent to
position + size
. Setting this point affects thesize
.var position: Vector2i
The origin point. This is usually the top-left corner of the rectangle.
var size: Vector2i
The rectangle’s width and height, starting from
position
. Setting this value also affects theend
point.func abs(
) -> Rect2i Returns a
Rect2i
equivalent to this rectangle, with its width and height modified to be non-negative values, and with itsposition
being the top-left corner of the rectangle.func encloses(b: Rect2i
) -> Bool Returns
true
if thisRect2i
completely encloses another one.func expand(to: Vector2i
) -> Rect2i Returns a copy of this rectangle expanded to align the edges with the given
to
point, if necessary.func getArea(
) -> Int64 Returns the rectangle’s area. This is equivalent to
size.x * size.y
. See alsohasArea
.func getCenter(
) -> Vector2i Returns the center point of the rectangle. This is the same as
position + (size / 2)
.func grow(amount: Int64
) -> Rect2i Returns a copy of this rectangle extended on all sides by the given
amount
. A negativeamount
shrinks the rectangle instead. See alsogrowIndividual(left:top:right:bottom:)
andgrowSide(side:amount:)
.func growIndividual(left: Int64, top: Int64, right: Int64, bottom: Int64
) -> Rect2i Returns a copy of this rectangle with its
left
,top
,right
, andbottom
sides extended by the given amounts. Negative values shrink the sides, instead. See alsogrow(amount:)
andgrowSide(side:amount:)
.func growSide(Int64, amount: Int64
) -> Rect2i Returns a copy of this rectangle with its
side
extended by the givenamount
(seeSide
constants). A negativeamount
shrinks the rectangle, instead. See alsogrow(amount:)
andgrowIndividual(left:top:right:bottom:)
.func hasArea(
) -> Bool Returns
true
if this rectangle has positive width and height. See alsogetArea
.func hasPoint(Vector2i
) -> Bool Returns
true
if the rectangle contains the givenpoint
. By convention, points on the right and bottom edges are not included.func intersection(b: Rect2i
) -> Rect2i Returns the intersection between this rectangle and
b
. If the rectangles do not intersect, returns an emptyRect2i
.func intersects(b: Rect2i
) -> Bool Returns
true
if this rectangle overlaps with theb
rectangle. The edges of both rectangles are excluded.func merge(b: Rect2i
) -> Rect2i Returns a
Rect2i
that encloses both this rectangle andb
around the edges. See alsoencloses(b:)
.
Type features
init?(Variant
) static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant.
static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant. This is useful when you want one method to call that will return the unwrapped Variant, regardless of whether it is a SwiftGodot.Object or not.
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.