geom

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package geom provides Vector2 and the shapes Circle, Rect, and Segment for spatial calculations, along with Lerp and easing curves for interpolation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EaseInCubic added in v0.2.0

func EaseInCubic(t float64) float64

EaseInCubic returns a cubic easing weight that starts slowly and accelerates.

func EaseInOutCubic added in v0.2.0

func EaseInOutCubic(t float64) float64

EaseInOutCubic returns a cubic easing weight that accelerates in the first half and decelerates in the second.

func EaseInOutQuad added in v0.2.0

func EaseInOutQuad(t float64) float64

EaseInOutQuad returns a quadratic easing weight that accelerates in the first half and decelerates in the second.

func EaseInQuad added in v0.2.0

func EaseInQuad(t float64) float64

EaseInQuad returns a quadratic easing weight that starts slowly and accelerates.

func EaseLinear added in v0.2.0

func EaseLinear(t float64) float64

EaseLinear returns t unchanged for constant-speed interpolation.

func EaseNames added in v0.4.0

func EaseNames() []string

EaseNames returns the registered ease names in unspecified order.

func EaseOutCubic added in v0.2.0

func EaseOutCubic(t float64) float64

EaseOutCubic returns a cubic easing weight that starts quickly and decelerates.

func EaseOutQuad added in v0.2.0

func EaseOutQuad(t float64) float64

EaseOutQuad returns a quadratic easing weight that starts quickly and decelerates.

func Lerp added in v0.2.0

func Lerp(from, to, weight float64) float64

Lerp linearly interpolates from from to to by weight. Values outside [0, 1] extrapolate beyond the endpoints.

Types

type Circle added in v0.0.2

type Circle struct {
	// Center is the center point.
	Center Vector2
	// Radius is the radius; its magnitude is used, so negative values behave
	// like their positive counterparts.
	Radius float64
}

Circle is a two-dimensional circle represented by its center and radius.

func (Circle) Area added in v0.0.2

func (c Circle) Area() float64

Area returns the area enclosed by the circle.

func (Circle) BoundingBox added in v0.0.2

func (c Circle) BoundingBox() Rect

BoundingBox returns the smallest Rect containing c.

func (Circle) Circumference added in v0.0.2

func (c Circle) Circumference() float64

Circumference returns the circle's circumference.

func (Circle) Contains added in v0.0.2

func (c Circle) Contains(point Vector2) bool

Contains reports whether point lies inside or on the circle's boundary.

func (Circle) OverlapsOrTouches added in v0.1.0

func (c Circle) OverlapsOrTouches(other Circle) bool

OverlapsOrTouches reports whether c and other overlap or share a boundary point.

func (Circle) OverlapsOrTouchesRect added in v0.1.0

func (c Circle) OverlapsOrTouchesRect(r Rect) bool

OverlapsOrTouchesRect reports whether c overlaps or touches r. It returns false when r is invalid.

type Ease added in v0.2.0

type Ease func(t float64) float64

Ease maps normalized time to an interpolation weight. Use the result with Lerp or Vector2.Lerp to shape motion.

The curves provided by this package map [0, 1] to [0, 1], with endpoints at 0 and 1. They do not clamp inputs outside this range.

func EaseByName added in v0.4.0

func EaseByName(name string) (Ease, bool)

EaseByName returns the registered ease for name and whether one exists.

type Rect

type Rect struct {
	// Min is the lower coordinate bound on each axis.
	Min Vector2
	// Max is the upper coordinate bound on each axis.
	Max Vector2
}

Rect is a two-dimensional axis-aligned bounding rectangle. Canonical rectangles have Min no greater than Max on either axis.

func RectFromCenterSize

func RectFromCenterSize(center, size Vector2) Rect

RectFromCenterSize returns a rectangle centered at center with the given size. Negative size components produce the same canonical bounds as their absolute values.

func RectFromMinMax

func RectFromMinMax(min, max Vector2) Rect

RectFromMinMax returns a rectangle from two opposite corners. The corners are normalized so Min is no greater than Max on either axis.

func (Rect) Area

func (r Rect) Area() float64

Area returns the rectangle's area. Invalid or non-canonical rectangles have zero area.

func (Rect) BoundingBox

func (r Rect) BoundingBox() Rect

BoundingBox returns r because a rectangle is already axis-aligned.

func (Rect) Center

func (r Rect) Center() Vector2

Center returns the midpoint between the rectangle's corners.

func (Rect) ClosestPoint

func (r Rect) ClosestPoint(point Vector2) Vector2

ClosestPoint returns the point in r nearest to point. An invalid rectangle returns the zero vector.

func (Rect) Contains

func (r Rect) Contains(point Vector2) bool

Contains reports whether point lies inside or on the rectangle's boundary.

func (Rect) DistanceSquared

func (r Rect) DistanceSquared(point Vector2) float64

DistanceSquared returns the squared distance from point to r. It returns zero for points inside or on the rectangle, and for invalid rectangles.

func (Rect) Expand

func (r Rect) Expand(amount float64) Rect

Expand grows r outward by amount on every side. Negative amounts shrink the rectangle and clamp each extent at zero.

func (Rect) Extents

func (r Rect) Extents() Vector2

Extents returns half the rectangle's size in each dimension.

func (Rect) Height

func (r Rect) Height() float64

Height returns the distance between the rectangle's horizontal sides. It is negative when Max.Y is less than Min.Y.

func (Rect) IsEmpty

func (r Rect) IsEmpty() bool

IsEmpty reports whether r has zero width or height, or is invalid.

func (Rect) IsValid

func (r Rect) IsValid() bool

IsValid reports whether the rectangle has finite, canonical bounds.

func (Rect) Normalize

func (r Rect) Normalize() Rect

Normalize returns r with its bounds ordered on each axis.

func (Rect) OverlapsOrTouches

func (r Rect) OverlapsOrTouches(other Rect) bool

OverlapsOrTouches reports whether two rectangles overlap or share any boundary point.

func (Rect) Size

func (r Rect) Size() Vector2

Size returns the rectangle's width and height.

func (Rect) String

func (r Rect) String() string

String returns a human-readable representation of r.

func (Rect) Union

func (r Rect) Union(other Rect) Rect

Union returns the smallest rectangle containing r and other.

func (Rect) Width

func (r Rect) Width() float64

Width returns the distance between the rectangle's vertical sides. It is negative when Max.X is less than Min.X.

type Segment added in v0.0.2

type Segment struct {
	// Start is one endpoint of the segment.
	Start Vector2
	// End is the other endpoint of the segment.
	End Vector2
}

Segment is a finite line segment between Start and End.

func (Segment) BoundingBox added in v0.0.2

func (s Segment) BoundingBox() Rect

BoundingBox returns the canonical axis-aligned Rect containing s.

func (Segment) ClosestPoint added in v0.0.2

func (s Segment) ClosestPoint(point Vector2) Vector2

ClosestPoint returns the point on the segment nearest to point. A zero-length segment returns its single endpoint.

func (Segment) DistanceToPoint added in v0.0.2

func (s Segment) DistanceToPoint(point Vector2) float64

DistanceToPoint returns the shortest distance from point to s. Points on the segment have zero distance.

func (Segment) Length added in v0.0.2

func (s Segment) Length() float64

Length returns the distance between the segment's endpoints.

func (Segment) Midpoint added in v0.0.2

func (s Segment) Midpoint() Vector2

Midpoint returns the point halfway between the segment's endpoints.

type Vector2

type Vector2 struct {
	// X is the horizontal component.
	X float64 `json:"x"`
	// Y is the vertical component.
	Y float64 `json:"y"`
}

Vector2 is a two-dimensional vector.

func (Vector2) Add

func (v Vector2) Add(other Vector2) Vector2

Add returns the component-wise sum of v and other.

func (Vector2) AlmostEqual

func (v Vector2) AlmostEqual(other Vector2, epsilon float64) bool

AlmostEqual reports whether the components of v and other differ by no more than epsilon. Negative epsilon always returns false.

func (Vector2) Angle

func (v Vector2) Angle(other Vector2) float64

Angle returns the signed angle in radians from v to other. Positive values indicate counter-clockwise rotation; zero vectors have no meaningful angle.

func (Vector2) AngleDeg

func (v Vector2) AngleDeg(other Vector2) float64

AngleDeg returns the signed angle in degrees from v to other.

func (Vector2) Clamp

func (v Vector2) Clamp(min, max Vector2) Vector2

Clamp clamps each component of v to the corresponding range in min and max. Reversed bounds are normalized per component before clamping.

func (Vector2) ClampMagnitude

func (v Vector2) ClampMagnitude(maxLength float64) Vector2

ClampMagnitude limits v's magnitude to maxLength without changing its direction. A non-positive maxLength returns a zero vector.

func (Vector2) Cross

func (v Vector2) Cross(other Vector2) float64

Cross returns the scalar two-dimensional cross product of v and other. A positive result means other is counter-clockwise from v; a negative result means clockwise.

func (Vector2) Distance

func (v Vector2) Distance(other Vector2) float64

Distance returns the Euclidean distance between v and other. Use DistanceSquared when only comparing distances.

func (Vector2) DistanceSquared

func (v Vector2) DistanceSquared(other Vector2) float64

DistanceSquared returns the squared Euclidean distance between v and other. It avoids a square root when only comparing distances.

func (Vector2) Div

func (v Vector2) Div(scalar float64) Vector2

Div returns v divided by scalar. If the scalar is zero, it returns a zero vector to avoid infinities or NaNs.

func (Vector2) Dot

func (v Vector2) Dot(other Vector2) float64

Dot returns the scalar dot product of v and other.

func (Vector2) IsZero

func (v Vector2) IsZero() bool

IsZero reports whether both components of v are exactly zero. It is not an epsilon comparison and returns false for non-finite values.

func (Vector2) Length

func (v Vector2) Length() float64

Length returns the Euclidean magnitude of v.

func (Vector2) LengthSquared

func (v Vector2) LengthSquared() float64

LengthSquared returns the squared Euclidean magnitude of v.

func (Vector2) Lerp

func (v Vector2) Lerp(other Vector2, t float64) Vector2

Lerp linearly interpolates from v to other by t. Values outside [0, 1] extrapolate beyond the endpoints.

func (Vector2) Max

func (v Vector2) Max(other Vector2) Vector2

Max returns a vector containing the maximum components of this vector and another vector.

func (Vector2) Min

func (v Vector2) Min(other Vector2) Vector2

Min returns a vector containing the minimum components of this vector and another vector.

func (Vector2) Mul

func (v Vector2) Mul(scalar float64) Vector2

Mul returns v scaled by scalar.

func (Vector2) Neg

func (v Vector2) Neg() Vector2

Neg returns v with both components negated.

func (Vector2) Normalize

func (v Vector2) Normalize() Vector2

Normalize returns a unit vector in v's direction. A zero vector returns a zero vector; non-finite input propagates non-finite results.

func (Vector2) Project

func (v Vector2) Project(onto Vector2) Vector2

Project returns the component of v parallel to onto. Projection onto a zero vector returns a zero vector.

func (Vector2) Reflect

func (v Vector2) Reflect(normal Vector2) Vector2

Reflect returns v reflected across a surface with the given normal. The normal must be unit length; a zero normal returns v unchanged.

func (Vector2) Rotate

func (v Vector2) Rotate(angle float64) Vector2

Rotate returns v rotated counter-clockwise by angle radians. Rotation preserves the vector's magnitude apart from floating-point error.

func (Vector2) String

func (v Vector2) String() string

String returns the string representation of the vector.

func (Vector2) Sub

func (v Vector2) Sub(other Vector2) Vector2

Sub returns the component-wise difference between v and other.

Jump to

Keyboard shortcuts

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