maths

package
v0.0.0-...-1c48c43 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2017 License: MIT Imports: 3 Imported by: 7

Documentation

Overview

Package maths provides basic operations with floats and vectors

Index

Examples

Constants

View Source
const (
	// Ox represents the X axis
	Ox = iota
	// Oy represents the Y axis
	Oy
	// Oz represents the Z axis
	Oz
	// Leaf represents a leaf in a tree enumerated with axes
	Leaf
)
View Source
const (
	// Epsilon is a very small number
	Epsilon float64 = 1e-9
	// Inf is a very large number
	Inf float64 = 1e99
)

Variables

This section is empty.

Functions

func Between

func Between(min, max, point float64) bool

Between check whether point is between min and max (+- Epsilon)

func DotProduct

func DotProduct(first, second *Vec3) float64

DotProduct returns a float64 number which is the dot product of the two given vectors

Example
fmt.Printf("p = DotProduct((1, 2, 1), (-1, 0, 43))\n")
p := DotProduct(NewVec3(1, 2, 1), NewVec3(-1, 0, 43))
fmt.Printf("p = %.3g\n", p)
Output:

p = DotProduct((1, 2, 1), (-1, 0, 43))
p = 42

func MixedProduct

func MixedProduct(a, b, c *Vec3) float64

MixedProduct returns (a^b)*c

Example
fmt.Printf("p = MixedProduct((1, 0, 0), (0, 1, 0), (0, 2, 42))\n")
p := MixedProduct(NewVec3(1, 0, 0), NewVec3(0, 1, 0), NewVec3(0, 2, 42))
fmt.Printf("p = %.2f\n", p)
Output:

p = MixedProduct((1, 0, 0), (0, 1, 0), (0, 2, 42))
p = 42.00

func Pow32

func Pow32(x, a float32) float32

Pow32 is a Pow function which uses float32

func Round

func Round(number float64) int

Round returns the nearest int to a given float number

func Round32

func Round32(number float32) int

Round32 returns the nearest int to a given float32 number

func SnapZero

func SnapZero(x float64) float64

SnapZero returns 0 if x is too close to 0, and returns x otherwise

func SolveEquation

func SolveEquation(a, b, c *Vec3) (float64, float64)

SolveEquation solves the following system (returning x and y): | a1 * x + b1 * y + c1 = 0 | a2 * x + b2 * y + c2 = 0

Example
a := NewVec3(2, 1, 0)
b := NewVec3(4, -2, 0)
c := NewVec3(24, 4, 0)

// | 2x + 4y = 24
// |  x - 2y = 4

x, y := SolveEquation(a, b, c)
fmt.Printf("x = %.3g, y = %.3g\n", x, y)
Output:

x = 8, y = 2

Types

type Vec3

type Vec3 struct {
	X, Y, Z float64
}

Vec3 is a 3 dimensional vector

func AddVectors

func AddVectors(first, second *Vec3) *Vec3

AddVectors returns a new vector which is the sum of the two given vectors

Example
fmt.Printf("v = AddVectors((1, 2, 3), (2, -2, 3))\n")
v := AddVectors(NewVec3(1, 2, 3), NewVec3(2, -2, 3))
fmt.Printf("v = %s\n", v)
Output:

v = AddVectors((1, 2, 3), (2, -2, 3))
v = (3, 0, 6)

func CrossProduct

func CrossProduct(first, second *Vec3) *Vec3

CrossProduct returns a new Vec3 which is the cross product of the two given vectors

Example
fmt.Printf("p = CrossProduct((1, 0, 0), (0, 1, 0))\n")
p := CrossProduct(NewVec3(1, 0, 0), NewVec3(0, 1, 0))
fmt.Printf("p = %s\n", p)
Output:

p = CrossProduct((1, 0, 0), (0, 1, 0))
p = (0, 0, 1)

func MinusVectors

func MinusVectors(first, second *Vec3) *Vec3

MinusVectors returns a new vector which is the first vector minus the second

Example
fmt.Printf("v = MinusVectors((1, 2, 3), (2, 2, 2))\n")
v := MinusVectors(NewVec3(1, 2, 3), NewVec3(2, 2, 2))
fmt.Printf("v = %s\n", v)
Output:

v = MinusVectors((1, 2, 3), (2, 2, 2))
v = (-1, 0, 1)

func NewVec3

func NewVec3(x, y, z float64) *Vec3

NewVec3 returns new 3 dimensional vector

func NewVec3Array

func NewVec3Array(values [3]float64) *Vec3

NewVec3Array makes vector from array

func Refract

func Refract(incoming *Vec3, normal *Vec3, ior float64) *Vec3

Refract returns vector which is the refraction of incoming vector in relation with the normal with this ior

func (*Vec3) Add

func (v *Vec3) Add(other *Vec3)

Add takes another vector and adds its dimensions to those of the given vector

Example
v := NewVec3(1, -2, 3)
fmt.Printf("%s\n", v)
v.Add(NewVec3(1, 2, 3))
fmt.Printf("%s\n", v)
Output:

(1, -2, 3)
(2, 0, 6)

func (*Vec3) FaceForward

func (v *Vec3) FaceForward(ray *Vec3) *Vec3

FaceForward flips the vector so that it's in the same hemispace as ray

func (*Vec3) GetDimension

func (v *Vec3) GetDimension(axis int) float64

GetDimension return X, Y, or Z depending on the given axis (or INF for wrong axis)

func (*Vec3) Length

func (v *Vec3) Length() float64

Length return the lenght of a vector

Example
v := NewVec3(1, 2, 2)
fmt.Printf("%.3g\n", v.Length())
Output:

3

func (*Vec3) LengthSquared

func (v *Vec3) LengthSquared() float64

LengthSquared returns the square of the length of a vector

Example
v := NewVec3(1, 2, 2)
fmt.Printf("%.3g\n", v.LengthSquared())
Output:

9

func (*Vec3) MakeZero

func (v *Vec3) MakeZero()

MakeZero makes all the dimentsions of the vector zero

Example
v := NewVec3(1, 2, 3)
fmt.Printf("%s\n", v)
v.MakeZero()
fmt.Printf("%s\n", v)
Output:

(1, 2, 3)
(0, 0, 0)

func (*Vec3) Negate

func (v *Vec3) Negate()

Negate makes the given vector equal to its opposite vector

Example
v := NewVec3(1, -2, 10)
fmt.Printf("%s\n", v)
v.Negate()
fmt.Printf("%s\n", v)
Output:

(1, -2, 10)
(-1, 2, -10)

func (*Vec3) Negative

func (v *Vec3) Negative() *Vec3

Negative returns the opposite of the given vector

func (*Vec3) Normalise

func (v *Vec3) Normalise()

Normalise sets the length to the given vector to 1

func (*Vec3) Normalised

func (v *Vec3) Normalised() *Vec3

Normalised returns a new vector which length is 1

func (*Vec3) Reflect

func (v *Vec3) Reflect(normal *Vec3)

Reflect makes the given vector equal to its reflected vector by the normal and is also normalised

func (*Vec3) Reflected

func (v *Vec3) Reflected(normal *Vec3) *Vec3

Reflected returns a new Vec3 witch is the normalised reflected vector of the given vector by the normal

func (*Vec3) Scale

func (v *Vec3) Scale(multiplier float64)

Scale multiplies all the dimension of the vector by the given multiplier

Example
v := NewVec3(1, -2, 3)
v.Scale(2)
fmt.Printf("%s\n", v)
Output:

(2, -4, 6)

func (*Vec3) Scaled

func (v *Vec3) Scaled(multiplier float64) *Vec3

Scaled returns a new Vec3 which is the product of the multiplication of the given vector and the multiplier

func (*Vec3) SetDimension

func (v *Vec3) SetDimension(axis int, value float64)

SetDimension makes the axis dimension of the vector equal to the value.

func (*Vec3) SetLength

func (v *Vec3) SetLength(newLength float64)

SetLength makes the lenght of the vector equal to the given newLength

func (*Vec3) String

func (v *Vec3) String() string

String returns the string representation of the vector in the form of (x, y, z)

func (*Vec3) UnmarshalJSON

func (v *Vec3) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface

Jump to

Keyboard shortcuts

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