Documentation
¶
Overview ¶
Package region provides efficient 2D region operations using Y-X banded rectangles.
A Region is a set of disjoint rectangles, plus an extent rectangle that bounds them. It supports union, intersection, subtraction, inversion, translation, and spatial queries such as point and rectangle containment.
Internally, rectangles are stored in a Y-X banded array: sorted by top side Y1, then by left side X1. Rectangles are grouped into horizontal bands that share the same Y1 and Y2. Rectangles within a band never overlap or touch. Adjacent bands with identical X spans are coalesced to reduce rectangle count.
Coordinates use inclusive-exclusive semantics: [X1, X2) x [Y1, Y2) with int32 values. Boxes with non-positive width or height are invalid.
Index ¶
- type Box
- func (b Box) Area() int64
- func (b Box) Contains(x, y int32) bool
- func (b Box) Equal(other Box) bool
- func (b Box) Height() int32
- func (b Box) Intersection(other Box) Box
- func (b Box) Intersects(other Box) bool
- func (b Box) IsValid() bool
- func (b Box) Subsumes(other Box) bool
- func (b Box) Translate(dx, dy int32) Box
- func (b Box) Union(other Box) Box
- func (b Box) Width() int32
- type OverlapType
- type Region
- func (r *Region) Clear()
- func (r *Region) Clone() *Region
- func (r *Region) ContainsPoint(x, y int32) bool
- func (r *Region) ContainsPointBox(x, y int32) (bool, Box)
- func (r *Region) ContainsRectangle(box Box) OverlapType
- func (r *Region) Copy(other *Region)
- func (r *Region) Equal(other *Region) bool
- func (r *Region) Extents() Box
- func (r *Region) Intersect(other *Region)
- func (r *Region) IntersectRect(box Box)
- func (r *Region) Inverse(bounds Box)
- func (r *Region) IsEmpty() bool
- func (r *Region) NotEmpty() bool
- func (r *Region) NumRects() int
- func (r *Region) Rectangles() []Box
- func (r *Region) Reset(box Box)
- func (r *Region) SelfCheck() bool
- func (r *Region) Subtract(other *Region)
- func (r *Region) Translate(dx, dy int32)
- func (r *Region) Union(other *Region)
- func (r *Region) UnionRect(box Box)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Box ¶
type Box struct {
X1, Y1, X2, Y2 int32
}
Box represents a rectangle using inclusive-exclusive semantics: [X1, X2) x [Y1, Y2). A valid box requires X1 < X2 and Y1 < Y2.
func (Box) Contains ¶
Contains returns true if point (x, y) is inside the box. Uses inclusive-exclusive: x1 <= x < x2, y1 <= y < y2.
func (Box) Intersection ¶
Intersection returns the intersection of two boxes. Returns an invalid box if they don't overlap.
func (Box) Intersects ¶
Intersects returns true if this box overlaps with other.
func (Box) Translate ¶
Translate returns a new box shifted by (dx, dy). Returns the original box if the translation would cause overflow.
type OverlapType ¶
type OverlapType int
OverlapType represents the relationship between a rectangle and a region.
const ( // OverlapOut means the rectangle is completely outside the region. OverlapOut OverlapType = iota // OverlapIn means the rectangle is completely inside the region. OverlapIn // OverlapPart means the rectangle partially overlaps the region. OverlapPart )
type Region ¶
type Region struct {
// contains filtered or unexported fields
}
Region represents a set of non-overlapping rectangles. Internally, rectangles are stored in Y-X banded format for efficient operations.
The region is stored as follows:
- extents: bounding box of the entire region
- rects: slice of rectangles in Y-X banded order
For an empty region, rects is nil and extents is invalid. For a single rectangle region, rects may be nil and extents represents the rectangle.
func NewRect ¶
NewRect creates a new region containing a single rectangle. The rectangle is specified by its top-left corner (x, y) and dimensions (width, height). If width or height is <= 0, returns an empty region.
func NewRects ¶
NewRects creates a new region from a slice of rectangles. The rectangles will be validated, sorted, and merged into Y-X banded format. Invalid rectangles (with width <= 0 or height <= 0) are ignored.
func NewWithExtents ¶
NewWithExtents creates a new region from a bounding box. If the box is invalid, returns an empty region.
func (*Region) ContainsPoint ¶
ContainsPoint returns true if the point (x, y) is inside the region.
func (*Region) ContainsPointBox ¶
ContainsPointBox returns true if the point (x, y) is inside the region, along with the box that contains the point (if found).
func (*Region) ContainsRectangle ¶
func (r *Region) ContainsRectangle(box Box) OverlapType
ContainsRectangle returns the overlap relationship between the region and a box. Returns:
- OverlapOut if the box is completely outside the region
- OverlapIn if the box is completely inside the region
- OverlapPart if the box partially overlaps the region
Boxes are not validated here; invalid boxes may return any OverlapType depending on their coordinates. The algorithm walks the banded rectangles to cover the box, tracking whether any part is covered (partIn) and any part is uncovered (partOut). It stops once the box is fully covered, partially covered, or disjoint.
func (*Region) Extents ¶
Extents returns the bounding box of the entire region. Returns an invalid box if the region is empty.
func (*Region) IntersectRect ¶
IntersectRect sets r to the intersection of r and the rectangle.
func (*Region) Inverse ¶
Inverse sets r to the inverse of r within the given bounds. This is equivalent to bounds - r.
func (*Region) Rectangles ¶
Rectangles returns a copy of all rectangles in the region. The rectangles are in Y-X banded order.
func (*Region) SelfCheck ¶
SelfCheck validates the internal consistency of the region. Returns true if the region is valid, false otherwise. This is primarily useful for debugging.
func (*Region) Translate ¶
Translate moves the region by (dx, dy). Translates in place.
- If completely in range after translation: translate all boxes
- If completely out of range: clear the region
- If partially out of range: clip to valid range and filter out-of-range boxes