Documentation
¶
Index ¶
- Variables
- func Choose[T any](m *MathHelper, args ...T) T
- func ChooseFromArray[T any](m *MathHelper, arr []T) (T, bool)
- func ChooseFromArrayN[T any](m *MathHelper, arr []T, n int) []T
- func ChooseWeighted[T any](m *MathHelper, weights []float32, args ...T) T
- func Shuffle[T any](m *MathHelper, arr []T)
- type MathHelper
- func (m MathHelper) Angle(x1, y1, x2, y2 float32) float32
- func (m MathHelper) AngleAdd(angle, amount float32) float32
- func (m MathHelper) AngleAddV(dir Vec2, amountDeg float32) Vec2
- func (m MathHelper) AngleDif(angle1, angle2 float32) float32
- func (m MathHelper) AngleDifV(v1, v2 Vec2) float32
- func (m MathHelper) AngleV(x1, y1, x2, y2 float32) Vec2
- func (m MathHelper) Approach(current, target, amount float32) float32
- func (m MathHelper) Clamp(val, min, max float32) float32
- func (m MathHelper) CompareDistance(x1, y1, x2, y2, x3, y3, x4, y4 float32) int
- func (m MathHelper) DirFromDeg(deg float32) Vec2
- func (m MathHelper) Distance(x1, y1, x2, y2 float32) float32
- func (m MathHelper) DistanceSq(x1, y1, x2, y2 float32) float32
- func (m MathHelper) IsInFOV(ox, oy float32, forward Vec2, tx, ty, halfFOVDeg float32) bool
- func (m MathHelper) LengthDirV(length float32, dir Vec2) Vec2
- func (m MathHelper) LengthDirX(length, direction float32) float32
- func (m MathHelper) LengthDirY(length, direction float32) float32
- func (m MathHelper) Lerp(a, b, t float32) float32
- func (m *MathHelper) RandomArray(n int, min, max float32) []float32
- func (m *MathHelper) RandomArray2D(rows, cols int, min, max float32) [][]float32
- func (m *MathHelper) RandomArray2DInt(rows, cols, min, max int) [][]int
- func (m *MathHelper) RandomArrayInt(n, min, max int) []int
- func (m *MathHelper) RandomBool() bool
- func (m *MathHelper) RandomChance(p float32) bool
- func (m *MathHelper) RandomRangeDouble(min, max float32) float32
- func (m *MathHelper) RandomRangeDouble64(min, max float64) float64
- func (m *MathHelper) RandomRangeInt(min, max int) int
- func (m *MathHelper) RandomRangeInt32(min, max int32) int32
- func (m MathHelper) Reflect(v, normal Vec2) Vec2
- func (m MathHelper) RotateV(vec, dir Vec2) Vec2
- func (m *MathHelper) SetSeed(seed int64)
- func (m MathHelper) Sign(val float64) int
- func (m MathHelper) SlerpDir(from, to Vec2, t float32) Vec2
- func (m MathHelper) SlerpDirDeg(fromDeg, toDeg, t float32) float32
- func (m MathHelper) Snap(val, step float32) float32
- func (m MathHelper) ToDeg(v Vec2) float32
- func (m MathHelper) Wrap(val, min, max float32) float32
- type Vec2
- func (a Vec2) Add(b Vec2) Vec2
- func (a Vec2) Conj() Vec2
- func (a Vec2) Cross(b Vec2) float32
- func (a Vec2) Dot(b Vec2) float32
- func (a Vec2) Len() float32
- func (a Vec2) LenSq() float32
- func (a Vec2) Mul(b Vec2) Vec2
- func (a Vec2) Norm() Vec2
- func (a Vec2) Scale(s float32) Vec2
- func (a Vec2) Sub(b Vec2) Vec2
- type WeightedTable
Constants ¶
This section is empty.
Variables ¶
var (
Once sync.Once
)
Functions ¶
func Choose ¶
func Choose[T any](m *MathHelper, args ...T) T
Purpose: Randomly chooses one element from the provided arguments. Inputs: m (*MathHelper) - The math helper instance, args (...T) - The elements to choose from. Outputs: (T) - The chosen element.
func ChooseFromArray ¶
func ChooseFromArray[T any](m *MathHelper, arr []T) (T, bool)
Purpose: Randomly chooses one element from a slice. Inputs: m (*MathHelper), arr ([]T) - The slice to choose from. Outputs: (T) - The chosen element, (bool) - True if successful, false if slice is empty.
func ChooseFromArrayN ¶
func ChooseFromArrayN[T any](m *MathHelper, arr []T, n int) []T
Purpose: Randomly chooses n unique elements from a slice. Inputs: m (*MathHelper), arr ([]T) - The slice, n (int) - Number of elements to choose. Outputs: ([]T) - A slice containing the chosen elements.
func ChooseWeighted ¶
func ChooseWeighted[T any](m *MathHelper, weights []float32, args ...T) T
Purpose: Randomly chooses one element from the arguments, using the provided weights. Inputs: m (*MathHelper), weights ([]float32) - The weights, args (...T) - The elements to choose from. Outputs: (T) - The chosen element.
func Shuffle ¶
func Shuffle[T any](m *MathHelper, arr []T)
Purpose: Shuffles a slice in-place using Fisher-Yates algorithm. Inputs: m (*MathHelper), arr ([]T) - The slice to shuffle.
Types ¶
type MathHelper ¶
type MathHelper struct {
// contains filtered or unexported fields
}
func GetInstance ¶
func GetInstance() MathHelper
Purpose: Returns a singleton instance of MathHelper.
func NewMathHelper ¶
func NewMathHelper() MathHelper
Purpose: Creates a new MathHelper with a random seed derived from the runtime. Outputs: (MathHelper) - The newly created MathHelper.
func (MathHelper) Angle ¶
func (m MathHelper) Angle(x1, y1, x2, y2 float32) float32
Purpose: Calculates the angle in degrees from point 1 to point 2. Inputs: x1, y1 (float32) - Start point, x2, y2 (float32) - End point. Outputs: (float32) - Angle in degrees [-180, 180].
func (MathHelper) AngleAdd ¶
func (m MathHelper) AngleAdd(angle, amount float32) float32
Purpose: Adds an amount in degrees to an angle, handling wrapping automatically. Inputs: angle (float32) - Base angle, amount (float32) - Amount to add in degrees. Outputs: (float32) - The resulting wrapped angle.
func (MathHelper) AngleAddV ¶
func (m MathHelper) AngleAddV(dir Vec2, amountDeg float32) Vec2
Purpose: Adds an angle in degrees to a direction vector. Inputs: dir (Vec2) - The initial direction vector, amountDeg (float32) - Angle to add in degrees. Outputs: (Vec2) - The rotated unit vector.
func (MathHelper) AngleDif ¶
func (m MathHelper) AngleDif(angle1, angle2 float32) float32
Purpose: Calculates the shortest angular difference between two angles in degrees. Inputs: angle1, angle2 (float32) - Angles in degrees. Outputs: (float32) - The shortest difference in degrees [-180, 180].
func (MathHelper) AngleDifV ¶
func (m MathHelper) AngleDifV(v1, v2 Vec2) float32
Purpose: Calculates the angular difference between two unit vectors. Inputs: v1, v2 (Vec2) - The unit vectors. Outputs: (float32) - The shortest angular difference in degrees.
func (MathHelper) AngleV ¶
func (m MathHelper) AngleV(x1, y1, x2, y2 float32) Vec2
Purpose: Calculates the unit direction vector from point 1 to point 2. Inputs: x1, y1 (float32) - Start point, x2, y2 (float32) - End point. Outputs: (Vec2) - Unit direction vector.
func (MathHelper) Approach ¶
func (m MathHelper) Approach(current, target, amount float32) float32
Purpose: Approaches a target value by a given amount. Inputs: current (float32) - Current value, target (float32) - Target value, amount (float32) - Maximum change step. Outputs: (float32) - The new value closer to the target.
func (MathHelper) Clamp ¶
func (m MathHelper) Clamp(val, min, max float32) float32
Purpose: Clamps a value between a minimum and maximum. Inputs: val (float32) - Value to clamp, min (float32) - Minimum value, max (float32) - Maximum value. Outputs: (float32) - Clamped value.
func (MathHelper) CompareDistance ¶
func (m MathHelper) CompareDistance(x1, y1, x2, y2, x3, y3, x4, y4 float32) int
Purpose: Compares the squared distance between (x1, y1) -> (x2, y2) and (x3, y3) -> (x4, y4). Outputs: (int) - Returns 1 if first distance is greater, -1 if smaller, 0 if equal.
func (MathHelper) DirFromDeg ¶
func (m MathHelper) DirFromDeg(deg float32) Vec2
Purpose: Creates a unit vector from an angle in degrees. Inputs: deg (float32) - Angle in degrees. Outputs: (Vec2) - The resulting unit direction vector.
func (MathHelper) Distance ¶
func (m MathHelper) Distance(x1, y1, x2, y2 float32) float32
Purpose: Calculates the Euclidean distance between two points. Inputs: x1, y1 (float32) - First point, x2, y2 (float32) - Second point. Outputs: (float32) - The distance.
func (MathHelper) DistanceSq ¶
func (m MathHelper) DistanceSq(x1, y1, x2, y2 float32) float32
Purpose: Calculates the squared Euclidean distance between two points (faster than Distance as it skips Sqrt). Inputs: x1, y1 (float32) - First point, x2, y2 (float32) - Second point. Outputs: (float32) - The squared distance.
func (MathHelper) IsInFOV ¶
func (m MathHelper) IsInFOV(ox, oy float32, forward Vec2, tx, ty, halfFOVDeg float32) bool
Purpose: Checks if a target point is within the field of view of an observer. Inputs: ox, oy (float32) - Observer coordinates, forward (Vec2) - Observer's forward direction vector, tx, ty (float32) - Target coordinates, halfFOVDeg (float32) - Half the FOV angle in degrees. Outputs: (bool) - True if the target is within the FOV.
func (MathHelper) LengthDirV ¶
func (m MathHelper) LengthDirV(length float32, dir Vec2) Vec2
Purpose: Returns a scaled Vec2 from length and a unit direction vector. Inputs: length (float32) - The magnitude, dir (Vec2) - The unit direction vector. Outputs: (Vec2) - The resulting vector.
func (MathHelper) LengthDirX ¶
func (m MathHelper) LengthDirX(length, direction float32) float32
Purpose: Returns the X component of a vector given length and direction in degrees. Inputs: length (float32) - The length, direction (float32) - The angle in degrees. Outputs: (float32) - The X component.
func (MathHelper) LengthDirY ¶
func (m MathHelper) LengthDirY(length, direction float32) float32
Purpose: Returns the Y component of a vector given length and direction in degrees. Inputs: length (float32) - The length, direction (float32) - The angle in degrees. Outputs: (float32) - The Y component. Note: Y axis points downwards in 2D.
func (MathHelper) Lerp ¶
func (m MathHelper) Lerp(a, b, t float32) float32
Purpose: Performs linear interpolation between two values. Inputs: a (float32) - Start value, b (float32) - End value, t (float32) - Interpolation factor [0-1]. Outputs: (float32) - Interpolated value.
func (*MathHelper) RandomArray ¶
func (m *MathHelper) RandomArray(n int, min, max float32) []float32
Purpose: Generates a slice of random float32s in [min, max). Inputs: n (int) - Number of elements, min, max (float32) - Range. Outputs: ([]float32) - The generated slice.
func (*MathHelper) RandomArray2D ¶
func (m *MathHelper) RandomArray2D(rows, cols int, min, max float32) [][]float32
Purpose: Generates a 2D slice of random float32s in [min, max). Inputs: rows, cols (int) - Dimensions, min, max (float32) - Range. Outputs: ([][]float32) - The generated 2D slice.
func (*MathHelper) RandomArray2DInt ¶
func (m *MathHelper) RandomArray2DInt(rows, cols, min, max int) [][]int
Purpose: Generates a 2D slice of random ints in [min, max). Inputs: rows, cols (int) - Dimensions, min, max (int) - Range. Outputs: ([][]int) - The generated 2D slice.
func (*MathHelper) RandomArrayInt ¶
func (m *MathHelper) RandomArrayInt(n, min, max int) []int
Purpose: Generates a slice of random integers in [min, max). Inputs: n (int) - Number of elements, min, max (int) - Range. Outputs: ([]int) - The generated slice.
func (*MathHelper) RandomBool ¶
func (m *MathHelper) RandomBool() bool
Purpose: Returns a random boolean with 50/50 probability. Outputs: (bool) - True or false.
func (*MathHelper) RandomChance ¶
func (m *MathHelper) RandomChance(p float32) bool
Purpose: Returns true with a given probability p [0.0, 1.0]. Inputs: p (float32) - The probability of returning true. Outputs: (bool) - The result.
func (*MathHelper) RandomRangeDouble ¶
func (m *MathHelper) RandomRangeDouble(min, max float32) float32
Purpose: Generates a random float32 in the range [min, max). Inputs: min, max (float32) - The boundaries of the range. Outputs: (float32) - The generated random number.
func (*MathHelper) RandomRangeDouble64 ¶
func (m *MathHelper) RandomRangeDouble64(min, max float64) float64
Purpose: Generates a random float64 in the range [min, max). Inputs: min, max (float64) - The boundaries of the range. Outputs: (float64) - The generated random number.
func (*MathHelper) RandomRangeInt ¶
func (m *MathHelper) RandomRangeInt(min, max int) int
Purpose: Generates a random integer in the range [min, max). Inputs: min, max (int) - The boundaries of the range. Outputs: (int) - The generated random integer.
func (*MathHelper) RandomRangeInt32 ¶
func (m *MathHelper) RandomRangeInt32(min, max int32) int32
Purpose: Generates a random int32 in the range [min, max). Inputs: min, max (int32) - The boundaries of the range. Outputs: (int32) - The generated random integer.
func (MathHelper) Reflect ¶
func (m MathHelper) Reflect(v, normal Vec2) Vec2
Purpose: Reflects a vector across a surface normal. Inputs: v (Vec2) - The incoming vector, normal (Vec2) - The surface normal unit vector. Outputs: (Vec2) - The reflected vector.
func (MathHelper) RotateV ¶
func (m MathHelper) RotateV(vec, dir Vec2) Vec2
Purpose: Rotates a vector by a given direction vector (complex multiplication). Inputs: vec (Vec2) - The vector to rotate, dir (Vec2) - The rotation direction vector. Outputs: (Vec2) - The rotated vector.
func (*MathHelper) SetSeed ¶
func (m *MathHelper) SetSeed(seed int64)
Purpose: Initializes the RNG with a specific seed for reproducible results. Inputs: seed (int64) - The seed value to use.
func (MathHelper) Sign ¶
func (m MathHelper) Sign(val float64) int
Purpose: Returns the sign of a value. Outputs: (int) - 1 if positive, -1 if negative, 0 if zero.
func (MathHelper) SlerpDir ¶
func (m MathHelper) SlerpDir(from, to Vec2, t float32) Vec2
Purpose: Spherical linear interpolation between two direction vectors. Inputs: from, to (Vec2) - The unit vectors, t (float32) - Interpolation factor [0-1]. Outputs: (Vec2) - The interpolated unit vector.
func (MathHelper) SlerpDirDeg ¶
func (m MathHelper) SlerpDirDeg(fromDeg, toDeg, t float32) float32
Purpose: Wrapper around SlerpDir that operates with degree angles. Inputs: fromDeg, toDeg (float32) - Angles in degrees, t (float32) - Factor [0-1]. Outputs: (float32) - The interpolated angle in degrees.
func (MathHelper) Snap ¶
func (m MathHelper) Snap(val, step float32) float32
Purpose: Snaps a value to the nearest grid step. Inputs: val (float32) - Value to snap, step (float32) - Grid step size. Outputs: (float32) - Snapped value.
func (MathHelper) ToDeg ¶
func (m MathHelper) ToDeg(v Vec2) float32
Purpose: Converts a unit vector to an angle in degrees [-180, 180]. Inputs: v (Vec2) - The direction vector. Outputs: (float32) - The angle in degrees.
func (MathHelper) Wrap ¶
func (m MathHelper) Wrap(val, min, max float32) float32
Purpose: Wraps a value within a specified range [min, max). Inputs: val (float32) - Value to wrap, min (float32) - Minimum bound, max (float32) - Maximum bound. Outputs: (float32) - Wrapped value.
type Vec2 ¶
type Vec2 struct{ X, Y float32 }
Vec2 biểu diễn vector 2D hoặc số phức X + Yi.
func (Vec2) Add ¶
Purpose: Adds another Vec2 to this one. Inputs: b (Vec2) - The vector to add. Outputs: (Vec2) - The sum vector.
func (Vec2) Conj ¶
Purpose: Returns the complex conjugate (negating the imaginary part) of the Vec2. Outputs: (Vec2) - The conjugated vector.
func (Vec2) Cross ¶
Purpose: Calculates the 2D cross product (scalar) of this vector and another. Inputs: b (Vec2) - The other vector. Outputs: (float32) - The cross product value. Positive if b is left of a, negative if right.
func (Vec2) Dot ¶
Purpose: Calculates the dot product of this vector and another. Inputs: b (Vec2) - The other vector. Outputs: (float32) - The dot product value.
func (Vec2) Len ¶
Purpose: Calculates the actual length of the Vec2. Outputs: (float32) - The length.
func (Vec2) LenSq ¶
Purpose: Calculates the squared length of the vector. Outputs: (float32) - The squared length. Useful for fast distance comparisons without Sqrt.
func (Vec2) Mul ¶
Purpose: Multiplies two Vec2 instances treating them as complex numbers, equivalent to rotation and scaling. Inputs: b (Vec2) - The vector to multiply with. Outputs: (Vec2) - The result of the multiplication. If b is a unit vector, this results in pure rotation.
func (Vec2) Norm ¶
Purpose: Normalizes the Vec2 to a unit vector (length 1). Outputs: (Vec2) - The normalized unit vector.
type WeightedTable ¶
type WeightedTable[T any] struct { // contains filtered or unexported fields }
WeightedTable là cấu trúc tiền xử lý cho Alias Method. Build O(n), sau đó mỗi lần chọn O(1) — lý tưởng cho loot table gọi nhiều lần.
func NewWeightedTable ¶
func NewWeightedTable[T any](items []T, weights []float32) *WeightedTable[T]
Purpose: Creates a WeightedTable for O(1) weighted random selection using Alias Method. Inputs: items ([]T) - Items to pick from, weights ([]float32) - Corresponding weights. Outputs: (*WeightedTable[T]) - The created table.
func (*WeightedTable[T]) Choose ¶
func (t *WeightedTable[T]) Choose(m *MathHelper) T
Purpose: Chooses a random element from the WeightedTable in O(1) time. Inputs: m (*MathHelper). Outputs: (T) - The chosen element.