Documentation ¶
Overview ¶
A collection of math functions and types. Lots of wheel reinventing inside.
Index ¶
- Constants
- Variables
- func Abs[N Number](n N) N
- func ArrangeCategories(entities []Category)
- func Bound[N Number](a, low, high N) N
- func ChebyshevDistance(a, b Vector2[int]) int
- func Clamp[N Number](x, min, max N) N
- func Clockwise(a, b Vector2[float64]) bool
- func Diagonal(a, b Vector2[int]) bool
- func DistancePointLine(l1, l2, p Vector2[float64]) float64
- func DistancePointLineSeg(l1, l2, p Vector2[float64]) float64
- func DistancePointPoint[N Number](p1, p2 Vector2[N]) N
- func Divisor[N constraints.Integer](n N) N
- func Equals[N Number](a, b, e N) bool
- func Fade(t float64) float64
- func FilterAtPoint(kernel, g [][]float64, p Vector2[int]) (float64, error)
- func FilterInPlace(kernel, g [][]float64, p Vector2[int]) error
- func Gauss[F constraints.Float](x, mean, sigma F) F
- func InRange[N Number](a, b Vector2[N], r N) bool
- func Lerp(a, b, t float64) float64
- func ManhattanDistance(a, b Vector2[int]) int
- func MapHistogram(hmap [][]int) []int
- func Max[N Number](a, b N) N
- func Min[N Number](a, b N) N
- func NTable(hmap [][]int) [][]float64
- func NextPowerOf2(v uint64) uint64
- func NormalizeHist(d []int) []float64
- func Sample(d []float64, rejections int) (int, error)
- func Sample2D(d [][]float64, rejections int) (int, int, error)
- func VectorToOctant(a Vector2[float64]) int
- type Box2D
- func (b Box2D[N]) Contains(v Vector2[N]) bool
- func (b Box2D[N]) ContainsInc(v Vector2[N]) bool
- func (b *Box2D[N]) CropToFitIn(other Box2D[N])
- func (b Box2D[N]) Fit(v Vector2[N]) Vector2[N]
- func (b Box2D[N]) FromImageRect(r image.Rectangle) Box2D[N]
- func (b Box2D[N]) IsBox() bool
- func (b *Box2D[N]) MakeCanonical()
- func (b Box2D[N]) New(p1x, p1y, p2x, p2y N) Box2D[N]
- func (b Box2D[N]) Overlaps(other Box2D[N]) bool
- func (b Box2D[N]) P3() Vector2[N]
- func (b Box2D[N]) P4() Vector2[N]
- func (b Box2D[N]) Size() Vector2[N]
- func (b Box2D[N]) ToImageRect() image.Rectangle
- type ByWeight
- type Category
- type Matrix4
- func NewMatrix4Identity[N Number]() Matrix4[N]
- func NewMatrix4RotateX[N Number](angle N) Matrix4[N]
- func NewMatrix4RotateY[N Number](angle N) Matrix4[N]
- func NewMatrix4RotateZ[N Number](angle N) Matrix4[N]
- func NewMatrix4Scale[N Number](v Vector3[N]) Matrix4[N]
- func NewMatrix4Transform[N Number](translation, scale Vector3[N], rotationX, rotationY, rotationZ N) Matrix4[N]
- func NewMatrix4Translation[N Number](v Vector3[N]) Matrix4[N]
- func (m Matrix4[N]) Column(i int) []N
- func (m Matrix4[N]) Decompose() (translation, rotation, scale Vector3[N])
- func (ml Matrix4[N]) MulMatrix(mr Matrix4[N]) Matrix4[N]
- func (m Matrix4[N]) MulVector(p Vector3[N]) Vector3[N]
- func (m *Matrix4[N]) Populate(data [16]N)
- func (m *Matrix4[N]) RotationAxis(axis Vector3[N], angle float64)
- func (m *Matrix4[N]) RotationX(radians N)
- func (m *Matrix4[N]) RotationY(radians N)
- func (m *Matrix4[N]) RotationZ(radians N)
- func (m Matrix4[N]) Row(i int) []N
- func (m *Matrix4[N]) Scale(v Vector3[N])
- func (m *Matrix4[N]) SetIdentity()
- func (m Matrix4[N]) String() string
- func (m *Matrix4[N]) Translation(v Vector3[N])
- type Noise1D
- type Number
- type Vector2
- func Ellipse(a, b, cx, cy, t float64) Vector2[float64]
- func GridLine(p1, p2 Vector2[int], length int) []Vector2[int]
- func Line(p0, p1 Vector2[int]) []Vector2[int]
- func OctantToOffset(oct int) Vector2[int]
- func ScaleChess(v Vector2[int], l int) Vector2[int]
- func SymLine(p1, p2 Vector2[int]) []Vector2[int]
- func Vector2ConvertType[A Number, B Number](a Vector2[A]) Vector2[B]
- func VectorPointLine[N Number](l1, l2, p Vector2[N]) Vector2[N]
- func (v Vector2[N]) Add(b Vector2[N]) Vector2[N]
- func (v Vector2[N]) AddZ(z N) Vector3[N]
- func (v Vector2[N]) Div(b Vector2[N]) Vector2[N]
- func (v Vector2[N]) Dot(b Vector2[N]) N
- func (v Vector2[N]) Equals(b Vector2[N], e N) bool
- func (v Vector2[N]) Length() N
- func (a Vector2[N]) Lerp(b Vector2[N], t N) Vector2[N]
- func (v Vector2[N]) Mul(b Vector2[N]) Vector2[N]
- func (v Vector2[N]) Normalize() Vector2[N]
- func (v Vector2[N]) Scale(b N) Vector2[N]
- func (v Vector2[N]) Sub(b Vector2[N]) Vector2[N]
- func (v Vector2[N]) Zero() Vector2[N]
- type Vector3
- func (v Vector3[N]) Add(b Vector3[N]) Vector3[N]
- func (v Vector3[N]) Div(b Vector3[N]) Vector3[N]
- func (v Vector3[N]) Dot(b Vector3[N]) N
- func (v Vector3[N]) Equals(b Vector3[N], e N) bool
- func (v Vector3[N]) Length() N
- func (a Vector3[N]) Lerp(b Vector3[N], t N) Vector3[N]
- func (v Vector3[N]) Mul(b Vector3[N]) Vector3[N]
- func (v Vector3[N]) Normalize() Vector3[N]
- func (v Vector3[N]) Scale(b N) Vector3[N]
- func (v Vector3[N]) Sub(b Vector3[N]) Vector3[N]
- func (v Vector3[N]) XY() Vector2[N]
- func (v Vector3[N]) Zero() Vector3[N]
Constants ¶
const ( EpsilonVeryLax = 0.1 EpsilonLax = 0.001 Epsilon = 0.000001 EpsilonStrict = 0.000000001 EpsilonVeryStrict = 0.000000000001 )
Variables ¶
var KernelGaussian3x3 = [][]float64{
{0.0625, 0.1250, 0.0625},
{0.1250, 0.2500, 0.1250},
{0.0625, 0.1250, 0.0625},
}
var KernelLaplacian = [][]float64{
{0.0, 1.0, 0.0},
{1.0, -4.0, 1.0},
{0.0, 1.0, 0.0},
}
var KernelXAngle = [][]float64{
{0, 0, 0},
{-1, 0, 1},
{0, 0, 0},
}
var KernelYAngle = [][]float64{
{0, -1, 0},
{0, 0, 0},
{0, 1, 0},
}
Functions ¶
func ArrangeCategories ¶
func ArrangeCategories(entities []Category)
Sort by weight and make weights cumulative and normal
func Bound ¶
func Bound[N Number](a, low, high N) N
Return a if a in [low,high], low if a<low and high if a>high
func ChebyshevDistance ¶
func Clockwise ¶
Check if vector a is clockwise of vector b. Only works for angles up to Pi. (all vectors are clockwise if you want it bad enough).
func DistancePointLine ¶
Point-line d. Line defined by points l1, l2.
func DistancePointLineSeg ¶
Distance of point p flom line segment l1-l2.
func DistancePointPoint ¶
Distance between two points.
func Divisor ¶
func Divisor[N constraints.Integer](n N) N
func FilterAtPoint ¶
Same as FilterInPlace but returns the pixel value without applying it.
func FilterInPlace ¶
FilterInPlace applies a kernel at point p of grid g. Kernel dimensions should be odd numbers.
func ManhattanDistance ¶
func MapHistogram ¶
MapHistogram computes the histogram of an int heightmap.
func NTable ¶
Computes the neighbourhood table (ntable) of a hightmap. The ntable contains a histogram of neighbour frequencies for each height in the heightmap. It uses a 4-neighbourhood. Note: ntable is implemented as a 2D array which is ok for small height values but can be very inefficient if heights are large (it allocates max(height)^2). It should be reimplemented as a map if that is the case. TODO: bins
func NextPowerOf2 ¶
Next power of 2 after v (http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2).
func NormalizeHist ¶
Normalize distribution so that it adds up to 1.
func Sample ¶
Sample distribution d using rejection sampling. Returns an error if number of rejections exceeds rejections parameter. No checks on the validity (i.e adds up to 1) of d are made.
Types ¶
type Box2D ¶
func SegmentBox ¶
Segment an int box into (size)-sized smaller boxes.
func (Box2D[N]) ContainsInc ¶
Is v in the box (Upper bounds inclusive)
func (*Box2D[N]) CropToFitIn ¶
CropToFitIn box to fit into other box. Boxes must overlap partially.
func (*Box2D[N]) MakeCanonical ¶
func (b *Box2D[N]) MakeCanonical()
Make P1 min(x,y) and P2 max(x,y)
func (Box2D[N]) P3 ¶
Returns complementary point P3 which if the box is canonical is the upper-left one.
func (Box2D[N]) P4 ¶
Returns complementary point P4 which if the box is canonical is the lower-right one.
func (Box2D[N]) ToImageRect ¶
type Category ¶
func ChoseRandom ¶
Randomly choose some entry in categories given a random value r in [0, 1) Assumes entities are sorted and weights are cumulative (use Arrange)
type Matrix4 ¶
type Matrix4[N Number] struct { Elements [16]N }
func NewMatrix4Identity ¶
func NewMatrix4RotateX ¶
func NewMatrix4RotateY ¶
func NewMatrix4RotateZ ¶
func NewMatrix4Scale ¶
func NewMatrix4Transform ¶
func NewMatrix4Transform[N Number](translation, scale Vector3[N], rotationX, rotationY, rotationZ N) Matrix4[N]
Create a transformation matrix from a translation, XYZ rotations and scale
func NewMatrix4Translation ¶
func (Matrix4[N]) Decompose ¶
Return the translation, rotation and scale vectors that make up this matrix. TODO rotation
func (*Matrix4[N]) Populate ¶
func (m *Matrix4[N]) Populate(data [16]N)
Set matrix elements - data must be in rows.
func (*Matrix4[N]) RotationAxis ¶
Set to rotation around axis (unit vector)
func (*Matrix4[N]) RotationX ¶
func (m *Matrix4[N]) RotationX(radians N)
Set to rotation matrix around X
func (*Matrix4[N]) RotationY ¶
func (m *Matrix4[N]) RotationY(radians N)
Set to rotation matrix around Y
func (*Matrix4[N]) RotationZ ¶
func (m *Matrix4[N]) RotationZ(radians N)
Set to rotation matrix around Z
func (*Matrix4[N]) Translation ¶
Make m a translation matrix
type Noise1D ¶
type Noise1D struct {
// contains filtered or unexported fields
}
func (*Noise1D) Initialize ¶
Initialize a 1D noise array
type Number ¶
type Number interface { constraints.Integer | constraints.Float }
type Vector2 ¶
type Vector2[N Number] struct{ X, Y N }
func GridLine ¶
Calculate the cells to be filed for a grid line (Bresenham's line algorithm) The line can be restricted to a specific length.
func OctantToOffset ¶
Octant to pixel offset 3 2 1 -1, 1 0, 1 1, 1 4 0 -> -1, 0 1, 0 5 6 7 -1,-1 0,-1 1,-1
func ScaleChess ¶
Scale vector's length to be l (in Chebyshev distance)
func Vector2ConvertType ¶
Perform a type cast
func VectorPointLine ¶
Returns the vector perpendicular to line l1-l2 from p.
type Vector3 ¶
type Vector3[N Number] struct{ X, Y, Z N }
func Vector3ConvertType ¶
Perform a type cast
func (Vector3[N]) Normalize ¶
Normalize returns a unit vector with the same direction as v. Will not work on integers