Documentation
¶
Overview ¶
Package geometry provides fundamental geometric types for UI layout and rendering.
This package defines the core geometric primitives used throughout the gogpu/ui toolkit: points, sizes, rectangles, constraints, and insets. All types use float32 coordinates for GPU compatibility and are designed for zero-allocation in hot paths.
Types Overview ¶
- Point: A 2D point with X, Y coordinates
- Size: Dimensions with Width, Height
- Rect: Axis-aligned rectangle defined by Min and Max points
- Constraints: Flutter-style layout constraints (min/max width and height)
- Insets: Edge insets for padding and margins
Design Principles ¶
All types in this package follow these principles:
- Value semantics: Types are small structs passed by value
- Immutable operations: Methods return new values, never modify receiver
- Zero-allocation: No heap allocations in hot paths
- NaN safety: Operations handle NaN gracefully (replace with zero)
Usage Example ¶
// Create a rectangle from position and size
rect := geometry.FromPointSize(
geometry.Point{X: 10, Y: 20},
geometry.Size{Width: 100, Height: 50},
)
// Apply padding
padding := geometry.UniformInsets(8)
contentRect := rect.Inset(padding)
// Check if a point is inside
if contentRect.Contains(geometry.Point{X: 50, Y: 40}) {
// Handle hit
}
Thread Safety ¶
All types are safe for concurrent use as they use value semantics and do not contain any mutable state or pointers.
Index ¶
- Constants
- type Constraints
- func BoxConstraints(minWidth, maxWidth, minHeight, maxHeight float32) Constraints
- func Expand() Constraints
- func ExpandHeight(width float32) Constraints
- func ExpandWidth(height float32) Constraints
- func Loose(size Size) Constraints
- func Tight(size Size) Constraints
- func TightHeight(height float32) Constraints
- func TightWidth(width float32) Constraints
- func (c Constraints) Biggest() Size
- func (c Constraints) BiggestFinite(fallbackWidth, fallbackHeight float32) Size
- func (c Constraints) Constrain(size Size) Size
- func (c Constraints) ConstrainDimensions(width, height float32) (float32, float32)
- func (c Constraints) ConstrainHeight(height float32) float32
- func (c Constraints) ConstrainWidth(width float32) float32
- func (c Constraints) Deflate(insets Insets) Constraints
- func (c Constraints) Enforce(other Constraints) Constraints
- func (c Constraints) HasBoundedHeight() bool
- func (c Constraints) HasBoundedWidth() bool
- func (c Constraints) HasInfiniteHeight() bool
- func (c Constraints) HasInfiniteWidth() bool
- func (c Constraints) IsNaN() bool
- func (c Constraints) IsNormalized() bool
- func (c Constraints) IsSatisfiedBy(size Size) bool
- func (c Constraints) IsTight() bool
- func (c Constraints) IsTightHeight() bool
- func (c Constraints) IsTightWidth() bool
- func (c Constraints) IsUnbounded() bool
- func (c Constraints) IsZero() bool
- func (c Constraints) Loosen() Constraints
- func (c Constraints) LoosenHeight() Constraints
- func (c Constraints) LoosenWidth() Constraints
- func (c Constraints) Normalize() Constraints
- func (c Constraints) Sanitize() Constraints
- func (c Constraints) Smallest() Size
- func (c Constraints) String() string
- func (c Constraints) Tighten(size Size) Constraints
- func (c Constraints) TightenHeight(height float32) Constraints
- func (c Constraints) TightenWidth(width float32) Constraints
- type Insets
- func (i Insets) Abs() Insets
- func (i Insets) Add(other Insets) Insets
- func (i Insets) BottomRight() Point
- func (i Insets) Clamp(minInsets, maxInsets Insets) Insets
- func (i Insets) Horizontal() float32
- func (i Insets) IsNaN() bool
- func (i Insets) IsNonNegative() bool
- func (i Insets) IsSymmetric() bool
- func (i Insets) IsUniform() bool
- func (i Insets) IsZero() bool
- func (i Insets) Max(other Insets) Insets
- func (i Insets) Min(other Insets) Insets
- func (i Insets) Negate() Insets
- func (i Insets) Sanitize() Insets
- func (i Insets) Scale(scalar float32) Insets
- func (i Insets) Size() Size
- func (i Insets) String() string
- func (i Insets) Sub(other Insets) Insets
- func (i Insets) TopLeft() Point
- func (i Insets) Vertical() float32
- type Point
- func (p Point) Add(other Point) Point
- func (p Point) Clamp(minP, maxP Point) Point
- func (p Point) Distance(other Point) float32
- func (p Point) DistanceSquared(other Point) float32
- func (p Point) Div(other Point) Point
- func (p Point) Dot(other Point) float32
- func (p Point) IsNaN() bool
- func (p Point) IsZero() bool
- func (p Point) Length() float32
- func (p Point) LengthSquared() float32
- func (p Point) Lerp(other Point, t float32) Point
- func (p Point) Max(other Point) Point
- func (p Point) Min(other Point) Point
- func (p Point) Mul(other Point) Point
- func (p Point) Negate() Point
- func (p Point) Normalize() Point
- func (p Point) Sanitize() Point
- func (p Point) Scale(scalar float32) Point
- func (p Point) String() string
- func (p Point) Sub(other Point) Point
- type Rect
- func (r Rect) Area() float32
- func (r Rect) BottomLeft() Point
- func (r Rect) BottomRight() Point
- func (r Rect) Center() Point
- func (r Rect) Contains(p Point) bool
- func (r Rect) ContainsRect(other Rect) bool
- func (r Rect) Expand(delta float32) Rect
- func (r Rect) Height() float32
- func (r Rect) Inset(insets Insets) Rect
- func (r Rect) Intersection(other Rect) Rect
- func (r Rect) Intersects(other Rect) bool
- func (r Rect) IsEmpty() bool
- func (r Rect) IsNaN() bool
- func (r Rect) IsZero() bool
- func (r Rect) Normalize() Rect
- func (r Rect) Sanitize() Rect
- func (r Rect) Size() Size
- func (r Rect) String() string
- func (r Rect) TopLeft() Point
- func (r Rect) TopRight() Point
- func (r Rect) Translate(offset Point) Rect
- func (r Rect) TranslateXY(dx, dy float32) Rect
- func (r Rect) Union(other Rect) Rect
- func (r Rect) Width() float32
- func (r Rect) WithCenter(center Point) Rect
- func (r Rect) WithSize(s Size) Rect
- type Size
- func (s Size) Add(other Size) Size
- func (s Size) Area() float32
- func (s Size) AspectRatio() float32
- func (s Size) Clamp(minSize, maxSize Size) Size
- func (s Size) Contains(other Size) bool
- func (s Size) Contract(delta float32) Size
- func (s Size) Expand(delta float32) Size
- func (s Size) FillIn(maxSize Size) Size
- func (s Size) FitIn(maxSize Size) Size
- func (s Size) IsEmpty() bool
- func (s Size) IsNaN() bool
- func (s Size) IsZero() bool
- func (s Size) Max(other Size) Size
- func (s Size) Min(other Size) Size
- func (s Size) Sanitize() Size
- func (s Size) Scale(scalar float32) Size
- func (s Size) String() string
- func (s Size) Sub(other Size) Size
- func (s Size) ToPoint() Point
Constants ¶
const Infinity = float32(math.MaxFloat32)
Infinity represents unbounded constraint dimension. Use this value for MaxWidth/MaxHeight when there is no upper limit.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Constraints ¶
Constraints represents box constraints for layout, similar to Flutter's BoxConstraints.
Constraints define the minimum and maximum dimensions a widget can have. They are passed down the widget tree during layout and used to constrain the size of child widgets.
A constraint is considered "tight" when min equals max (widget must be exactly that size). A constraint is "loose" when min is 0 (widget can be any size up to max). A constraint is "unbounded" when max is Infinity (no upper limit).
The zero value represents unconstrained (0 to Infinity on both dimensions).
func BoxConstraints ¶
func BoxConstraints(minWidth, maxWidth, minHeight, maxHeight float32) Constraints
BoxConstraints creates constraints with explicit min/max dimensions.
Example:
c := geometry.BoxConstraints(50, 200, 30, 100) // MinWidth=50, MaxWidth=200, MinHeight=30, MaxHeight=100
func Expand ¶
func Expand() Constraints
Expand creates constraints where the widget should expand to fill available space. This is a loose constraint with infinite max dimensions.
Example:
c := geometry.Expand() // MinWidth=0, MaxWidth=Infinity, MinHeight=0, MaxHeight=Infinity
func ExpandHeight ¶
func ExpandHeight(width float32) Constraints
ExpandHeight creates constraints that expand vertically but are flexible horizontally.
func ExpandWidth ¶
func ExpandWidth(height float32) Constraints
ExpandWidth creates constraints that expand horizontally but are flexible vertically.
func Loose ¶
func Loose(size Size) Constraints
Loose creates constraints where the widget can be any size up to the given size.
Example:
c := geometry.Loose(geometry.Sz(100, 50)) // MinWidth=0, MaxWidth=100, MinHeight=0, MaxHeight=50
func Tight ¶
func Tight(size Size) Constraints
Tight creates constraints where the widget must be exactly the given size.
Example:
c := geometry.Tight(geometry.Sz(100, 50)) // MinWidth=100, MaxWidth=100, MinHeight=50, MaxHeight=50
func TightHeight ¶
func TightHeight(height float32) Constraints
TightHeight creates constraints with exact height but flexible width.
func TightWidth ¶
func TightWidth(width float32) Constraints
TightWidth creates constraints with exact width but flexible height.
func (Constraints) Biggest ¶
func (c Constraints) Biggest() Size
Biggest returns the biggest finite size that satisfies these constraints. If a dimension is unbounded (Infinity), it uses the minimum instead.
func (Constraints) BiggestFinite ¶
func (c Constraints) BiggestFinite(fallbackWidth, fallbackHeight float32) Size
BigggestFinite returns the biggest size, treating Infinity as the provided fallback.
func (Constraints) Constrain ¶
func (c Constraints) Constrain(size Size) Size
Constrain returns a size that satisfies these constraints. The returned size will be clamped to [min, max] for each dimension.
Example:
c := geometry.BoxConstraints(50, 200, 30, 100)
s := c.Constrain(geometry.Sz(300, 150)) // Size{200, 100}
func (Constraints) ConstrainDimensions ¶
func (c Constraints) ConstrainDimensions(width, height float32) (float32, float32)
ConstrainDimensions returns dimensions that satisfy these constraints.
func (Constraints) ConstrainHeight ¶
func (c Constraints) ConstrainHeight(height float32) float32
ConstrainHeight returns a height that satisfies these constraints.
func (Constraints) ConstrainWidth ¶
func (c Constraints) ConstrainWidth(width float32) float32
ConstrainWidth returns a width that satisfies these constraints.
func (Constraints) Deflate ¶
func (c Constraints) Deflate(insets Insets) Constraints
Deflate returns constraints reduced by the given insets. This is useful when calculating available space for content inside padding.
Example:
c := geometry.Tight(geometry.Sz(100, 100)) padding := geometry.UniformInsets(10) content := c.Deflate(padding) // MaxWidth=80, MaxHeight=80
func (Constraints) Enforce ¶
func (c Constraints) Enforce(other Constraints) Constraints
Enforce returns constraints that are at least as restrictive as both c and other. The result has the max of minimums and min of maximums.
func (Constraints) HasBoundedHeight ¶
func (c Constraints) HasBoundedHeight() bool
HasBoundedHeight returns true if max height is not Infinity.
func (Constraints) HasBoundedWidth ¶
func (c Constraints) HasBoundedWidth() bool
HasBoundedWidth returns true if max width is not Infinity.
func (Constraints) HasInfiniteHeight ¶
func (c Constraints) HasInfiniteHeight() bool
HasInfiniteHeight returns true if max height is Infinity.
func (Constraints) HasInfiniteWidth ¶
func (c Constraints) HasInfiniteWidth() bool
HasInfiniteWidth returns true if max width is Infinity.
func (Constraints) IsNaN ¶
func (c Constraints) IsNaN() bool
IsNaN returns true if any value is NaN.
func (Constraints) IsNormalized ¶
func (c Constraints) IsNormalized() bool
IsNormalized returns true if the constraints are valid (min <= max, no negatives).
func (Constraints) IsSatisfiedBy ¶
func (c Constraints) IsSatisfiedBy(size Size) bool
IsSatisfiedBy returns true if the given size satisfies these constraints.
Example:
c := geometry.BoxConstraints(50, 200, 30, 100) c.IsSatisfiedBy(geometry.Sz(100, 50)) // true c.IsSatisfiedBy(geometry.Sz(300, 50)) // false (width too large)
func (Constraints) IsTight ¶
func (c Constraints) IsTight() bool
IsTight returns true if min equals max for both dimensions. A tight constraint forces a specific size.
func (Constraints) IsTightHeight ¶
func (c Constraints) IsTightHeight() bool
IsTightHeight returns true if min height equals max height.
func (Constraints) IsTightWidth ¶
func (c Constraints) IsTightWidth() bool
IsTightWidth returns true if min width equals max width.
func (Constraints) IsUnbounded ¶
func (c Constraints) IsUnbounded() bool
IsUnbounded returns true if both max dimensions are Infinity. An unbounded constraint has no upper size limit.
func (Constraints) IsZero ¶
func (c Constraints) IsZero() bool
IsZero returns true if all constraints are zero.
func (Constraints) Loosen ¶
func (c Constraints) Loosen() Constraints
Loosen returns constraints with the same max but min set to 0. This allows a child to be smaller than the parent's minimum.
Example:
c := geometry.BoxConstraints(100, 200, 50, 100) loose := c.Loosen() // MinWidth=0, MaxWidth=200, MinHeight=0, MaxHeight=100
func (Constraints) LoosenHeight ¶
func (c Constraints) LoosenHeight() Constraints
LoosenHeight returns constraints with min height set to 0.
func (Constraints) LoosenWidth ¶
func (c Constraints) LoosenWidth() Constraints
LoosenWidth returns constraints with min width set to 0.
func (Constraints) Normalize ¶
func (c Constraints) Normalize() Constraints
Normalize returns constraints with mins clamped to not exceed maxes, and ensures no negative values.
Example:
c := geometry.BoxConstraints(200, 100, 50, 30) // Invalid: min > max normalized := c.Normalize() // MinWidth=100, MaxWidth=100, MinHeight=30, MaxHeight=30
func (Constraints) Sanitize ¶
func (c Constraints) Sanitize() Constraints
Sanitize returns constraints with NaN values replaced by appropriate defaults (0 for minimums, Infinity for maximums).
func (Constraints) Smallest ¶
func (c Constraints) Smallest() Size
Smallest returns the smallest size that satisfies these constraints.
func (Constraints) String ¶
func (c Constraints) String() string
String returns a string representation of the constraints.
Example:
c := geometry.BoxConstraints(50, 200, 30, 100) s := c.String() // "Constraints(50<=w<=200, 30<=h<=100)"
func (Constraints) Tighten ¶
func (c Constraints) Tighten(size Size) Constraints
Tighten returns tight constraints using the given size, clamped to current constraints. This is useful when a widget wants to report its preferred size while respecting parent constraints.
Example:
c := geometry.BoxConstraints(50, 200, 30, 100) tight := c.Tighten(geometry.Sz(150, 80)) // MinWidth=150, MaxWidth=150, MinHeight=80, MaxHeight=80
func (Constraints) TightenHeight ¶
func (c Constraints) TightenHeight(height float32) Constraints
TightenHeight returns constraints with tight height, keeping width constraints.
func (Constraints) TightenWidth ¶
func (c Constraints) TightenWidth(width float32) Constraints
TightenWidth returns constraints with tight width, keeping height constraints.
type Insets ¶
type Insets struct {
Top, Right, Bottom, Left float32
}
Insets represents edge insets for padding, margins, or borders.
Insets define spacing on each of the four edges of a rectangle. They are commonly used for padding (space inside a widget) and margins (space outside a widget).
The zero value represents no insets (0 on all edges).
func InsetsLTRB ¶
InsetsLTRB creates insets from Left, Top, Right, Bottom values. This ordering matches CSS shorthand (when specifying all four values).
Example:
margin := geometry.InsetsLTRB(10, 20, 30, 40)
// Insets{Top: 20, Right: 30, Bottom: 40, Left: 10}
func InsetsOnly ¶
InsetsOnly creates insets with specific edges set, others zero.
Example:
topPadding := geometry.InsetsOnly(10, 0, 0, 0) // top only
func InsetsTRBL ¶
InsetsTRBL creates insets from Top, Right, Bottom, Left values. This ordering matches CSS shorthand.
Example:
margin := geometry.InsetsTRBL(20, 30, 40, 10)
// Insets{Top: 20, Right: 30, Bottom: 40, Left: 10}
func SymmetricInsets ¶
SymmetricInsets creates insets with separate horizontal and vertical values.
Example:
padding := geometry.SymmetricInsets(16, 8)
// Insets{Top: 8, Right: 16, Bottom: 8, Left: 16}
func UniformInsets ¶
UniformInsets creates insets with the same value on all edges.
Example:
padding := geometry.UniformInsets(16)
// Insets{Top: 16, Right: 16, Bottom: 16, Left: 16}
func (Insets) Add ¶
Add returns new insets with values added.
Example:
i1 := geometry.UniformInsets(10)
i2 := geometry.UniformInsets(5)
result := i1.Add(i2) // Insets{15, 15, 15, 15}
func (Insets) BottomRight ¶
BottomRight returns the bottom-right offset as a Point.
func (Insets) Horizontal ¶
Horizontal returns the sum of left and right insets.
Example:
padding := geometry.UniformInsets(16) h := padding.Horizontal() // 32
func (Insets) IsNonNegative ¶
IsNonNegative returns true if all values are >= 0.
func (Insets) IsSymmetric ¶
IsSymmetric returns true if horizontal edges match and vertical edges match.
func (Insets) Scale ¶
Scale returns new insets with all values multiplied by the scalar.
Example:
padding := geometry.UniformInsets(10)
doubled := padding.Scale(2) // Insets{20, 20, 20, 20}
func (Insets) Size ¶
Size returns a Size representing the total horizontal and vertical insets. This is useful for calculating how much space insets consume.
Example:
padding := geometry.SymmetricInsets(16, 8)
s := padding.Size() // Size{Width: 32, Height: 16}
func (Insets) String ¶
String returns a string representation of the insets.
Example:
i := geometry.UniformInsets(16) s := i.String() // "Insets(16, 16, 16, 16)"
type Point ¶
type Point struct {
X, Y float32
}
Point represents a 2D point with float32 coordinates.
Point uses float32 for GPU compatibility and efficient memory layout. All operations return new values without modifying the original point.
The zero value is the origin point (0, 0).
func (Point) Add ¶
Add returns a new point with the coordinates of p added to other.
Example:
p1 := geometry.Pt(10, 20)
p2 := geometry.Pt(5, 5)
result := p1.Add(p2) // Point{15, 25}
func (Point) Clamp ¶
Clamp returns a new point with coordinates clamped to the range [minP, maxP].
Example:
p := geometry.Pt(15, -5)
minP := geometry.Pt(0, 0)
maxP := geometry.Pt(10, 10)
result := p.Clamp(minP, maxP) // Point{10, 0}
func (Point) Distance ¶
Distance returns the Euclidean distance between p and other.
Example:
p1 := geometry.Pt(0, 0) p2 := geometry.Pt(3, 4) d := p1.Distance(p2) // 5.0
func (Point) DistanceSquared ¶
DistanceSquared returns the squared Euclidean distance between p and other. This is faster than Distance when only comparing distances.
Example:
p1 := geometry.Pt(0, 0) p2 := geometry.Pt(3, 4) d2 := p1.DistanceSquared(p2) // 25.0
func (Point) Div ¶
Div returns a new point with coordinates divided component-wise. If a component of other is zero, the result for that component is zero.
Example:
p1 := geometry.Pt(10, 20)
p2 := geometry.Pt(2, 4)
result := p1.Div(p2) // Point{5, 5}
func (Point) Dot ¶
Dot returns the dot product of p and other.
Example:
p1 := geometry.Pt(1, 2) p2 := geometry.Pt(3, 4) d := p1.Dot(p2) // 11.0 (1*3 + 2*4)
func (Point) Length ¶
Length returns the length (magnitude) of the vector from origin to p.
Example:
p := geometry.Pt(3, 4) l := p.Length() // 5.0
func (Point) LengthSquared ¶
LengthSquared returns the squared length of the vector from origin to p. This is faster than Length when only comparing lengths.
func (Point) Lerp ¶
Lerp returns a point linearly interpolated between p and other. t=0 returns p, t=1 returns other, values outside [0,1] extrapolate.
Example:
p1 := geometry.Pt(0, 0)
p2 := geometry.Pt(10, 20)
mid := p1.Lerp(p2, 0.5) // Point{5, 10}
func (Point) Max ¶
Max returns a new point with the maximum coordinates of p and other.
Example:
p1 := geometry.Pt(10, 5)
p2 := geometry.Pt(3, 20)
result := p1.Max(p2) // Point{10, 20}
func (Point) Min ¶
Min returns a new point with the minimum coordinates of p and other.
Example:
p1 := geometry.Pt(10, 5)
p2 := geometry.Pt(3, 20)
result := p1.Min(p2) // Point{3, 5}
func (Point) Mul ¶
Mul returns a new point with coordinates multiplied component-wise.
Example:
p1 := geometry.Pt(10, 20)
p2 := geometry.Pt(2, 3)
result := p1.Mul(p2) // Point{20, 60}
func (Point) Negate ¶
Negate returns a new point with both coordinates negated.
Example:
p := geometry.Pt(10, -20)
result := p.Negate() // Point{-10, 20}
func (Point) Normalize ¶
Normalize returns a unit vector in the same direction as p. If p is the zero point, returns the zero point.
Example:
p := geometry.Pt(3, 4)
n := p.Normalize() // Point{0.6, 0.8}
func (Point) Scale ¶
Scale returns a new point with both coordinates multiplied by the scalar.
Example:
p := geometry.Pt(10, 20)
result := p.Scale(2) // Point{20, 40}
type Rect ¶
type Rect struct {
Min, Max Point
}
Rect represents an axis-aligned rectangle defined by its minimum and maximum corners.
A rectangle is considered valid when Min.X <= Max.X and Min.Y <= Max.Y. Some operations may return invalid rectangles (e.g., Intersection when rectangles don't overlap). Use IsEmpty() to check validity.
The zero value is an empty rectangle at the origin.
func FromCenter ¶
FromCenter creates a rectangle centered at the given point with the given size.
Example:
r := geometry.FromCenter(geometry.Pt(50, 50), geometry.Sz(100, 50)) // Min: (0, 25), Max: (100, 75)
func FromMinMax ¶
FromMinMax creates a rectangle from two corner points. The points are normalized so Min contains the smaller coordinates.
func FromPointSize ¶
FromPointSize creates a rectangle from a point (top-left) and size.
Example:
r := geometry.FromPointSize(geometry.Pt(10, 20), geometry.Sz(100, 50))
func NewRect ¶
NewRect creates a rectangle from position (x, y) and dimensions (w, h).
Example:
r := geometry.NewRect(10, 20, 100, 50) // Min: (10, 20), Max: (110, 70)
func (Rect) BottomLeft ¶
BottomLeft returns the bottom-left corner.
func (Rect) BottomRight ¶
BottomRight returns the bottom-right corner (same as Max).
func (Rect) Center ¶
Center returns the center point of the rectangle.
Example:
r := geometry.NewRect(0, 0, 100, 50)
c := r.Center() // Point{50, 25}
func (Rect) Contains ¶
Contains returns true if the point p is inside or on the edge of the rectangle.
Example:
r := geometry.NewRect(0, 0, 100, 50) r.Contains(geometry.Pt(50, 25)) // true r.Contains(geometry.Pt(150, 25)) // false
func (Rect) ContainsRect ¶
ContainsRect returns true if other is entirely inside or equal to r.
Example:
outer := geometry.NewRect(0, 0, 100, 100) inner := geometry.NewRect(10, 10, 50, 50) outer.ContainsRect(inner) // true
func (Rect) Expand ¶
Expand returns a new rectangle with all edges moved outward by delta. Negative delta values shrink the rectangle.
Example:
r := geometry.NewRect(10, 10, 80, 80)
expanded := r.Expand(5) // Rect{Min:(5,5), Max:(95,95)}
func (Rect) Inset ¶
Inset returns a new rectangle with edges moved inward by the specified insets. Positive inset values shrink the rectangle, negative values expand it.
Example:
r := geometry.NewRect(0, 0, 100, 100)
padding := geometry.UniformInsets(10)
inner := r.Inset(padding) // Rect{Min:(10,10), Max:(90,90)}
func (Rect) Intersection ¶
Intersection returns the overlapping area of r and other. If the rectangles don't overlap, returns an empty rectangle.
Example:
r1 := geometry.NewRect(0, 0, 100, 100)
r2 := geometry.NewRect(50, 50, 100, 100)
inter := r1.Intersection(r2) // Rect{Min:(50,50), Max:(100,100)}
func (Rect) Intersects ¶
Intersects returns true if r and other overlap (share any area).
Example:
r1 := geometry.NewRect(0, 0, 100, 100) r2 := geometry.NewRect(50, 50, 100, 100) r1.Intersects(r2) // true
func (Rect) IsEmpty ¶
IsEmpty returns true if the rectangle has zero or negative area. This can happen when Min >= Max in either dimension.
func (Rect) Normalize ¶
Normalize returns a copy of r with Min and Max swapped if necessary so that Min contains the smaller coordinates.
func (Rect) Size ¶
Size returns the dimensions of the rectangle.
Example:
r := geometry.NewRect(10, 20, 100, 50)
s := r.Size() // Size{100, 50}
func (Rect) String ¶
String returns a string representation of the rectangle.
Example:
r := geometry.NewRect(10, 20, 100, 50) str := r.String() // "Rect(10, 20, 100x50)"
func (Rect) Translate ¶
Translate returns a new rectangle moved by the given offset.
Example:
r := geometry.NewRect(0, 0, 100, 50)
moved := r.Translate(geometry.Pt(10, 20))
// Rect{Min:(10,20), Max:(110,70)}
func (Rect) TranslateXY ¶
TranslateXY returns a new rectangle moved by (dx, dy).
func (Rect) Union ¶
Union returns the smallest rectangle containing both r and other.
Example:
r1 := geometry.NewRect(0, 0, 50, 50)
r2 := geometry.NewRect(100, 100, 50, 50)
union := r1.Union(r2) // Rect{Min:(0,0), Max:(150,150)}
func (Rect) WithCenter ¶
WithCenter returns a new rectangle with the same size but centered at the given point.
type Size ¶
type Size struct {
Width, Height float32
}
Size represents dimensions with Width and Height.
Size uses float32 for GPU compatibility. Negative dimensions are allowed but may have special meaning in some contexts (e.g., "shrink to fit").
The zero value represents empty dimensions (0x0).
func (Size) Add ¶
Add returns a new size with dimensions added.
Example:
s1 := geometry.Sz(100, 50)
s2 := geometry.Sz(10, 20)
result := s1.Add(s2) // Size{110, 70}
func (Size) Area ¶
Area returns the area (Width * Height). Returns negative value if one dimension is negative.
Example:
s := geometry.Sz(100, 50) area := s.Area() // 5000
func (Size) AspectRatio ¶
AspectRatio returns Width / Height. Returns 0 if Height is zero.
func (Size) Clamp ¶
Clamp returns a new size with dimensions clamped to the range [minSize, maxSize].
Example:
s := geometry.Sz(150, 25)
minS := geometry.Sz(50, 50)
maxS := geometry.Sz(100, 100)
result := s.Clamp(minS, maxS) // Size{100, 50}
func (Size) Contains ¶
Contains returns true if s can contain other. Both dimensions of s must be >= corresponding dimensions of other.
Example:
s1 := geometry.Sz(100, 50) s2 := geometry.Sz(80, 40) s1.Contains(s2) // true
func (Size) Contract ¶
Contract returns a new size with dimensions decreased by delta on each edge. Total decrease is 2*delta for each dimension.
Example:
s := geometry.Sz(100, 50)
result := s.Contract(10) // Size{80, 30}
func (Size) Expand ¶
Expand returns a new size with dimensions increased by delta on each edge. Total increase is 2*delta for each dimension.
Example:
s := geometry.Sz(100, 50)
result := s.Expand(10) // Size{120, 70}
func (Size) FillIn ¶
FillIn returns a size that maintains aspect ratio while filling maxSize. The resulting size will have at least one dimension equal to maxSize, and will completely cover maxSize (may overflow in one dimension).
Example:
s := geometry.Sz(200, 100) // 2:1 aspect ratio
maxS := geometry.Sz(100, 100)
result := s.FillIn(maxS) // Size{200, 100}
func (Size) FitIn ¶
FitIn returns a size that maintains aspect ratio while fitting within maxSize. If the size already fits within maxSize, it is returned unchanged. Otherwise, the size is scaled down proportionally.
Example:
s := geometry.Sz(200, 100) // 2:1 aspect ratio
maxS := geometry.Sz(100, 100)
result := s.FitIn(maxS) // Size{100, 50}
func (Size) IsEmpty ¶
IsEmpty returns true if either dimension is zero or negative.
Example:
geometry.Sz(100, 50).IsEmpty() // false geometry.Sz(100, 0).IsEmpty() // true geometry.Sz(-1, 50).IsEmpty() // true
func (Size) Max ¶
Max returns a new size with the maximum dimensions of s and other.
Example:
s1 := geometry.Sz(100, 30)
s2 := geometry.Sz(80, 50)
result := s1.Max(s2) // Size{100, 50}
func (Size) Min ¶
Min returns a new size with the minimum dimensions of s and other.
Example:
s1 := geometry.Sz(100, 30)
s2 := geometry.Sz(80, 50)
result := s1.Min(s2) // Size{80, 30}
func (Size) Scale ¶
Scale returns a new size with both dimensions multiplied by the scalar.
Example:
s := geometry.Sz(100, 50)
result := s.Scale(2) // Size{200, 100}
func (Size) String ¶
String returns a string representation of the size.
Example:
s := geometry.Sz(100, 50) str := s.String() // "Size(100, 50)"