Documentation
¶
Index ¶
- Constants
- Variables
- func Abs[T Number](value T) T
- func AdjustDestinationPixel(x float32) float32
- func Clamp[T Number](value, min, max T) T
- func ClampTowardsZero[T Number](value, clampReference T) T
- func CubicInterpolate(from, to, pre, post, t float64) float64
- func EqualsApproximately[T Float](a, b T) bool
- func FastFloor[T Float, U Number](value T) U
- func Lerp[T Number](from, to, t T) T
- func Max[T Number](v1, v2 T) T
- func Min[T Number](v1, v2 T) T
- func RandomChoose[T any](r *Rand, elements ...T) (element T)
- func RandomElement[T any](r *Rand, slice []T) (element T)
- func RandomIndex[T any](r *Rand, slice []T) int
- func RandomShuffle[T any](r *Rand, slice []T)
- func Repeat(t, length float64) float64
- func Sign[T Number](value T) T
- func ToDegrees(radians float64) float64
- func ToRadians(degrees float64) float64
- type Float
- type Matrix
- type Number
- type Point
- type Rand
- func (self *Rand) Bool() bool
- func (self *Rand) Chance(probability float64) bool
- func (self *Rand) Float64() float64
- func (self *Rand) FloatRange(min, max float64) float64
- func (self *Rand) IntRange(min, max int) int
- func (self *Rand) NextFloat64(max float64) float64
- func (self *Rand) Offset(min, max float64) Vector
- func (self *Rand) PositiveInt() int
- func (self *Rand) PositiveInt64() int64
- func (self *Rand) Rad() float64
- func (self *Rand) SetSeed(seed int64)
- func (self *Rand) Uint64() uint64
- func (self *Rand) VectorRange(min, max Vector) Vector
- type RandPicker
- type Rectangle
- func (r Rectangle) Center() Vector
- func (r Rectangle) Contains(p Vector) bool
- func (r Rectangle) ContainsRect(other Rectangle) bool
- func (r Rectangle) Equals(other Rectangle) bool
- func (r Rectangle) GetAxis(angle float64, index int) Vector
- func (r Rectangle) GetCorners() [4]Vector
- func (r Rectangle) Height() float64
- func (r Rectangle) Intersects(other Rectangle) bool
- func (r Rectangle) IntersectsCircle(center Vector, radius float64) bool
- func (r Rectangle) IsEmpty() bool
- func (r Rectangle) OverlapOnAxis(other Rectangle, axis Vector) bool
- func (r Rectangle) ProjectOntoAxis(axis Vector) struct{ ... }
- func (r *Rectangle) SetAngle(angle float64)
- func (r Rectangle) Width() float64
- func (r Rectangle) X1() float64
- func (r Rectangle) X2() float64
- func (r Rectangle) Y1() float64
- func (r Rectangle) Y2() float64
- type Transform
- func (self *Transform) Abs() Transform
- func (self *Transform) AddScale(add ...Vector)
- func (self *Transform) Connect(parent Transformer)
- func (self *Transform) Connected() bool
- func (self *Transform) Disconnect()
- func (self *Transform) GetInitialParentTransform() *Transform
- func (self *Transform) GetParentTransform() *Transform
- func (self *Transform) GetTransform() *Transform
- func (self *Transform) IsDirty() bool
- func (self *Transform) Matrix() Matrix
- func (self *Transform) MatrixForParenting() (Matrix, Matrix)
- func (self *Transform) Move(v ...Vector)
- func (self *Transform) Offset() Vector
- func (self *Transform) Origin() Vector
- func (self *Transform) Position() Vector
- func (self *Transform) Rel() Transform
- func (self *Transform) Replace(new Transformer)
- func (self *Transform) Rotate(rotation float64)
- func (self *Transform) Rotation() float64
- func (self *Transform) Scale() Vector
- func (self *Transform) SetOffset(offset Vector)
- func (self *Transform) SetOrigin(origin Vector)
- func (self *Transform) SetPosition(position Vector)
- func (self *Transform) SetRotation(rotation float64)
- func (self *Transform) SetScale(scale Vector)
- type Transformer
- type Vector
- func (self Vector) Abs() Vector
- func (self Vector) Add(others ...Vector) Vector
- func (self Vector) AddF(scalar float64) Vector
- func (self Vector) Angle() float64
- func (self Vector) AngleToPoint(other Vector) float64
- func (self Vector) Apply(m Matrix) Vector
- func (self Vector) Ceil() Vector
- func (self Vector) ClampLength(limit float64) Vector
- func (self Vector) Cross(other Vector) float64
- func (self Vector) DirectionTo(other Vector) Vector
- func (self Vector) DistanceSquaredTo(v2 Vector) float64
- func (self Vector) DistanceTo(v2 Vector) float64
- func (self Vector) Div(other Vector) Vector
- func (self Vector) DivF(scalar float64) Vector
- func (self Vector) Dot(v2 Vector) float64
- func (self Vector) Equals(other Vector) bool
- func (self Vector) Extend(length float64) Vector
- func (self Vector) Floor() Vector
- func (self Vector) IsNormalized() bool
- func (self Vector) IsZero() bool
- func (self Vector) Length() float64
- func (self Vector) LengthSquared() float64
- func (v Vector) Lerp(other Vector, t float64) Vector
- func (self Vector) MoveInDirection(angle, distance float64) Vector
- func (self Vector) MoveTowards(other Vector, length float64) Vector
- func (self Vector) Negate() Vector
- func (self Vector) Normalize() Vector
- func (self Vector) Orthogonal() Vector
- func (self Vector) Reflect(normal Vector) Vector
- func (self Vector) Rotate(angle float64) Vector
- func (self Vector) RotateAround(around Vector, angle float64) Vector
- func (self Vector) RotateDegrees(degrees float64) Vector
- func (self Vector) Round() Vector
- func (self Vector) Scale(other Vector) Vector
- func (self Vector) ScaleF(scalar float64) Vector
- func (self Vector) Shorten(limit float64) Vector
- func (self Vector) String() string
- func (self Vector) Sub(others ...Vector) Vector
- func (self Vector) SubF(scalar float64) Vector
- func (self Vector) ToInt() (int, int)
- func (self Vector) VecTowards(other Vector, length float64) Vector
Constants ¶
const ( Pi = 3.141592653589793 Epsilon = 1e-9 )
Constants ---------
Variables ¶
var ( // ZeroVector represents a Vector at the origin (0, 0). ZeroVector = V2(0) // Right represents a unit vector pointing to the right (1, 0). Right = V(1, 0) // Left represents a unit vector pointing to the left (-1, 0). Left = Right.Negate() // Up represents a unit vector pointing up (0, 1). Up = V(0, 1) // Down represents a unit vector pointing down (0, -1). Down = Up.Negate() )
Functions ¶
func Abs ¶
func Abs[T Number](value T) T
Absolute and Sign Functions --------------------------- Abs returns the absolute value.
func AdjustDestinationPixel ¶
Pixel Adjustment ---------------- AdjustDestinationPixel adjusts the pixel position to avoid center issues in rendering.
func Clamp ¶
func Clamp[T Number](value, min, max T) T
Clamping and Rounding --------------------- Clamp restricts a value to be within specified bounds.
func ClampTowardsZero ¶
func ClampTowardsZero[T Number](value, clampReference T) T
ClampTowardsZero clamps a value towards zero based on another value's sign.
func CubicInterpolate ¶
CubicInterpolate performs cubic interpolation between values.
func EqualsApproximately ¶
Utility Functions for Floating Point Comparisons ------------------------------------------------ EqualsApproximately checks if two numbers of a generic float type are approximately equal.
func Lerp ¶
func Lerp[T Number](from, to, t T) T
Linear Interpolation -------------------- Lerp performs linear interpolation.
func Max ¶
func Max[T Number](v1, v2 T) T
Min and Max Functions --------------------- Max returns the larger of two values.
func RandomChoose ¶
RandomChoose selects a random element from the provided elements.
func RandomElement ¶
RandomElement selects a random element from the slice. Returns the zero value if the slice is empty.
func RandomIndex ¶
RandomIndex selects a random index from a slice. Returns -1 if the slice is empty.
func RandomShuffle ¶
RandomShuffle shuffles the elements of the slice in place.
func Sign ¶
func Sign[T Number](value T) T
Sign returns +1 for positive numbers, -1 for negative numbers, ignoring zero.
Types ¶
type Point ¶
type Point struct {
X, Y int
}
Point represents a point in 2D space with integer coordinates.
type Rand ¶
type Rand struct {
// contains filtered or unexported fields
}
Rand provides methods for generating random numbers with various distributions.
func Random ¶
func Random() *Rand
Random creates a new random number generator with the current time as seed.
func RandomWidthSeed ¶
RandomWidthSeed initializes a random number generator with a specific seed.
func (*Rand) FloatRange ¶
FloatRange returns a random float64 within the specified range [min, max).
func (*Rand) NextFloat64 ¶
NextFloat64 returns a random float64 in the range [0.0, max).
func (*Rand) Offset ¶
Offset generates a random Vector within the given range for both X and Y components.
func (*Rand) PositiveInt ¶
PositiveInt returns a non-negative random int.
func (*Rand) PositiveInt64 ¶
PositiveInt64 returns a non-negative random int64.
func (*Rand) SetSeed ¶
SetSeed sets the seed for the random number generator, allowing for reproducible randomness.
func (*Rand) VectorRange ¶
VectorRange returns a random Vector within the specified range for both X and Y.
type RandPicker ¶
type RandPicker[T any] struct { // contains filtered or unexported fields }
RandPicker for weighted random selection ---------------------------------------
func RandomPicker ¶
func RandomPicker[T any](r *Rand) *RandPicker[T]
RandomPicker creates a new RandPicker with the given random number generator.
func (*RandPicker[T]) AddOption ¶
func (self *RandPicker[T]) AddOption(value T, weight float64)
AddOption adds a new option to the picker with the given weight for selection probability.
func (*RandPicker[T]) AddOptions ¶
func (self *RandPicker[T]) AddOptions(values ...T)
AddOptions adds multiple options to the picker, each with a default weight of 1.
func (*RandPicker[T]) IsEmpty ¶
func (self *RandPicker[T]) IsEmpty() bool
IsEmpty checks if there are no options in the picker.
func (*RandPicker[T]) Pick ¶
func (self *RandPicker[T]) Pick() T
Pick selects a random option based on the weights provided. If no options exist, returns the zero value.
func (*RandPicker[T]) Reset ¶
func (self *RandPicker[T]) Reset()
Reset clears all options from the picker, resetting it to an empty state.
type Rectangle ¶
Rectangle represents a 2D rectangle with min and max vectors for bounds and an orientation angle.
func NewRectangle ¶
NewRectangle creates a new axis-aligned rectangle.
func (Rectangle) ContainsRect ¶
ContainsRect checks if one rectangle is completely inside another.
func (Rectangle) GetCorners ¶
GetCorners returns the four corners of the rectangle, considering rotation.
func (Rectangle) Intersects ¶
Intersects checks if two rectangles intersect using the Separating Axis Theorem. This handles rotated rectangles.
func (Rectangle) IntersectsCircle ¶
IntersectsCircle checks if the rectangle intersects with a circle.
func (Rectangle) OverlapOnAxis ¶
OverlapOnAxis checks if there's overlap along a specific axis.
func (Rectangle) ProjectOntoAxis ¶
ProjectOntoAxis projects the rectangle onto an axis, returning the min and max values.
type Transform ¶
type Transform struct {
// contains filtered or unexported fields
}
Transform represents a transformation in 2D space. It includes position, scale, rotation, and a parent for hierarchical transforms.
func T ¶
func T() *Transform
T creates a new Transform with default values. The default scale is 1, and the dirty flag is set to true.
func (*Transform) Abs ¶
Transform Modifiers ------------------- Abs returns a new transform with the absolute world properties of this transform, effectively disconnecting it from its parent hierarchy.
func (*Transform) Connect ¶
func (self *Transform) Connect(parent Transformer)
Connect establishes a parent-child relationship, preserving the object's world space transform.
func (*Transform) Connected ¶
Parent Management ----------------- Connected returns true if the transform has a parent.
func (*Transform) Disconnect ¶
func (self *Transform) Disconnect()
Disconnect removes the parent relationship, making the transform absolute. It does this by creating a new absolute transform and overwriting the current one.
func (*Transform) GetInitialParentTransform ¶
GetInitialParentTransform finds the topmost parent in the hierarchy. It iterates up the parent chain until it finds the root transform.
func (*Transform) GetParentTransform ¶
Methods for Parent Hierarchy ---------------------------- GetParentTransform returns the parent Transform or nil if there is no parent.
func (*Transform) GetTransform ¶
GetTransform returns this Transform. This method fulfills the Transformer interface.
func (*Transform) IsDirty ¶
IsDirty checks if this transform or any of its parents are dirty. This recursive check is used for cache invalidation.
func (*Transform) Matrix ¶
Matrix computes the full world transformation matrix for this node. The result is cached to avoid repeated calculations.
func (*Transform) MatrixForParenting ¶
Matrix Operations ----------------- MatrixForParenting returns matrices for child positioning. It returns the world matrix without the origin offset and its inverse. It ensures the world matrix is up-to-date by calling Matrix() if needed.
func (*Transform) Origin ¶
Transformation Properties ------------------------- Origin returns the origin of the transform.
func (*Transform) Position ¶
Position returns the absolute position in world space. It calculates the world position by applying the transform's world matrix to the zero vector.
func (*Transform) Replace ¶
func (self *Transform) Replace(new Transformer)
Replace updates this transform's local properties to match the world properties of another transform.
func (*Transform) Scale ¶
Scale returns the absolute scale in world space by multiplying with the parent's scale.
func (*Transform) SetPosition ¶
Position and Movement --------------------- SetPosition updates the position, preserving the world-space position by adjusting the local position based on the parent's inverse matrix.
func (*Transform) SetRotation ¶
Rotation -------- SetRotation updates the rotation, preserving the world-space rotation by adjusting the local rotation based on the parent's rotation.
type Transformer ¶
Transformer defines the interface for objects that have transforms.
type Vector ¶
type Vector struct {
X, Y float64
}
Vector represents a 2D vector with X and Y components.
func AngleToVector ¶
Vector and Angle ---------------- AngleToVector converts an angle in radians to a vector with a given length.
func V2Int ¶
V2Int creates a Vector where both X and Y are set to the integer value converted to float64.
func (Vector) AngleToPoint ¶
AngleToPoint returns the angle from this Vector towards another point.
func (Vector) Ceil ¶
Ceil returns a new Vector with each component rounded up to the nearest integer.
func (Vector) ClampLength ¶
ClampLength ensures the Vector's length does not exceed a given limit.
func (Vector) DirectionTo ¶
DirectionTo returns a normalized vector pointing from this Vector to another.
func (Vector) DistanceSquaredTo ¶
DistanceSquaredTo computes the squared distance to another Vector.
func (Vector) DistanceTo ¶
DistanceTo calculates the Euclidean distance to another Vector.
func (Vector) Div ¶
Div divides the Vector by another Vector component-wise, returning a new Vector.
func (Vector) Extend ¶ added in v1.2.0
Extend adds magnitude to the Vector in the direction it's already pointing.
func (Vector) Floor ¶
Floor returns a new Vector with each component rounded down to the nearest integer.
func (Vector) IsNormalized ¶
IsNormalized checks if the vector's length is approximately 1.
func (Vector) LengthSquared ¶
LengthSquared returns the square of the Vector's length.
func (Vector) Lerp ¶ added in v1.2.3
Lerp performs linear interpolation between two vectors. t is the interpolation factor, typically between 0 and 1.
func (Vector) MoveInDirection ¶
MoveInDirection moves the Vector in the direction of the angle by a given distance.
func (Vector) MoveTowards ¶
MoveTowards moves the Vector towards another Vector by a maximum distance.
func (Vector) Normalize ¶ added in v1.2.0
Normalize returns a unit vector in the same direction as this Vector.
func (Vector) Orthogonal ¶ added in v1.2.2
func (Vector) RotateAround ¶
RotateAround rotates this Vector around another Vector by an angle in radians.
func (Vector) RotateDegrees ¶
RotateDegrees rotates the Vector by degrees.
func (Vector) Round ¶
Round returns a new Vector with each component rounded to the nearest integer.
func (Vector) Scale ¶
Scale scales the Vector by another Vector component-wise, returning a new Vector.
func (Vector) Shorten ¶ added in v1.2.0
Shorten subtracts the given magnitude from the Vector's existing magnitude.
func (Vector) SubF ¶
SubF subtracts a scalar from both components of the Vector, returning a new Vector.