Commit 0b90a242 authored by Boris Mühmer's avatar Boris Mühmer
Browse files

switched to github.com/go-gl/mathgl/mgl32 for types and math

parent c33aa535
Loading
Loading
Loading
Loading
+18 −40
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ package icosahedron

import (
	"math"

	mgl "github.com/go-gl/mathgl/mgl32"
)

/*
@@ -55,18 +57,18 @@ func (i *Icosahedron) Faces() Faces {
// Create creates the vertices and faces for a regular icosahedron.
func Create() (*Icosahedron, error) {
	vertices := Vertices{
		{x: 0, y: +1, z: +Phi}, // 0
		{x: 0, y: +1, z: -Phi}, // 1
		{x: 0, y: -1, z: -Phi}, // 2
		{x: 0, y: -1, z: +Phi}, // 3
		{x: +1, y: +Phi, z: 0}, // 4
		{x: +1, y: -Phi, z: 0}, // 5
		{x: -1, y: -Phi, z: 0}, // 6
		{x: -1, y: +Phi, z: 0}, // 7
		{x: +Phi, y: 0, z: +1}, // 8
		{x: -Phi, y: 0, z: +1}, // 9
		{x: -Phi, y: 0, z: -1}, // 10
		{x: +Phi, y: 0, z: -1}, // 11
		{0, +1, +Phi}, // 0
		{0, +1, -Phi}, // 1
		{0, -1, -Phi}, // 2
		{0, -1, +Phi}, // 3
		{+1, +Phi, 0}, // 4
		{+1, -Phi, 0}, // 5
		{-1, -Phi, 0}, // 6
		{-1, +Phi, 0}, // 7
		{+Phi, 0, +1}, // 8
		{-Phi, 0, +1}, // 9
		{-Phi, 0, -1}, // 10
		{+Phi, 0, -1}, // 11
	}

	faces := Faces{
@@ -97,32 +99,8 @@ func Create() (*Icosahedron, error) {
	return &i, nil
}

func (v Vertex) add(a Vertex) Vertex {
	var r Vertex
	r.x = v.x + a.x
	r.y = v.y + a.y
	r.z = v.z + a.z
	return r
}

func (v Vertex) sub(a Vertex) Vertex {
	var r Vertex
	r.x = v.x - a.x
	r.y = v.y - a.y
	r.z = v.z - a.z
	return r
}

func (v Vertex) scale(s float32) Vertex {
	var r Vertex
	r.x = v.x * s
	r.y = v.y * s
	r.z = v.z * s
	return r
}

func calculateMidPoint(va Vertex, vb Vertex, r float32) (Vertex, error) {
	vr := va.add(vb.sub(va).scale(r))
func calculateMidPoint(va mgl.Vec3, vb mgl.Vec3, r float32) (mgl.Vec3, error) {
	vr := va.Add(vb.Sub(va).Mul(r))

	return vr, nil
}
@@ -153,7 +131,7 @@ func Subdivide(icoIn *Icosahedron) (*Icosahedron, error) {
		in := [3]int{-1, -1, -1}

		// do the new vertices already exist
		for vi, vn := range []Vertex{
		for vi, vn := range []mgl.Vec3{
			v0,
			v1,
			v2,
@@ -161,7 +139,7 @@ func Subdivide(icoIn *Icosahedron) (*Icosahedron, error) {
			im := -1
			match := false
			for vj, vo := range vso {
				if (vn.x == vo.x) && (vn.y == vo.y) && (vn.z == vo.z) {
				if (vn.X() == vo.X()) && (vn.Y() == vo.Y()) && (vn.Z() == vo.Z()) {
					// found a match
					im = vj
					match = true
+4 −5
Original line number Diff line number Diff line
package icosahedron

// Vertex is the tuple (x,y,z).
type Vertex struct {
	x, y, z float32
}
import (
	mgl "github.com/go-gl/mathgl/mgl32"
)

// Vertices of a regular icosahedron.
type Vertices []Vertex
type Vertices []mgl.Vec3

// Face contains the indexes of verticies.
type Face []int