Documentation ¶
Overview ¶
Package maths provides basic operations with floats and vectors
Index ¶
- Constants
- func Between(min, max, point float64) bool
- func DotProduct(first, second *Vec3) float64
- func MixedProduct(a, b, c *Vec3) float64
- func Pow32(x, a float32) float32
- func Round(number float64) int
- func Round32(number float32) int
- func SnapZero(x float64) float64
- func SolveEquation(a, b, c *Vec3) (float64, float64)
- type Vec3
- func (v *Vec3) Add(other *Vec3)
- func (v *Vec3) FaceForward(ray *Vec3) *Vec3
- func (v *Vec3) GetDimension(axis int) float64
- func (v *Vec3) Length() float64
- func (v *Vec3) LengthSquared() float64
- func (v *Vec3) MakeZero()
- func (v *Vec3) Negate()
- func (v *Vec3) Negative() *Vec3
- func (v *Vec3) Normalise()
- func (v *Vec3) Normalised() *Vec3
- func (v *Vec3) Reflect(normal *Vec3)
- func (v *Vec3) Reflected(normal *Vec3) *Vec3
- func (v *Vec3) Scale(multiplier float64)
- func (v *Vec3) Scaled(multiplier float64) *Vec3
- func (v *Vec3) SetDimension(axis int, value float64)
- func (v *Vec3) SetLength(newLength float64)
- func (v *Vec3) String() string
- func (v *Vec3) UnmarshalJSON(data []byte) error
Examples ¶
Constants ¶
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 )
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 DotProduct ¶
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 ¶
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 SolveEquation ¶
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 ¶
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 ¶
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 ¶
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 Refract ¶
Refract returns vector which is the refraction of incoming vector in relation with the normal with this ior
func (*Vec3) Add ¶
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 ¶
FaceForward flips the vector so that it's in the same hemispace as ray
func (*Vec3) GetDimension ¶
GetDimension return X, Y, or Z depending on the given axis (or INF for wrong axis)
func (*Vec3) Length ¶
Length return the lenght of a vector
Example ¶
v := NewVec3(1, 2, 2) fmt.Printf("%.3g\n", v.Length())
Output: 3
func (*Vec3) LengthSquared ¶
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) Normalise ¶
func (v *Vec3) Normalise()
Normalise sets the length to the given vector to 1
func (*Vec3) Normalised ¶
Normalised returns a new vector which length is 1
func (*Vec3) Reflect ¶
Reflect makes the given vector equal to its reflected vector by the normal and is also normalised
func (*Vec3) Reflected ¶
Reflected returns a new Vec3 witch is the normalised reflected vector of the given vector by the normal
func (*Vec3) Scale ¶
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 ¶
Scaled returns a new Vec3 which is the product of the multiplication of the given vector and the multiplier
func (*Vec3) SetDimension ¶
SetDimension makes the axis dimension of the vector equal to the value.
func (*Vec3) String ¶
String returns the string representation of the vector in the form of (x, y, z)
func (*Vec3) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface