geometry

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package geometry is go-gfx's floating-point 2-D geometry substrate: a Point, an axis-aligned Rect, and a 2-D affine Matrix (translate / scale / rotate / shear, composed, inverted, and applied to points and rectangles).

Everything here is in continuous float64 coordinates, deliberately distinct from the standard library's integer image.Point / image.Rectangle (which raster keeps using for pixel-grid bounds): a vector transform needs sub-pixel positions, so this layer never rounds until a consumer asks it to. The types are small value types passed and returned by value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matrix

type Matrix struct {
	Xx, Yx, X0 float64
	Xy, Yy, Y0 float64
}

Matrix is a 2-D affine transform stored as the six meaningful entries of a 3x3 homogeneous matrix (the bottom row is always [0 0 1]):

| Xx Yx X0 |        x' = Xx*x + Yx*y + X0
| Xy Yy Y0 |        y' = Xy*x + Yy*y + Y0
|  0  0  1 |

It maps a Point with Matrix.TransformPoint. Transforms compose with Matrix.Mul: m.Mul(n) is the transform that applies n first and then m, so m.Mul(n).TransformPoint(p) equals m.TransformPoint(n.TransformPoint(p)). A Matrix is a small value type, passed and returned by value.

func Identity

func Identity() Matrix

Identity returns the transform that leaves every point unchanged.

func Rotate

func Rotate(theta float64) Matrix

Rotate returns the transform that rotates about the origin by theta radians, counter-clockwise in a y-down (screen) coordinate system.

func Scale

func Scale(sx, sy float64) Matrix

Scale returns the transform that scales the axes by (sx, sy) about the origin.

func Shear

func Shear(shx, shy float64) Matrix

Shear returns the transform that shears by (shx, shy): x gains shx*y and y gains shy*x.

func Translate

func Translate(tx, ty float64) Matrix

Translate returns the transform that shifts every point by (tx, ty).

func (Matrix) Determinant

func (m Matrix) Determinant() float64

Determinant returns the determinant of the transform's linear part (Xx*Yy - Yx*Xy). Its magnitude is the area-scaling factor; a zero value means the transform collapses the plane and cannot be inverted.

func (Matrix) Invert

func (m Matrix) Invert() (inv Matrix, ok bool)

Invert returns the inverse transform and ok=true, or the zero Matrix and ok=false when the transform is singular (zero determinant). When ok is true, inv.Mul(m) is the identity up to floating-point rounding.

func (Matrix) IsInvertible

func (m Matrix) IsInvertible() bool

IsInvertible reports whether the transform has a non-zero determinant and can therefore be inverted.

func (Matrix) Mul

func (m Matrix) Mul(n Matrix) Matrix

Mul returns the composed transform m*n: the one that applies n first and then m to a point.

func (Matrix) TransformPoint

func (m Matrix) TransformPoint(p Point) Point

TransformPoint returns p mapped through the transform.

func (Matrix) TransformRect

func (m Matrix) TransformRect(r Rect) Rect

TransformRect returns the axis-aligned bounding box of r's four corners after they are mapped through the transform. Under a rotation or shear the mapped rectangle is no longer axis-aligned, so this reports the tight enclosing upright box rather than the true (rotated) quadrilateral.

type Point

type Point struct{ X, Y float64 }

Point is a location in continuous 2-D space.

func Pt

func Pt(x, y float64) Point

Pt is shorthand for Point{x, y}.

func (Point) Add

func (p Point) Add(q Point) Point

Add returns the vector sum p+q.

func (Point) Mul

func (p Point) Mul(s float64) Point

Mul returns p scaled by s.

func (Point) Sub

func (p Point) Sub(q Point) Point

Sub returns the vector difference p-q.

type Rect

type Rect struct{ Min, Max Point }

Rect is an axis-aligned rectangle spanning [Min.X, Max.X] x [Min.Y, Max.Y]. It is well-formed ("canonical") when Min <= Max on both axes; the query and set methods assume canonical input, and Rect.Canon repairs one that is not.

func Rectangle

func Rectangle(x0, y0, x1, y1 float64) Rect

Rectangle returns the canonical Rect with the given corner coordinates, swapping ends as needed so Min <= Max on each axis.

func (Rect) Canon

func (r Rect) Canon() Rect

Canon returns the rectangle with its corners ordered so Min <= Max on each axis.

func (Rect) Contains

func (r Rect) Contains(p Point) bool

Contains reports whether p lies within the closed rectangle (edges included).

func (Rect) Dx

func (r Rect) Dx() float64

Dx returns the rectangle's width, Max.X-Min.X.

func (Rect) Dy

func (r Rect) Dy() float64

Dy returns the rectangle's height, Max.Y-Min.Y.

func (Rect) Empty

func (r Rect) Empty() bool

Empty reports whether the rectangle encloses no area (zero or inverted on either axis).

func (Rect) Intersect

func (r Rect) Intersect(s Rect) Rect

Intersect returns the overlap of r and s. When they do not overlap it returns the zero Rect (which is empty).

func (Rect) Union

func (r Rect) Union(s Rect) Rect

Union returns the smallest rectangle that contains both r and s. An empty operand contributes nothing, so the union of an empty rectangle with s is s.

Jump to

Keyboard shortcuts

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