-
Notifications
You must be signed in to change notification settings - Fork 110
/
walk.go
36 lines (29 loc) · 847 Bytes
/
walk.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
package utils
// WalkCallback is to be called for each point visited by Walk.
type WalkCallback func(x, y int) error
// Walk starts at the given middle point and walks around increasingly
// bigger squares based on the given radius growing outwards.
func Walk(middleX, middleY, maxRadius int, f WalkCallback) error {
if err := f(middleX, middleY); err != nil {
return err
}
for radius := 1; radius <= maxRadius; radius++ {
for x := middleX - radius; x <= middleX+radius; x++ {
if err := f(x, middleY+radius); err != nil {
return err
}
if err := f(x, middleY-radius); err != nil {
return err
}
}
for y := middleY - radius + 1; y < middleY+radius; y++ {
if err := f(middleX-radius, y); err != nil {
return err
}
if err := f(middleX+radius, y); err != nil {
return err
}
}
}
return nil
}