alg

package
v2.3.3-beta Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2020 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

Package alg stores useful algorithms and math functions

Index

Constants

View Source
const (
	// DegToRad is the constant value something in
	// degrees should be multiplied by to obtain
	// something in radians.
	DegToRad = math.Pi / 180
	// RadToDeg is the constant value something in
	// radians should be multiplied by to obtain
	// something in degrees.
	RadToDeg = 180 / math.Pi
)

Variables

This section is empty.

Functions

func ChooseX

func ChooseX(weights []float64, n int) []int

ChooseX - also known as Roulette Search. This returns n indices from the input weights at a count relative to the weight of each index. It can return the same index multiple times.

func CumulativeWeights

func CumulativeWeights(weights []float64) []float64

CumulativeWeights converts a slice of weights into a slice of cumulative weights, where each index is the sum of all weights up until that index in the original slice

func F64eq

func F64eq(f1, f2 float64) bool

F64eq equates two float64s within a small epsilon.

func F64eqEps

func F64eqEps(f1, f2, epsilon float64) bool

F64eqEps equates two float64s within a provided epsilon.

func RemainingWeights

func RemainingWeights(weights []float64) []float64

RemainingWeights is equivalent to CumulativeWeights with the slice reversed, where the zeroth element will contain the total weight.

func RoundF64

func RoundF64(a float64) int

RoundF64 rounds a float64 to an int

func TriangulateConvex

func TriangulateConvex(face []int) [][3]int

TriangulateConvex takes a face, in the form of a slice of indices, and outputs those indicies split into triangles based on the assumption that the face is convex. This involves forming pairs of indices and drawing an edge back to the first index repeatedly.

If given less than 3 indices, returns an empty slice.

Example input: [0,1,2,3,4] Example output: [[0,1,2][0,2,3][0,3,4]]

Visual Example:

 ____0____
4         1
 \       /
  3-----2
   - - -
 ____0____
4   / \   1
 \ /   \ /
  3-----2

This makes additional assumptions that the points represented by the indices are coplanar, and that there are no holes present in the face.

func UniqueChooseX

func UniqueChooseX(weights []float64, n int) []int

UniqueChooseX returns n indices from the input weights at a count relative to the weight of each index. This will never return duplicate indices. if n > len(weights), it will return -1 after depleting the n elements from weights.

func UniqueChooseXSeeded

func UniqueChooseXSeeded(weights []float64, n int, rng Float64Generator) []int

UniqueChooseXSeeded returns n indices from the input weights at a count relative to the weight of each index. This will never return duplicate indices. if n > len(weights), it will return -1 after depleting the n elements from weights. If you do not want to use your own rand use UniqueChooseX.

func WeightedChooseOne

func WeightedChooseOne(remainingWeights []float64) int

WeightedChooseOne returns a single index from the weights given at a rate relative to the magnitude of each weight. It expects the input to be in the form of RemainingWeights, cumulative with the total at index 0.

func WeightedChooseOneSeeded

func WeightedChooseOneSeeded(remainingWeights []float64, rng Float64Generator) int

WeightedChooseOneSeeded returns a single index from the weights given at a rate relative to the magnitude of each weight. It expects the input to be in the form of RemainingWeights, cumulative with the total at index 0. If you do not want to use your own rand use WeightedChooseOne.

func WeightedMapChoice

func WeightedMapChoice(weightMap map[int]float64) int

WeightedMapChoice converts the input map into a set where keys are indices and values are weights for WeightedChooseOne, then returns the key for WeightedChooseOne of the weights.

func WeightedMapChoiceSeeded

func WeightedMapChoiceSeeded(weightMap map[int]float64, rng Float64Generator) int

WeightedMapChoiceSeeded converts the input map into a set where keys are indices and values are weights for WeightedChooseOne, then returns the key for WeightedChooseOne of the weight. If you do not want to use your own rand use WeightedMapChoice.

Types

type Degree

type Degree float64

A Degree value is a float that specifies it should be in degrees.

func (Degree) Radians

func (d Degree) Radians() Radian

Radians converts a Degree to Radians.

type Float64Generator

type Float64Generator interface {
	Float64() float64
}

A Float64Generator must be able to generate a float64. This is generally used to implement randomness See rand.Float64 for an example

type Radian

type Radian float64

A Radian value is a float that specifies it should be in radians.

func (Radian) Degrees

func (r Radian) Degrees() Degree

Degrees converts a Radian to Degrees.

Directories

Path Synopsis
Package floatgeom stores primitives for floating point geometry
Package floatgeom stores primitives for floating point geometry
Package intgeom stores primitives for integer geometry
Package intgeom stores primitives for integer geometry

Jump to

Keyboard shortcuts

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