Skip to content

Commit 6006843

Browse files
authored
Add direction and move (#3)
1 parent 24b670a commit 6006843

File tree

3 files changed

+149
-35
lines changed

3 files changed

+149
-35
lines changed

path_test.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,32 @@ func BenchmarkPath(b *testing.B) {
9494
}
9595
})
9696

97-
b.Run("6144x6144", func(b *testing.B) {
98-
m := NewGrid(6144, 6144)
97+
b.Run("3069x3069", func(b *testing.B) {
98+
m := NewGrid(3069, 3069)
9999
b.ReportAllocs()
100100
b.ResetTimer()
101101
for n := 0; n < b.N; n++ {
102-
m.Path(At(0, 0), At(380, 380), costOf)
102+
m.Path(At(0, 0), At(700, 700), costOf)
103103
}
104104
})
105105

106-
b.Run("6147x6147", func(b *testing.B) {
107-
m := NewGrid(6147, 6147)
106+
b.Run("3072x3072", func(b *testing.B) {
107+
m := NewGrid(3072, 3072)
108108
b.ReportAllocs()
109109
b.ResetTimer()
110110
for n := 0; n < b.N; n++ {
111-
m.Path(At(0, 0), At(380, 380), costOf)
111+
m.Path(At(0, 0), At(700, 700), costOf)
112112
}
113113
})
114114

115+
b.Run("6144x6144", func(b *testing.B) {
116+
m := NewGrid(6144, 6144)
117+
b.ReportAllocs()
118+
b.ResetTimer()
119+
for n := 0; n < b.N; n++ {
120+
m.Path(At(0, 0), At(380, 380), costOf)
121+
}
122+
})
115123
}
116124

117125
// BenchmarkAround/3r-8 352876 3355 ns/op 385 B/op 1 allocs/op

point.go

+70
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,35 @@ func (p Point) WithinSize(size Point) bool {
188188
return p.X >= 0 && p.Y >= 0 && p.X < size.X && p.Y < size.Y
189189
}
190190

191+
// Move moves a point by one in the specified direction.
192+
func (p Point) Move(direction Direction) Point {
193+
return p.MoveBy(direction, 1)
194+
}
195+
196+
// MoveBy moves a point by n in the specified direction.
197+
func (p Point) MoveBy(direction Direction, n int16) Point {
198+
switch direction {
199+
case North:
200+
return Point{p.X, p.Y + n}
201+
case NorthEast:
202+
return Point{p.X + n, p.Y + n}
203+
case East:
204+
return Point{p.X + n, p.Y}
205+
case SouthEast:
206+
return Point{p.X + n, p.Y - n}
207+
case South:
208+
return Point{p.X, p.Y - n}
209+
case SouthWest:
210+
return Point{p.X - n, p.Y - n}
211+
case West:
212+
return Point{p.X - n, p.Y}
213+
case NorthWest:
214+
return Point{p.X - n, p.Y + n}
215+
default:
216+
return p
217+
}
218+
}
219+
191220
// DistanceTo calculates manhattan distance to the other point
192221
func (p Point) DistanceTo(other Point) uint32 {
193222
return abs(int32(p.X)-int32(other.X)) + abs(int32(p.Y)-int32(other.Y))
@@ -231,3 +260,44 @@ func (r *Rect) Size() Point {
231260
Y: r.Max.Y - r.Min.Y,
232261
}
233262
}
263+
264+
// -----------------------------------------------------------------------------
265+
266+
// Diretion represents a direction
267+
type Direction byte
268+
269+
// Various directions
270+
const (
271+
North Direction = iota
272+
NorthEast
273+
East
274+
SouthEast
275+
South
276+
SouthWest
277+
West
278+
NorthWest
279+
)
280+
281+
// String returns a string representation of a direction
282+
func (v Direction) String() string {
283+
switch v {
284+
case North:
285+
return "🡱N"
286+
case NorthEast:
287+
return "🡵NE"
288+
case East:
289+
return "🡲E"
290+
case SouthEast:
291+
return "🡶SE"
292+
case South:
293+
return "🡳S"
294+
case SouthWest:
295+
return "🡷SW"
296+
case West:
297+
return "🡰W"
298+
case NorthWest:
299+
return "🡴NW"
300+
default:
301+
return ""
302+
}
303+
}

point_test.go

+65-29
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,42 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12+
/*
13+
cpu: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
14+
BenchmarkPoint/within-8 1000000000 0.2697 ns/op 0 B/op 0 allocs/op
15+
BenchmarkPoint/within-rect-8 1000000000 0.2928 ns/op 0 B/op 0 allocs/op
16+
BenchmarkPoint/interleave-8 1000000000 0.8242 ns/op 0 B/op 0 allocs/op
17+
*/
18+
func BenchmarkPoint(b *testing.B) {
19+
p := At(10, 20)
20+
b.Run("within", func(b *testing.B) {
21+
b.ReportAllocs()
22+
b.ResetTimer()
23+
for n := 0; n < b.N; n++ {
24+
p.Within(At(0, 0), At(100, 100))
25+
}
26+
})
27+
28+
b.Run("within-rect", func(b *testing.B) {
29+
b.ReportAllocs()
30+
b.ResetTimer()
31+
for n := 0; n < b.N; n++ {
32+
p.WithinRect(NewRect(0, 0, 100, 100))
33+
}
34+
})
35+
36+
b.Run("interleave", func(b *testing.B) {
37+
out := int32(0)
38+
p := At(8191, 8191)
39+
b.ReportAllocs()
40+
b.ResetTimer()
41+
for n := 0; n < b.N; n++ {
42+
out = p.Interleave()
43+
}
44+
assert.NotZero(b, out)
45+
})
46+
}
47+
1248
func TestPoint(t *testing.T) {
1349
p := At(10, 20)
1450
p2 := At(2, 2)
@@ -37,35 +73,35 @@ func TestMorton(t *testing.T) {
3773
assert.Equal(t, p, deinterleavePoint(67108863))
3874
}
3975

40-
// BenchmarkPoint/within-8 1000000000 0.220 ns/op 0 B/op 0 allocs/op
41-
// BenchmarkPoint/within-rect-8 1000000000 0.219 ns/op 0 B/op 0 allocs/op
42-
// BenchmarkPoint/interleave-8 1000000000 0.657 ns/op 0 B/op 0 allocs/op
43-
func BenchmarkPoint(b *testing.B) {
44-
p := At(10, 20)
45-
b.Run("within", func(b *testing.B) {
46-
b.ReportAllocs()
47-
b.ResetTimer()
48-
for n := 0; n < b.N; n++ {
49-
p.Within(At(0, 0), At(100, 100))
50-
}
51-
})
76+
func TestDirection(t *testing.T) {
77+
for i := 0; i < 8; i++ {
78+
dir := Direction(i)
79+
assert.NotEmpty(t, dir.String())
80+
}
81+
}
5282

53-
b.Run("within-rect", func(b *testing.B) {
54-
b.ReportAllocs()
55-
b.ResetTimer()
56-
for n := 0; n < b.N; n++ {
57-
p.WithinRect(NewRect(0, 0, 100, 100))
58-
}
59-
})
83+
func TestDirection_Empty(t *testing.T) {
84+
dir := Direction(9)
85+
assert.Empty(t, dir.String())
86+
}
6087

61-
b.Run("interleave", func(b *testing.B) {
62-
out := int32(0)
63-
p := At(8191, 8191)
64-
b.ReportAllocs()
65-
b.ResetTimer()
66-
for n := 0; n < b.N; n++ {
67-
out = p.Interleave()
68-
}
69-
assert.NotZero(b, out)
70-
})
88+
func TestMove(t *testing.T) {
89+
tests := []struct {
90+
dir Direction
91+
out Point
92+
}{
93+
{North, Point{X: 0, Y: 1}},
94+
{South, Point{X: 0, Y: -1}},
95+
{East, Point{X: 1, Y: 0}},
96+
{West, Point{X: -1, Y: 0}},
97+
{NorthEast, Point{X: 1, Y: 1}},
98+
{NorthWest, Point{X: -1, Y: 1}},
99+
{SouthEast, Point{X: 1, Y: -1}},
100+
{SouthWest, Point{X: -1, Y: -1}},
101+
{Direction(99), Point{}},
102+
}
103+
104+
for _, tc := range tests {
105+
assert.Equal(t, tc.out, Point{}.Move(tc.dir), tc.dir.String())
106+
}
71107
}

0 commit comments

Comments
 (0)