Skip to content

Commit 4ab18b6

Browse files
committed
Vector2D with Int, it does not implement Vector, maybe with generics later
1 parent e122e61 commit 4ab18b6

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

math/vector2d.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,9 @@ func (v Vector2D) Neighbours() []Vector {
6161
func (v Vector2D) Values() []float64 {
6262
return []float64{v.X, v.Y}
6363
}
64+
65+
// Vector2DInt is a 2D vector.
66+
type Vector2DInt struct {
67+
X int
68+
Y int
69+
}

math/vector2dint.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package math
2+
3+
import "math"
4+
5+
// Rotate the vector.
6+
func (v *Vector2DInt) Rotate(angle float64) {
7+
angle *= rad
8+
9+
cos, sin := math.Round(math.Cos(angle)), math.Round(math.Sin(angle))
10+
11+
x := (float64(v.X) * cos) - (float64(v.Y) * sin)
12+
y := (float64(v.X) * sin) + (float64(v.Y) * cos)
13+
14+
v.X, v.Y = int(x), int(y)
15+
}
16+
17+
// Hash for cache.
18+
func (v Vector2DInt) Hash() interface{} {
19+
return v
20+
}
21+
22+
// Manhattan distance.
23+
func (v Vector2DInt) Manhattan() int {
24+
return int(math.Abs(float64(v.X)) + math.Abs(float64(v.Y)))
25+
}
26+
27+
// Neighbours if it's a coordinate.
28+
func (v Vector2DInt) Neighbours() []Vector2DInt {
29+
vectors := []Vector2DInt{}
30+
checkRange := []int{-1, 0, 1}
31+
32+
//nolint: varnamelen // These are common names for coordinates.
33+
for _, x := range checkRange {
34+
for _, y := range checkRange {
35+
if x == 0 && y == 0 {
36+
continue
37+
}
38+
39+
vectors = append(
40+
vectors,
41+
Vector2DInt{
42+
X: v.X + x,
43+
Y: v.Y + y,
44+
},
45+
)
46+
}
47+
}
48+
49+
return vectors
50+
}
51+
52+
// Values for the vector as plain int slice.
53+
func (v Vector2DInt) Values() []int {
54+
return []int{v.X, v.Y}
55+
}
56+
57+
// Add two vectors together.
58+
func (v Vector2DInt) Add(v2 Vector2DInt) Vector2DInt {
59+
return Vector2DInt{
60+
X: v.X + v2.X,
61+
Y: v.Y + v2.Y,
62+
}
63+
}

math/vector2dint_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package math_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/yitsushi/go-aoc/math"
8+
)
9+
10+
func TestVector2DInt_Rotate(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
initial math.Vector2DInt
14+
degree float64
15+
expected math.Vector2DInt
16+
}{
17+
{
18+
name: "Rotate 90",
19+
initial: math.Vector2DInt{X: 1, Y: 0},
20+
degree: 90,
21+
expected: math.Vector2DInt{X: 0, Y: 1},
22+
},
23+
{
24+
name: "Rotate -90",
25+
initial: math.Vector2DInt{X: 1, Y: 0},
26+
degree: -90,
27+
expected: math.Vector2DInt{X: 0, Y: -1},
28+
},
29+
{
30+
name: "Rotate 180",
31+
initial: math.Vector2DInt{X: 1, Y: 0},
32+
degree: 180,
33+
expected: math.Vector2DInt{X: -1, Y: 0},
34+
},
35+
{
36+
name: "Rotate -180",
37+
initial: math.Vector2DInt{X: 1, Y: 0},
38+
degree: -180,
39+
expected: math.Vector2DInt{X: -1, Y: 0},
40+
},
41+
}
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
v := &math.Vector2DInt{
45+
X: tt.initial.X,
46+
Y: tt.initial.Y,
47+
}
48+
49+
v.Rotate(tt.degree)
50+
51+
assert.Equal(t, tt.expected.X, v.X)
52+
assert.Equal(t, tt.expected.Y, v.Y)
53+
})
54+
}
55+
}
56+
57+
func TestVector2DInt_Manhattan(t *testing.T) {
58+
v := math.Vector2DInt{15, 33}
59+
60+
assert.Equal(t, int(48), v.Manhattan())
61+
}
62+
63+
func TestVector2DInt_Hash(t *testing.T) {
64+
v := math.Vector2DInt{15, 33}
65+
66+
assert.Equal(t, math.Vector2DInt{X: 15, Y: 33}, v.Hash())
67+
}
68+
69+
func TestVector2DInt_Neighbours(t *testing.T) {
70+
v := math.Vector2DInt{15, 33}
71+
72+
neighbors := v.Neighbours()
73+
74+
assert.Len(t, neighbors, 8)
75+
}
76+
77+
func TestVector2DInt_Values(t *testing.T) {
78+
v := math.Vector2DInt{15, 33}
79+
80+
assert.Equal(t, []int{15, 33}, v.Values())
81+
}
82+
83+
func TestVector2DInt_Add(t *testing.T) {
84+
v := math.Vector2DInt{15, 33}
85+
86+
assert.Equal(t, math.Vector2DInt{26, 36}, v.Add(math.Vector2DInt{11, 3}))
87+
}

0 commit comments

Comments
 (0)