-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.go
79 lines (67 loc) · 1.39 KB
/
point.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
package eugor
import "math"
type Direction string
const (
North Direction = "north"
South = "south"
East = "east"
West = "west"
)
type Point struct {
X int
Y int
}
func MakePoint(x, y int) Point {
return Point{X: x, Y: y}
}
func MakePoints(p Point, dirs []string) []Point {
points := make([]Point, len(dirs))
var newPoint Point
for i, name := range dirs {
switch name {
case "up":
newPoint = MakePoint(p.X, p.Y+1)
case "down":
newPoint = MakePoint(p.X, p.Y-1)
case "left":
newPoint = MakePoint(p.X-1, p.Y)
case "right":
newPoint = MakePoint(p.X+1, p.Y)
}
points[i] = newPoint
}
return points
}
func DetermineDirection(source, dest Point) (dir Direction) {
if source.Y == dest.Y {
if source.X < dest.X {
dir = East
} else {
dir = West
}
} else {
if source.Y < dest.Y {
dir = South
} else {
dir = North
}
}
return dir
}
func (p Point) LessThan(other Point) bool {
return p.X < other.X && p.Y < other.Y
}
func (p Point) GreaterThan(other Point) bool {
return p.X > other.X && p.Y > other.Y
}
func (p Point) Plus(other Point) Point {
return MakePoint(p.X+other.X, p.Y+other.Y)
}
func (p Point) Minus(other Point) Point {
return MakePoint(p.X-other.X, p.Y-other.Y)
}
func (p Point) Distance(other Point) float64 {
dx := float64(Abs(p.X - other.X))
dy := float64(Abs(p.Y - other.Y))
return math.Sqrt(dx*dx + dy*dy)
}