1 package geo
2
3 import (
4 "math"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestExtendVerticalLineSegments(t *testing.T) {
11 p1 := &Point{0, 0}
12 p2 := &Point{0, 1}
13
14 v := p1.VectorTo(p2)
15 v = v.Multiply(2)
16 p2New := p1.AddVector(v)
17 expected := Point{0, 2}
18 assert.Equal(t, expected, *p2New)
19
20 v = p2.VectorTo(p1)
21 v = v.Multiply(2)
22 p1New := p2.AddVector(v)
23 expected = Point{0, -1}
24 assert.Equal(t, expected, *p1New)
25 }
26
27 func TestExtendHorizontalLineSegment(t *testing.T) {
28 p1 := &Point{0, 0}
29 p2 := &Point{1, 0}
30
31 v := p1.VectorTo(p2)
32 v = v.Multiply(1.5)
33 p2New := p1.AddVector(v)
34 expected := Point{1.5, 0}
35 assert.Equal(t, expected, *p2New)
36
37 v = p2.VectorTo(p1)
38 v = v.Multiply(1.5)
39 p1New := p2.AddVector(v)
40 expected = Point{-0.5, 0}
41 assert.Equal(t, expected, *p1New)
42 }
43
44 func TestExtendDiagonalLineSegment(t *testing.T) {
45 p1 := &Point{0, 0}
46 p2 := &Point{3, 1}
47
48 v := p1.VectorTo(p2)
49 v = v.Multiply(2)
50 p2New := p1.AddVector(v)
51 expected := Point{6, 2}
52 assert.Equal(t, expected, *p2New)
53
54 v = p2.VectorTo(p1)
55 v = v.Multiply(2)
56 p1New := p2.AddVector(v)
57 expected = Point{-3, -1}
58 assert.Equal(t, expected, *p1New)
59 }
60
61 func TestVectorAdd(t *testing.T) {
62 a := NewVector(1, 2)
63 b := NewVector(3, 4)
64
65 c := a.Add(b)
66
67 assert.Truef(t, c.equals(NewVector(4, 6)), "Expected Vector %v to be (4, 6)", c)
68 }
69
70 func TestVectorMinus(t *testing.T) {
71 a := NewVector(1, 2)
72 b := NewVector(3, 4)
73
74 c := a.Minus(b)
75
76 assert.Truef(t, c.equals(NewVector(-2, -2)), "Expected Vector %v to be (-2, -2)", c)
77 }
78
79 func TestVectorMultiply(t *testing.T) {
80 a := NewVector(1, 2)
81
82 c := a.Multiply(3)
83
84 assert.Truef(t, c.equals(NewVector(3, 6)), "Expected Vector %v to be (3, 6)", c)
85 }
86
87 func TestVectorLength(t *testing.T) {
88 a := NewVector(3, 4)
89
90 assert.Equal(t, 5.0, a.Length())
91 }
92
93 func TestNewVectorFromProperties(t *testing.T) {
94 a := NewVectorFromProperties(3, math.Pi/3)
95 if !a.equals(NewVector(2.59807621135, 1.5)) {
96 t.Errorf("expected Vector to be close to (2.59807, 1.5), got %v", a)
97 }
98
99 b := NewVectorFromProperties(3, -math.Pi/3)
100 if !b.equals(NewVector(-2.59807621135, 1.5)) {
101 t.Errorf("expected Vector to be close to (-2.59807, 1.5), got %v", b)
102 }
103
104 c := NewVectorFromProperties(3, math.Pi*2/3)
105 if !c.equals(NewVector(2.59807621135, -1.5)) {
106 t.Errorf("expected Vector to be close to (2.59807, -1.5), got %v", c)
107 }
108
109 d := NewVectorFromProperties(3, -math.Pi*2/3)
110 if !d.equals(NewVector(-2.59807621135, -1.5)) {
111 t.Errorf("expected Vector to be close to (-2.59807, -1.5), got %v", c)
112 }
113 }
114
115 func TestVectorUnit(t *testing.T) {
116 a := NewVector(3, 4).Unit()
117 expected := NewVector(3.0/5, 4.0/5)
118 assert.Truef(t, a.equals(expected), "Expected (%f, %f) Vector, got %v", 3/5.0, 4/5.0, a)
119 }
120
121 func TestVectorAddLength(t *testing.T) {
122 a := NewVector(3, 4)
123 b := a.AddLength(8)
124
125 if PrecisionCompare(b.Length(), 13.0, PRECISION) != 0 {
126 t.Fatalf("Expected new Vector to have length 13, got %v", b.Length())
127 }
128 }
129
130 func TestVectorEquals(t *testing.T) {
131 a := NewVector(1, 2)
132 assert.True(t, a.equals(a), "Expected Vector to be equal to itself")
133 assert.True(t, a.equals(NewVector(1.0, 2.0)), "Expected Vector to be equal to different Vector with same components")
134 assert.False(t, a.equals(NewVector(1, 2, 3)), "Expected Vector to be different if different component count")
135 assert.False(t, a.equals(NewVector(2, 2)), "Expected Vector to be different if components are different")
136 }
137
138 func TestVectorToPoint(t *testing.T) {
139 v := NewVector(3.789, -0.731)
140 p := v.ToPoint()
141
142 assert.Equal(t, v[0], p.X)
143 assert.Equal(t, v[1], p.Y)
144 }
145
146 func (a Vector) equals(other Vector) bool {
147 if len(a) != len(other) {
148 return false
149 }
150 for i := 0; i < len(a); i++ {
151 if PrecisionCompare(a[i], other[i], PRECISION) != 0 {
152 return false
153 }
154 }
155 return true
156 }
157
View as plain text