-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlayout_test.go
131 lines (108 loc) · 3.01 KB
/
layout_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package hexgrid
import (
"fmt"
"math"
"testing"
)
// 100(size) 50 50
// |-------|---|---|
// | | *********
// | | * U:1,V:-1*
// | | * *
// | * (150,-86.6) *
// ********* + *********
// * U:0,V:0 * * U:2,V:-1*
// * * * *
// * (0,0) * * (300,0) *
// * + ********* + *
// * * U:1,V:0 * *
// * * * *
// * * (150,86.6) * *
// ********* + *********
// * *
// * *
// * *
// *********
//
var defaultLayout = layout{size: point{100, 100}, origin: point{0, 0}, orientation: orientationFlat}
// utility functions
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}
func TestHexToPixel(t *testing.T) {
var testCases = []struct {
hexA hex
expected string
}{
{NewHex(0, 0), "0.0;0.0"},
{NewHex(1, 0), "150.0;86.6"},
{NewHex(1, -1), "150.0;-86.6"},
{NewHex(2, -1), "300.0;0.0"},
}
for _, tt := range testCases {
pixel := HexToPixel(defaultLayout, tt.hexA)
actual := fmt.Sprintf("%.1f;%.1f", pixel.x, pixel.y)
if actual != tt.expected {
t.Error("Expected:", tt.expected, "got:", actual)
}
}
}
func TestPixelToHex(t *testing.T) {
var testCases = []struct {
point point
expected hex
}{
{point{0, 0}, NewHex(0, 0)},
{point{150, 87}, NewHex(1, 0)},
{point{300, 10}, NewHex(2, -1)},
}
for _, tt := range testCases {
actual := PixelToHex(defaultLayout, tt.point).Round()
if actual != tt.expected {
t.Error("Expected:", tt.expected, "got:", actual)
}
}
}
// 50 100 50
// |---|---|---|---|
//
// (-50;-86.6) +*******+ (50;-86.6)
// * *
// * *
// * (0,0) *
//(-100;0) + + + (100;0)
// * *
// * *
// * *
// (-50;86.6) +*******+ (50;86.6)
func TestHexagonCorners(t *testing.T) {
corners := HexagonCorners(defaultLayout, NewHex(0, 0))
if len(corners) != 6 {
t.Error("Invalid length:", len(corners))
}
// The expected corners of the hexagon, starting at the East vertex and proceeding in CCW order
testCase := []struct {
roundedX float64
roundedY float64
}{
{100, 0},
{50, -86.6},
{-50, -86.6},
{-100, 0},
{-50, 86.6},
{50, 86.6},
}
for i := 0; i < len(corners); i++ {
actualX := toFixed(corners[i].x, 1)
actualY := toFixed(corners[i].y, 1)
expectedX := testCase[i].roundedX
expectedY := testCase[i].roundedY
if actualX != expectedX || actualY != expectedY {
t.Errorf("Expected: (%.1f,%.1f) got: (%.1f,%.1f)", expectedX, expectedY, actualX, actualY)
}
}
}