math

package module
v0.0.0-...-299a98a Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2024 License: Apache-2.0 Imports: 7 Imported by: 10

Documentation

Overview

A collection of math functions and types. Lots of wheel reinventing inside.

Index

Constants

View Source
const (
	EpsilonVeryLax    = 0.1
	EpsilonLax        = 0.001
	Epsilon           = 0.000001
	EpsilonStrict     = 0.000000001
	EpsilonVeryStrict = 0.000000000001
)

Variables

View Source
var KernelGaussian3x3 = [][]float64{
	{0.0625, 0.1250, 0.0625},
	{0.1250, 0.2500, 0.1250},
	{0.0625, 0.1250, 0.0625},
}
View Source
var KernelLaplacian = [][]float64{
	{0.0, 1.0, 0.0},
	{1.0, -4.0, 1.0},
	{0.0, 1.0, 0.0},
}
View Source
var KernelXAngle = [][]float64{
	{0, 0, 0},
	{-1, 0, 1},
	{0, 0, 0},
}
View Source
var KernelYAngle = [][]float64{
	{0, -1, 0},
	{0, 0, 0},
	{0, 1, 0},
}

Functions

func Abs

func Abs[N Number](n N) N

8-pack

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 ChebyshevDistance(a, b Vector2[int]) int

func Clamp

func Clamp[N Number](x, min, max N) N

Make sure x is in the range [min,max]

func Clockwise

func Clockwise(a, b Vector2[float64]) bool

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 Diagonal

func Diagonal(a, b Vector2[int]) bool

Is a diagonal to b [ ][A] or [A][ ] [B][ ] [ ][B]

func DistancePointLine

func DistancePointLine(l1, l2, p Vector2[float64]) float64

Point-line d. Line defined by points l1, l2.

func DistancePointLineSeg

func DistancePointLineSeg(l1, l2, p Vector2[float64]) float64

Distance of point p flom line segment l1-l2.

func DistancePointPoint

func DistancePointPoint[N Number](p1, p2 Vector2[N]) N

Distance between two points.

func Divisor

func Divisor[N constraints.Integer](n N) N

func Equals

func Equals[N Number](a, b, e N) bool

Returns true if a==b with error margin e

func Fade

func Fade(t float64) float64

func FilterAtPoint

func FilterAtPoint(kernel, g [][]float64, p Vector2[int]) (float64, error)

Same as FilterInPlace but returns the pixel value without applying it.

func FilterInPlace

func FilterInPlace(kernel, g [][]float64, p Vector2[int]) error

FilterInPlace applies a kernel at point p of grid g. Kernel dimensions should be odd numbers.

func Gauss

func Gauss[F constraints.Float](x, mean, sigma F) F

Gaussian of x

func InRange

func InRange[N Number](a, b Vector2[N], r N) bool

Is the distance between points a and b <= r. Avoids calling distance.

func Lerp

func Lerp(a, b, t float64) float64

func ManhattanDistance

func ManhattanDistance(a, b Vector2[int]) int

func MapHistogram

func MapHistogram(hmap [][]int) []int

MapHistogram computes the histogram of an int heightmap.

func Max

func Max[N Number](a, b N) N

func Min

func Min[N Number](a, b N) N

func NTable

func NTable(hmap [][]int) [][]float64

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 NormalizeHist

func NormalizeHist(d []int) []float64

Normalize distribution so that it adds up to 1.

func Sample

func Sample(d []float64, rejections int) (int, error)

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.

func Sample2D

func Sample2D(d [][]float64, rejections int) (int, int, error)

Sample a 2D distribution using rejection sampling.

func VectorToOctant

func VectorToOctant(a Vector2[float64]) int

Vector to octant 3 2 1 4 0 5 6 7

Types

type Box2D

type Box2D[N Number] struct {
	P1, P2 Vector2[N]
}

func NewBox2D

func NewBox2D[N Number](p1x, p1y, p2x, p2y N) Box2D[N]

func SegmentBox

func SegmentBox(box Box2D[int], size int) []Box2D[int]

Segment an int box into (size)-sized smaller boxes.

func (Box2D[N]) Contains

func (b Box2D[N]) Contains(v Vector2[N]) bool

Is v in the box (Upper bounds non inclusive)

func (Box2D[N]) ContainsInc

func (b Box2D[N]) ContainsInc(v Vector2[N]) bool

Is v in the box (Upper bounds inclusive)

func (*Box2D[N]) CropToFitIn

func (b *Box2D[N]) CropToFitIn(other Box2D[N])

CropToFitIn box to fit into other box. Boxes must overlap partially.

func (Box2D[N]) Fit

func (b Box2D[N]) Fit(v Vector2[N]) Vector2[N]

Force vector inside the box

func (Box2D[N]) FromImageRect

func (b Box2D[N]) FromImageRect(r image.Rectangle) Box2D[N]

func (Box2D[N]) IsBox

func (b Box2D[N]) IsBox() bool

func (*Box2D[N]) MakeCanonical

func (b *Box2D[N]) MakeCanonical()

Make P1 min(x,y) and P2 max(x,y)

func (Box2D[N]) New

func (b Box2D[N]) New(p1x, p1y, p2x, p2y N) Box2D[N]

func (Box2D[N]) Overlaps

func (b Box2D[N]) Overlaps(other Box2D[N]) bool

Do the boxes overlap at all

func (Box2D[N]) P3

func (b Box2D[N]) P3() Vector2[N]

Returns complementary point P3 which if the box is canonical is the upper-left one.

func (Box2D[N]) P4

func (b Box2D[N]) P4() Vector2[N]

Returns complementary point P4 which if the box is canonical is the lower-right one.

func (Box2D[N]) Size

func (b Box2D[N]) Size() Vector2[N]

Size returns the box's width and height

func (Box2D[N]) ToImageRect

func (b Box2D[N]) ToImageRect() image.Rectangle

type ByWeight

type ByWeight []Category

func (ByWeight) Len

func (a ByWeight) Len() int

ByWeight implements the sorting interface

func (ByWeight) Less

func (a ByWeight) Less(i, j int) bool

func (ByWeight) Swap

func (a ByWeight) Swap(i, j int)

type Category

type Category struct {
	Id     int
	Weight float64
}

func ChoseRandom

func ChoseRandom(entities []Category, r float64) Category

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 NewMatrix4Identity[N Number]() Matrix4[N]

func NewMatrix4RotateX

func NewMatrix4RotateX[N Number](angle N) Matrix4[N]

func NewMatrix4RotateY

func NewMatrix4RotateY[N Number](angle N) Matrix4[N]

func NewMatrix4RotateZ

func NewMatrix4RotateZ[N Number](angle N) Matrix4[N]

func NewMatrix4Scale

func NewMatrix4Scale[N Number](v Vector3[N]) Matrix4[N]

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 NewMatrix4Translation[N Number](v Vector3[N]) Matrix4[N]

func (Matrix4[N]) Column

func (m Matrix4[N]) Column(i int) []N

Returns the i-th column

func (Matrix4[N]) Decompose

func (m Matrix4[N]) Decompose() (translation, rotation, scale Vector3[N])

Return the translation, rotation and scale vectors that make up this matrix. TODO rotation

func (Matrix4[N]) MulMatrix

func (ml Matrix4[N]) MulMatrix(mr Matrix4[N]) Matrix4[N]

func (Matrix4[N]) MulVector

func (m Matrix4[N]) MulVector(p Vector3[N]) Vector3[N]

Matrix x vector multiplication

func (*Matrix4[N]) Populate

func (m *Matrix4[N]) Populate(data [16]N)

Set matrix elements - data must be in rows.

func (*Matrix4[N]) RotationAxis

func (m *Matrix4[N]) RotationAxis(axis Vector3[N], angle float64)

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]) Row

func (m Matrix4[N]) Row(i int) []N

Returns the i-th row

func (*Matrix4[N]) Scale

func (m *Matrix4[N]) Scale(v Vector3[N])

Make a scaling matrix

func (*Matrix4[N]) SetIdentity

func (m *Matrix4[N]) SetIdentity()

Matrix becomes identity matrix

func (Matrix4[N]) String

func (m Matrix4[N]) String() string

func (*Matrix4[N]) Translation

func (m *Matrix4[N]) Translation(v Vector3[N])

Make m a translation matrix

type Noise1D

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

func MakeNoise

func MakeNoise(size, window int) Noise1D

Return an initialized noise array

func (*Noise1D) Combine

func (n *Noise1D) Combine(m *Noise1D) error

Combine two noise arrays with matching sizes.

func (*Noise1D) Initialize

func (n *Noise1D) Initialize(size, window int)

Initialize a 1D noise array

func (Noise1D) Sample

func (n Noise1D) Sample(t float64) float64

Get value at point t[0,1). Values > 1 are wrapped. Values < 0 are ignored.

func (*Noise1D) Scale

func (n *Noise1D) Scale(a float64)

Scale a noise array by a factor

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

type Vector2

type Vector2[N Number] struct{ X, Y N }

func Ellipse

func Ellipse(a, b, cx, cy, t float64) Vector2[float64]

Evaluates an ellipse centered at cx,cy with focal points a,b at parameter t.

func GridLine

func GridLine(p1, p2 Vector2[int], length int) []Vector2[int]

Calculate the cells to be filed for a grid line (Bresenham's line algorithm) The line can be restricted to a specific length.

func Line

func Line(p0, p1 Vector2[int]) []Vector2[int]

Line implements Bresenham's line algorithm (translated pseudocode from wikipedia)

func OctantToOffset

func OctantToOffset(oct int) Vector2[int]

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

func ScaleChess(v Vector2[int], l int) Vector2[int]

Scale vector's length to be l (in Chebyshev distance)

func SymLine

func SymLine(p1, p2 Vector2[int]) []Vector2[int]

Nikolas' symetric line

func Vector2ConvertType

func Vector2ConvertType[A Number, B Number](a Vector2[A]) Vector2[B]

Perform a type cast

func VectorPointLine

func VectorPointLine[N Number](l1, l2, p Vector2[N]) Vector2[N]

Returns the vector perpendicular to line l1-l2 from p.

func (Vector2[N]) Add

func (v Vector2[N]) Add(b Vector2[N]) Vector2[N]

func (Vector2[N]) AddZ

func (v Vector2[N]) AddZ(z N) Vector3[N]

func (Vector2[N]) Div

func (v Vector2[N]) Div(b Vector2[N]) Vector2[N]

func (Vector2[N]) Dot

func (v Vector2[N]) Dot(b Vector2[N]) N

func (Vector2[N]) Equals

func (v Vector2[N]) Equals(b Vector2[N], e N) bool

func (Vector2[N]) Length

func (v Vector2[N]) Length() N

func (Vector2[N]) Lerp

func (a Vector2[N]) Lerp(b Vector2[N], t N) Vector2[N]

Linearly interpolate a to b by t

func (Vector2[N]) Mul

func (v Vector2[N]) Mul(b Vector2[N]) Vector2[N]

func (Vector2[N]) Normalize

func (v Vector2[N]) Normalize() Vector2[N]

Normalize returns a unit vector with the same direction as v. Will not work on integers

func (Vector2[N]) Scale

func (v Vector2[N]) Scale(b N) Vector2[N]

func (Vector2[N]) Sub

func (v Vector2[N]) Sub(b Vector2[N]) Vector2[N]

func (Vector2[N]) Zero

func (v Vector2[N]) Zero() Vector2[N]

type Vector3

type Vector3[N Number] struct{ X, Y, Z N }

func Vector3ConvertType

func Vector3ConvertType[A Number, B Number](a Vector3[A]) Vector3[B]

Perform a type cast

func (Vector3[N]) Add

func (v Vector3[N]) Add(b Vector3[N]) Vector3[N]

func (Vector3[N]) Div

func (v Vector3[N]) Div(b Vector3[N]) Vector3[N]

func (Vector3[N]) Dot

func (v Vector3[N]) Dot(b Vector3[N]) N

func (Vector3[N]) Equals

func (v Vector3[N]) Equals(b Vector3[N], e N) bool

func (Vector3[N]) Length

func (v Vector3[N]) Length() N

func (Vector3[N]) Lerp

func (a Vector3[N]) Lerp(b Vector3[N], t N) Vector3[N]

Linearly interpolate a to b by t

func (Vector3[N]) Mul

func (v Vector3[N]) Mul(b Vector3[N]) Vector3[N]

func (Vector3[N]) Normalize

func (v Vector3[N]) Normalize() Vector3[N]

Normalize returns a unit vector with the same direction as v. Will not work on integers

func (Vector3[N]) Scale

func (v Vector3[N]) Scale(b N) Vector3[N]

func (Vector3[N]) Sub

func (v Vector3[N]) Sub(b Vector3[N]) Vector3[N]

func (Vector3[N]) XY

func (v Vector3[N]) XY() Vector2[N]

Returns a Vector2 with the x and y components of this vector.

func (Vector3[N]) Zero

func (v Vector3[N]) Zero() Vector3[N]

Jump to

Keyboard shortcuts

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