Rect2
A 2D axis-aligned bounding box using floating-point coordinates.
Rect2.swift:21struct Rect2
The Rect2
built-in Variant
type represents an axis-aligned rectangle in a 2D space. It is defined by its position
and size
, which are Vector2
. It is frequently used for fast overlap tests (see intersects(b:includeBorders:)
). Although Rect2
itself is axis-aligned, it can be combined with Transform2D
to represent a rotated or skewed rectangle.
For integer coordinates, use Rect2i
. The 3D equivalent to Rect2
is AABB
.
Citizens in SwiftGodot
Conformances
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
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
Rect2
with itsposition
andsize
set toVector2.ZERO
.init(from: Rect2
) init(from: Rect2i
) init(position: Vector2, size: Vector2
) Constructs a
Rect2
byposition
andsize
.init(x: Float, y: Float, width: Float, height: Float
) Constructs a
Rect2
by setting itsposition
to (x
,y
), and itssize
to (width
,height
).static var godotType: Variant.GType
static func != (lhs: Rect2, rhs: Rect2
) -> Bool Returns
true
if theposition
orsize
of both rectangles are not equal.static func * (lhs: Rect2, rhs: Transform2D
) -> Rect2 Inversely transforms (multiplies) the
Rect2
by the givenTransform2D
transformation matrix, under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection is fine, scaling/skew is not).static func == (lhs: Rect2, rhs: Rect2
) -> Bool Returns
true
if bothposition
andsize
of the rectangles are exactly equal, respectively.
Instance members
var end: Vector2
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: Vector2
The origin point. This is usually the top-left corner of the rectangle.
var size: Vector2
The rectangle’s width and height, starting from
position
. Setting this value also affects theend
point.func abs(
) -> Rect2 Returns a
Rect2
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: Rect2
) -> Bool Returns
true
if this rectangle completely encloses theb
rectangle.func expand(to: Vector2
) -> Rect2 Returns a copy of this rectangle expanded to align the edges with the given
to
point, if necessary.func getArea(
) -> Double Returns the rectangle’s area. This is equivalent to
size.x * size.y
. See alsohasArea
.func getCenter(
) -> Vector2 Returns the center point of the rectangle. This is the same as
position + (size / 2.0)
.func grow(amount: Double
) -> Rect2 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: Double, top: Double, right: Double, bottom: Double
) -> Rect2 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: Double
) -> Rect2 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(Vector2
) -> Bool Returns
true
if the rectangle contains the givenpoint
. By convention, points on the right and bottom edges are not included.func intersection(b: Rect2
) -> Rect2 Returns the intersection between this rectangle and
b
. If the rectangles do not intersect, returns an emptyRect2
.func intersects(b: Rect2, includeBorders: Bool
) -> Bool Returns
true
if this rectangle overlaps with theb
rectangle. The edges of both rectangles are excluded, unlessincludeBorders
istrue
.func isEqualApprox(rect: Rect2
) -> Bool Returns
true
if this rectangle andrect
are approximately equal, by callingisEqualApprox(to:)
on theposition
and thesize
.func isFinite(
) -> Bool Returns
true
if this rectangle’s values are finite, by callingisFinite
on theposition
and thesize
.func merge(b: Rect2
) -> Rect2 Returns a
Rect2
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.