math API

math

package

API reference for the math package.

S
struct

Vec2

Vec2 represents a 2D vector.

math/math.go:7-7
type Vec2 struct

Methods

Add
Method

Add adds two vectors component-wise.

Parameters

o Vec2

Returns

func (Vec2) Add(o Vec2) Vec2
{ return Vec2{v.X + o.X, v.Y + o.Y} }
Normalize
Method

Normalize returns the unit vector in the same direction.

Returns

func (Vec2) Normalize() Vec2
{
	l := float32(stdmath.Sqrt(float64(v.X*v.X + v.Y*v.Y)))
	if l == 0 {
		return Vec2{}
	}
	return Vec2{v.X / l, v.Y / l}
}

Fields

Name Type Description
X float32
Y float32
S
struct

Vec3

Vec3 represents a 3D vector.

math/math.go:22-22
type Vec3 struct

Methods

Add
Method

Add adds two Vec3 values component-wise.

Parameters

o Vec3

Returns

func (Vec3) Add(o Vec3) Vec3
{ return Vec3{v.X + o.X, v.Y + o.Y, v.Z + o.Z} }
Normalize
Method

Normalize returns the unit vector of v.

Returns

func (Vec3) Normalize() Vec3
{
	l := float32(stdmath.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z)))
	if l == 0 {
		return Vec3{}
	}
	return Vec3{v.X / l, v.Y / l, v.Z / l}
}

Fields

Name Type Description
X float32
Y float32
Z float32
T
type

Mat4

Mat4 represents a 4x4 matrix in column-major order.

math/math.go:37-37
type Mat4 [16]float32
F
function

Identity

Identity returns an identity matrix.

Returns

math/math.go:40-47
func Identity() Mat4

{
	return Mat4{
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1,
	}
}
F
function

Translation

Translation returns a matrix translating by v.

Parameters

v

Returns

math/math.go:50-54
func Translation(v Vec3) Mat4

{
	m := Identity()
	m[12], m[13], m[14] = v.X, v.Y, v.Z
	return m
}
F
function

Scale

Scale returns a scaling matrix for v.

Parameters

v

Returns

math/math.go:57-61
func Scale(v Vec3) Mat4

{
	m := Identity()
	m[0], m[5], m[10] = v.X, v.Y, v.Z
	return m
}
F
function

Perspective

Perspective returns a perspective projection matrix.

Parameters

fovY
float32
aspect
float32
near
float32
far
float32

Returns

math/math.go:75-84
func Perspective(fovY, aspect, near, far float32) Mat4

{
	f := float32(1 / stdmath.Tan(float64(fovY)/2))
	var m Mat4
	m[0] = f / aspect
	m[5] = f
	m[10] = (far + near) / (near - far)
	m[11] = -1
	m[14] = (2 * far * near) / (near - far)
	return m
}
F
function

Orthographic

Orthographic returns an orthographic projection matrix.

Parameters

left
float32
right
float32
bottom
float32
top
float32
near
float32
far
float32

Returns

math/math.go:87-97
func Orthographic(left, right, bottom, top, near, far float32) Mat4

{
	var m Mat4
	m[0] = 2 / (right - left)
	m[5] = 2 / (top - bottom)
	m[10] = -2 / (far - near)
	m[12] = -(right + left) / (right - left)
	m[13] = -(top + bottom) / (top - bottom)
	m[14] = -(far + near) / (far - near)
	m[15] = 1
	return m
}
F
function

almostEqual

Parameters

a
float32
b
float32

Returns

bool
math/math_test.go:8-8
func almostEqual(a, b float32) bool

{ return stdmath.Abs(float64(a-b)) < 1e-5 }
F
function

TestVecOperations

Parameters

math/math_test.go:10-19
func TestVecOperations(t *testing.T)

{
	v := Vec2{1, 2}.Add(Vec2{3, 4})
	if v.X != 4 || v.Y != 6 {
		t.Fatalf("unexpected sum: %+v", v)
	}
	n := Vec3{3, 4, 0}.Normalize()
	if !almostEqual(n.X, 0.6) || !almostEqual(n.Y, 0.8) || !almostEqual(n.Z, 0) {
		t.Fatalf("unexpected normalization: %+v", n)
	}
}
F
function

TestMat4Multiply

Parameters

math/math_test.go:21-36
func TestMat4Multiply(t *testing.T)

{
	a := Translation(Vec3{1, 2, 3})
	b := Scale(Vec3{2, 2, 2})
	r := a.Mul(b)
	exp := Mat4{
		2, 0, 0, 0,
		0, 2, 0, 0,
		0, 0, 2, 0,
		1, 2, 3, 1,
	}
	for i := 0; i < 16; i++ {
		if !almostEqual(r[i], exp[i]) {
			t.Fatalf("matrix mismatch at %d: got %v want %v", i, r[i], exp[i])
		}
	}
}