Rect2

package
v0.0.0-...-2b43b64 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package Rect2 provides a 2D axis-aligned bounding box using floating-point coordinates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Area

func Area(rect PositionSize) Float.X

Area returns the rectangle's area. This is equivalent to

size.X * size.Y

See also HasArea.

func Center

func Center(rect PositionSize) Vector2.XY

Center returns the center point of the rectangle position + (size / 2.0).

func End

func End(rect PositionSize) Vector2.XY

End returns the ending point. This is usually the bottom-right corner of the rectangle, and is equivalent to position + size.

func HasArea

func HasArea(rect PositionSize) bool

HasArea returns true if this rectangle has positive width and height. See also Area.

func HasPoint

func HasPoint(rect PositionSize, point Vector2.XY) bool

HasPoint returns true if the rectangle contains the given point. By convention, points on the right and bottom edges are not included.

Note: This method is not reliable for Rect2 with a negative size. Use abs first to get a valid rectangle.

func Inside

func Inside(enclosure, rect PositionSize) bool

Inside returns true if the rectangle is enclosed by the enclosure rectangle.

func IsApproximatelyEqual

func IsApproximatelyEqual(a, b PositionSize) bool

IsApproximatelyEqual returns true if this rectangle and rect are approximately equal, by calling [Vector2.IsApproximatelyEqual] on the position and the size.

func IsFinite

func IsFinite(rect PositionSize) bool

IsFinite returns true if this rectangle's values are finite, by calling [Vector2.IsFinite] on the position and the size.

func Overlaps

func Overlaps(a, b PositionSize) bool

Overlaps returns true if this rectangle overlaps with the b rectangle. The edges of both rectangles are excluded.

Types

type PositionSize

type PositionSize = struct {
	Position Vector2.XY // The origin point. This is usually the top-left corner of the rectangle.

	// The rectangle's width and height, starting from position. Setting this value also affects the
	// end point.
	//
	// Note: It's recommended setting the width and height to non-negative values, as most functions
	// assume that the position is the top-left corner, and the end is the bottom-right corner. To
	// get an equivalent rectangle with non-negative size, use [Abs].
	Size Vector2.XY
}

PositionSize 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). Although PositionSize itself is axis-aligned, it can be combined with [Transform2D.OriginXY] to represent a rotated or skewed rectangle.

For integer coordinates, use [Rect2i.PositionSize]. The 3D equivalent is [AABB.PositionSize].

Note: Negative values for size are not supported. With negative size, most PositionSize functions do not work correctly. Use Abs to get an equivalent PositionSize with a non-negative size.

func Abs

Abs returns a PositionSize equivalent to this rectangle, with its width and height modified to be non-negative values, and with its position being the top-left corner of the rectangle.

Note: It's recommended to use this method when size is negative, as most functions will assume that the position is the top-left corner, and the end is the bottom-right corner.

func Expand

func Expand[X Float.Any | Int.Any](rect PositionSize, amount X) PositionSize

Expand returns a copy of this rectangle extended on all sides by the given amount. A negative amount shrinks the rectangle instead. See also ExpandSides and ExpandSide.

func ExpandSide

func ExpandSide[X Float.Any | Int.Any](rect PositionSize, side Side, amount X) PositionSize

ExpandSize returns a copy of this rectangle with its side extended by the given amount (see Side constants). A negative amount shrinks the rectangle, instead. See also Expand and ExpandSides.

func ExpandSides

func ExpandSides[X Float.Any | Int.Any](rect PositionSize, left, top, right, bottom X) PositionSize

ExpandSides returns a copy of this rectangle with its left, top, right, and bottom sides extended by the given amounts. Negative values shrink the sides, instead. See also Expand and ExpandSide.

func ExpandTo

func ExpandTo(point Vector2.XY, rect PositionSize) PositionSize

ExpandTo returns a copy of this rectangle expanded to align the edges with the given to point, if necessary.

func Intersection

func Intersection(a, b PositionSize) PositionSize

Intersection returns the intersection between this rectangle and b. If the rectangles do not intersect, returns an empty PositionSize.

Note: If you only need to know whether two rectangles are overlapping, use Overlaps.

func Merge

func Merge(a, b PositionSize) PositionSize

Merge returns a PositionSize that encloses both this rectangle and b around the edges. See also Inside.

func New

func New[X Float.Any | Int.Any](x, y, w, h X) PositionSize

New constructs a PositionSize by setting its position to (x, y), and its size to (w, h).

type Side

type Side int
const (
	SideLeft   Side = 0
	SideTop    Side = 1
	SideRight  Side = 2
	SideBottom Side = 3
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL