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

removed constants, added level

parent aed46411
Loading
Loading
Loading
Loading
+9 −14
Original line number Diff line number Diff line
@@ -12,14 +12,8 @@ import "math"

*/

const (
	// PhiC at http://kryptografie.de/kryptografie/chiffre/goldener-schnitt-nachkommastellen.htm.
	PhiC = 1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137484754088075386891752126633862223536931793180060766726354433389086595939582905638322661319928290267880675208766892501711696207032221043216269548626296313614438149758701220340805887954454749246185695364
)

// These values are only used for testing.
const (
	phi1000          = 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144381497587012203408058879544547492461856953648644492410443207713449470495658467885098743394422125448770664780915884607499887124007652170575179788341662562494075890697040002812104276217711177780531531714101170466659914669798731761356006708748071013179523689427521948435305678300228785699782977834784587822891109762500302696156170025046433824377648610283831268330372429267526311653392473167111211588186385133162038400522216579128667529465490681131715993432359734949850904094762132229810172610705961164562990981629055520852479035240602017279974717534277759277862561943208275051312181562855122248093947123414517022373580577278616008688382952304592647878017889921990270776903895321968198615143780314997411069260886742962267575605231727775203536139362
	numberOfVertices = 12
	numberOfFaces    = 20
)
@@ -35,6 +29,7 @@ func init() {

// Icosahedron holds the information for an icosahedron in one place.
type Icosahedron struct {
	level int
	vs    Vertices
	fs    Faces
}
@@ -79,12 +74,12 @@ func Create() (*Icosahedron, error) {
		{0, 9, 7},  // 19
	}

	i := Icosahedron{vs: vertices, fs: faces}
	i := Icosahedron{level: 0, vs: vertices, fs: faces}

	return &i, nil
}

func scaleBy(va Vertex, vb Vertex, r float64) (Vertex, error) {
func calculateMidPoint(va Vertex, vb Vertex, r float64) (Vertex, error) {
	var vr Vertex
	var vt Vertex

@@ -117,9 +112,9 @@ func Subdivide(icoIn *Icosahedron) (*Icosahedron, error) {
		ia, ib, ic := f[0], f[1], f[2]
		va, vb, vc := vso[ia : ia+1][0], vso[ib : ib+1][0], vso[ic : ic+1][0]

		v0, _ := scaleBy(va, vb, 0.5)
		v1, _ := scaleBy(vb, vc, 0.5)
		v2, _ := scaleBy(vc, va, 0.5)
		v0, _ := calculateMidPoint(va, vb, 0.5)
		v1, _ := calculateMidPoint(vb, vc, 0.5)
		v2, _ := calculateMidPoint(vc, va, 0.5)

		// TBD: new verticies do not have the correct distance to the center!!!

@@ -144,7 +139,7 @@ func Subdivide(icoIn *Icosahedron) (*Icosahedron, error) {
		fso = append(fso, f3)
	}

	icoOut := Icosahedron{vs: vso, fs: fso}
	icoOut := Icosahedron{level: icoIn.level + 1, vs: vso, fs: fso}

	return &icoOut, nil
}
+5 −17
Original line number Diff line number Diff line
@@ -4,26 +4,14 @@ import (
	"testing"
)

func TestPhi(t *testing.T) {
	if Phi != phi1000 {
		t.Errorf("Phi = %g, expected %g", Phi, phi1000)
	}
	if d := Phi - phi1000; d != 0 {
		t.Errorf("Zero test = %g, expected 0", d)
	}
	if Phi != PhiC {
		t.Errorf("Phi = %g, expected %g", Phi, PhiC)
	}
	if d := Phi - PhiC; d != 0 {
		t.Errorf("Zero test 2 = %g, expected 0", d)
	}
}

func TestIcosahedron(t *testing.T) {
	ico, err := Create()
	if err != nil {
		t.Errorf("Icosahedron() = %v, expected nil", err)
	}
	if ico.level != 0 {
		t.Errorf("ico.level = %d, expected 0", ico.level)
	}
	if n := len(ico.vs); n != numberOfVertices {
		t.Errorf("len(vertices) = %d, expected %d", n, numberOfVertices)
	}
@@ -39,12 +27,12 @@ func TestIcosahedronSubdivide(t *testing.T) {
	lsv1 := len(ico1.vs)
	lsf1 := len(ico1.fs)

	t.Logf("Subdivided Ico 1: %d vertices, %d faces", lsv1, lsf1)
	t.Logf("Subdivided Ico %d: %d vertices, %d faces", ico1.level, lsv1, lsf1)

	ico2, _ := Subdivide(ico1)

	lsv2 := len(ico2.vs)
	lsf2 := len(ico2.fs)

	t.Logf("Subdivided Ico 2: %d vertices, %d faces", lsv2, lsf2)
	t.Logf("Subdivided Ico %d: %d vertices, %d faces", ico2.level, lsv2, lsf2)
}