geometry

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package geometry provides geometric types and operations for 2D space.

Vector2 is the primary type — a generic 2D point/vector supporting addition, subtraction, scaling, distance, magnitude, and unit-vector operations. Rectangle represents an axis-aligned bounding box. These types are used throughout the codebase for world coordinates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rectangle

type Rectangle struct {
	Min Vector2
	Max Vector2
}

Rectangle represents a rectangular area defined by its minimum and maximum positions.

func NewRectangle

func NewRectangle(min, max Vector2) Rectangle

NewRectangle creates and returns a new Rectangle with the given minimum and maximum positions.

func NewRectangleFromPoints

func NewRectangleFromPoints[T util.Number](minX, minY, maxX, maxY T) Rectangle

func SquareWithCenter

func SquareWithCenter(center Vector2, size float64) Rectangle

func (Rectangle) Height

func (r Rectangle) Height() float64

Height returns the height of the rectangle.

func (Rectangle) String

func (r Rectangle) String() string

func (Rectangle) Width

func (r Rectangle) Width() float64

Width returns the width of the rectangle.

type Vector2

type Vector2 struct {
	// contains filtered or unexported fields
}

Vector2 represents a 2D point in space.

func NewVector2

func NewVector2[T util.Number](x, y T) Vector2

NewVector2 creates and returns a new Vector2 with the given x and y values.

func RandomPointInRectangle

func RandomPointInRectangle(r Rectangle, rng util.Float64Source) Vector2

RandomPointInRectangle returns a random point within the given rectangle.

func Zero2D

func Zero2D() Vector2

Zero2D returns a zero Vector2, which is a vector with both x and y set to 0. It is a convenient way to create a zero vector without needing to specify the values.

func (Vector2) Add

func (p Vector2) Add(other Vector2) Vector2

Add returns a new Vector2 that is the sum of the current Vector2 and the given Vector2.

func (Vector2) AsFloats

func (p Vector2) AsFloats() (float64, float64)

AsFloats returns the vector components as floats.

func (Vector2) AsInts

func (p Vector2) AsInts() (int, int)

AsInts returns the vector components as integers.

func (Vector2) DistanceTo

func (p Vector2) DistanceTo(other Vector2) float64

DistanceTo calculates the Euclidean distance between two Vector2s.

func (Vector2) IsZero

func (p Vector2) IsZero() bool

IsZero returns true if the vector is the zero vector.

func (Vector2) Lerp added in v0.1.14

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

Lerp linearly interpolates between the current Vector2 and the given Vector2: it returns the current vector when t is 0 and the given vector when t is 1. It does not clamp t, so values outside [0,1] extrapolate along the line through both points. This method uses the form p*(1-t) + other*t to ensure the endpoints are exact in floating point arithmetic (at t=0 it returns p exactly, at t=1 it returns other exactly).

func (Vector2) Magnitude

func (p Vector2) Magnitude() float64

Magnitude calculates the length (magnitude) of the Vector2 vector.

func (Vector2) MarshalBinary added in v0.1.11

func (v Vector2) MarshalBinary() ([]byte, error)

MarshalBinary encodes the vector as 16 big-endian bytes (x then y, IEEE 754 bits). It implements encoding.BinaryMarshaler so vectors, and the engine components holding them, can be persisted in savegames (encoding/gob uses this implementation automatically).

func (Vector2) Max

func (p Vector2) Max(other Vector2) Vector2

Max returns a new Vector2 that is the maximum of the current Vector2 and the given Vector2.

func (Vector2) Min

func (p Vector2) Min(other Vector2) Vector2

Min returns a new Vector2 that is the minimum of the current Vector2 and the given Vector2.

func (Vector2) Scale

func (p Vector2) Scale(scalar float64) Vector2

Scale returns a new Vector2 that is the multiplication of the current Vector2 and the given scalar value.

func (Vector2) String

func (p Vector2) String() string

String returns a string representation of the Vector2.

func (Vector2) Sub

func (p Vector2) Sub(other Vector2) Vector2

Sub returns a new Vector2 that is the difference of the current Vector2 and the given Vector2.

func (Vector2) Unit

func (p Vector2) Unit() Vector2

Unit returns the normalized (unit) vector of the Vector2.

func (*Vector2) UnmarshalBinary added in v0.1.11

func (v *Vector2) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes 16 bytes produced by MarshalBinary.

func (Vector2) X

func (p Vector2) X() float64

X returns the x component of the vector.

func (Vector2) Y

func (p Vector2) Y() float64

Y returns the y component of the vector.

Jump to

Keyboard shortcuts

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