geometry

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 2 Imported by: 0

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

View Source
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

type Constraints struct {
	MinWidth, MaxWidth   float32
	MinHeight, MaxHeight float32
}

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

func InsetsLTRB(left, top, right, bottom float32) Insets

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

func InsetsOnly(top, right, bottom, left float32) Insets

InsetsOnly creates insets with specific edges set, others zero.

Example:

topPadding := geometry.InsetsOnly(10, 0, 0, 0) // top only

func InsetsTRBL

func InsetsTRBL(top, right, bottom, left float32) Insets

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

func SymmetricInsets(horizontal, vertical float32) Insets

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

func UniformInsets(all float32) Insets

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) Abs

func (i Insets) Abs() Insets

Abs returns insets with all values converted to absolute values.

func (Insets) Add

func (i Insets) Add(other Insets) Insets

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

func (i Insets) BottomRight() Point

BottomRight returns the bottom-right offset as a Point.

func (Insets) Clamp

func (i Insets) Clamp(minInsets, maxInsets Insets) Insets

Clamp returns insets with values clamped to the range [minInsets, maxInsets].

func (Insets) Horizontal

func (i Insets) Horizontal() float32

Horizontal returns the sum of left and right insets.

Example:

padding := geometry.UniformInsets(16)
h := padding.Horizontal() // 32

func (Insets) IsNaN

func (i Insets) IsNaN() bool

IsNaN returns true if any value is NaN.

func (Insets) IsNonNegative

func (i Insets) IsNonNegative() bool

IsNonNegative returns true if all values are >= 0.

func (Insets) IsSymmetric

func (i Insets) IsSymmetric() bool

IsSymmetric returns true if horizontal edges match and vertical edges match.

func (Insets) IsUniform

func (i Insets) IsUniform() bool

IsUniform returns true if all edges have the same value.

func (Insets) IsZero

func (i Insets) IsZero() bool

IsZero returns true if all inset values are zero.

func (Insets) Max

func (i Insets) Max(other Insets) Insets

Max returns insets with the maximum values from i and other.

func (Insets) Min

func (i Insets) Min(other Insets) Insets

Min returns insets with the minimum values from i and other.

func (Insets) Negate

func (i Insets) Negate() Insets

Negate returns insets with all values negated.

func (Insets) Sanitize

func (i Insets) Sanitize() Insets

Sanitize returns a copy with NaN values replaced by zero.

func (Insets) Scale

func (i Insets) Scale(scalar float32) Insets

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

func (i Insets) Size() 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

func (i Insets) String() string

String returns a string representation of the insets.

Example:

i := geometry.UniformInsets(16)
s := i.String() // "Insets(16, 16, 16, 16)"

func (Insets) Sub

func (i Insets) Sub(other Insets) Insets

Sub returns new insets with values subtracted.

func (Insets) TopLeft

func (i Insets) TopLeft() Point

TopLeft returns the top-left offset as a Point.

func (Insets) Vertical

func (i Insets) Vertical() float32

Vertical returns the sum of top and bottom insets.

Example:

padding := geometry.UniformInsets(16)
v := padding.Vertical() // 32

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 Pt

func Pt(x, y float32) Point

Pt is a shorthand constructor for creating a Point.

Example:

p := geometry.Pt(10, 20)

func (Point) Add

func (p Point) Add(other Point) Point

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

func (p Point) Clamp(minP, maxP Point) Point

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

func (p Point) Distance(other Point) float32

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

func (p Point) DistanceSquared(other Point) float32

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

func (p Point) Div(other Point) Point

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

func (p Point) Dot(other Point) float32

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) IsNaN

func (p Point) IsNaN() bool

IsNaN returns true if either coordinate is NaN.

func (Point) IsZero

func (p Point) IsZero() bool

IsZero returns true if both coordinates are zero.

func (Point) Length

func (p Point) Length() float32

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

func (p Point) LengthSquared() float32

LengthSquared returns the squared length of the vector from origin to p. This is faster than Length when only comparing lengths.

func (Point) Lerp

func (p Point) Lerp(other Point, t float32) Point

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

func (p Point) Max(other Point) Point

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

func (p Point) Min(other Point) Point

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

func (p Point) Mul(other Point) Point

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

func (p Point) Negate() Point

Negate returns a new point with both coordinates negated.

Example:

p := geometry.Pt(10, -20)
result := p.Negate() // Point{-10, 20}

func (Point) Normalize

func (p Point) Normalize() Point

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) Sanitize

func (p Point) Sanitize() Point

Sanitize returns a copy of p with NaN values replaced by zero.

func (Point) Scale

func (p Point) Scale(scalar float32) Point

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}

func (Point) String

func (p Point) String() string

String returns a string representation of the point.

Example:

p := geometry.Pt(10.5, 20.25)
s := p.String() // "Point(10.5, 20.25)"

func (Point) Sub

func (p Point) Sub(other Point) Point

Sub returns a new point with the coordinates of other subtracted from p.

Example:

p1 := geometry.Pt(10, 20)
p2 := geometry.Pt(3, 5)
result := p1.Sub(p2) // Point{7, 15}

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

func FromCenter(center Point, s Size) Rect

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

func FromMinMax(p1, p2 Point) Rect

FromMinMax creates a rectangle from two corner points. The points are normalized so Min contains the smaller coordinates.

func FromPointSize

func FromPointSize(p Point, s Size) Rect

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

func NewRect(x, y, width, height float32) Rect

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) Area

func (r Rect) Area() float32

Area returns the area of the rectangle. Returns 0 for empty rectangles.

func (Rect) BottomLeft

func (r Rect) BottomLeft() Point

BottomLeft returns the bottom-left corner.

func (Rect) BottomRight

func (r Rect) BottomRight() Point

BottomRight returns the bottom-right corner (same as Max).

func (Rect) Center

func (r Rect) Center() Point

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

func (r Rect) Contains(p Point) bool

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

func (r Rect) ContainsRect(other Rect) bool

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

func (r Rect) Expand(delta float32) Rect

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) Height

func (r Rect) Height() float32

Height returns the height of the rectangle.

func (Rect) Inset

func (r Rect) Inset(insets Insets) Rect

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

func (r Rect) Intersection(other Rect) Rect

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

func (r Rect) Intersects(other Rect) bool

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

func (r Rect) IsEmpty() bool

IsEmpty returns true if the rectangle has zero or negative area. This can happen when Min >= Max in either dimension.

func (Rect) IsNaN

func (r Rect) IsNaN() bool

IsNaN returns true if any coordinate is NaN.

func (Rect) IsZero

func (r Rect) IsZero() bool

IsZero returns true if the rectangle is the zero value.

func (Rect) Normalize

func (r Rect) Normalize() Rect

Normalize returns a copy of r with Min and Max swapped if necessary so that Min contains the smaller coordinates.

func (Rect) Sanitize

func (r Rect) Sanitize() Rect

Sanitize returns a copy of r with NaN values replaced by zero.

func (Rect) Size

func (r Rect) Size() 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

func (r Rect) String() 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) TopLeft

func (r Rect) TopLeft() Point

TopLeft returns the top-left corner (same as Min).

func (Rect) TopRight

func (r Rect) TopRight() Point

TopRight returns the top-right corner.

func (Rect) Translate

func (r Rect) Translate(offset Point) Rect

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

func (r Rect) TranslateXY(dx, dy float32) Rect

TranslateXY returns a new rectangle moved by (dx, dy).

func (Rect) Union

func (r Rect) Union(other Rect) Rect

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) Width

func (r Rect) Width() float32

Width returns the width of the rectangle.

func (Rect) WithCenter

func (r Rect) WithCenter(center Point) Rect

WithCenter returns a new rectangle with the same size but centered at the given point.

func (Rect) WithSize

func (r Rect) WithSize(s Size) Rect

WithSize returns a new rectangle with the same Min point but different size.

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 Sz

func Sz(width, height float32) Size

Sz is a shorthand constructor for creating a Size.

Example:

s := geometry.Sz(100, 50)

func (Size) Add

func (s Size) Add(other Size) Size

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

func (s Size) Area() float32

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

func (s Size) AspectRatio() float32

AspectRatio returns Width / Height. Returns 0 if Height is zero.

func (Size) Clamp

func (s Size) Clamp(minSize, maxSize Size) Size

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

func (s Size) Contains(other Size) bool

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

func (s Size) Contract(delta float32) Size

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

func (s Size) Expand(delta float32) Size

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

func (s Size) FillIn(maxSize Size) Size

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

func (s Size) FitIn(maxSize Size) Size

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

func (s Size) IsEmpty() bool

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) IsNaN

func (s Size) IsNaN() bool

IsNaN returns true if either dimension is NaN.

func (Size) IsZero

func (s Size) IsZero() bool

IsZero returns true if both dimensions are zero.

func (Size) Max

func (s Size) Max(other Size) Size

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

func (s Size) Min(other Size) Size

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) Sanitize

func (s Size) Sanitize() Size

Sanitize returns a copy of s with NaN values replaced by zero.

func (Size) Scale

func (s Size) Scale(scalar float32) Size

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

func (s Size) String() string

String returns a string representation of the size.

Example:

s := geometry.Sz(100, 50)
str := s.String() // "Size(100, 50)"

func (Size) Sub

func (s Size) Sub(other Size) Size

Sub returns a new size with dimensions subtracted.

Example:

s1 := geometry.Sz(100, 50)
s2 := geometry.Sz(10, 20)
result := s1.Sub(s2) // Size{90, 30}

func (Size) ToPoint

func (s Size) ToPoint() Point

ToPoint converts the size to a point (Width -> X, Height -> Y). Useful for offset calculations.

Jump to

Keyboard shortcuts

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