Rect2i

package
v0.0.0-...-7325ca5 Latest Latest
Warning

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

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

Documentation

Overview

Package Rect2i provides a 2D axis-aligned bounding box using integer coordinates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Area

func Area(rect PositionSize) int

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

size.X * size.Y

See also HasArea.

func Center

func Center(rect PositionSize) Vector2i.XY

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

func End

func End(rect PositionSize) Vector2i.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 Vector2i.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 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 Vector2i.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 Vector2i.XY
}

PositionSize represents an axis-aligned rectangle in a 2D space, using integer coordinates. It is defined by its position and size, which are [Vector2i.XY]. Because it does not rotate, it is frequently used for fast overlap tests (see intersects).

For floating-point coordinates, see Rect2.

Note: Negative values for size are not supported. With negative size, most functions do not work correctly. Use Abs to get an equivalent Rect2i 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 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 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 Vector2i.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