-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_vector.go
100 lines (82 loc) · 1.65 KB
/
template_vector.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
package main
type Vector2 struct {
x, y int
}
func (v Vector2) Plus(other Vector2) Vector2 {
return Vector2{
x: v.x + other.x,
y: v.y + other.y,
}
}
func (v Vector2) Minus(other Vector2) Vector2 {
return Vector2{
x: v.x - other.x,
y: v.y - other.y,
}
}
func (v Vector2) Times(factor int) Vector2 {
return Vector2{
x: factor * v.x,
y: factor * v.y,
}
}
func (v Vector2) Min(other Vector2) Vector2 {
return Vector2{
x: min(v.x, other.x),
y: min(v.y, other.y),
}
}
func (v Vector2) Max(other Vector2) Vector2 {
return Vector2{
x: max(v.x, other.x),
y: max(v.y, other.y),
}
}
func (v Vector2) LengthSquared() int {
return v.x*v.x + v.y*v.y
}
func (v Vector2) ManhattenLength() int {
return abs(v.x) + abs(v.y)
}
func (v Vector2) DistanceSquared(o Vector2) int {
return v.Minus(o).LengthSquared()
}
func (v Vector2) ManhattenDistance(o Vector2) int {
return v.Minus(o).ManhattenLength()
}
type Vector3 struct {
x, y, z int
}
func (v Vector3) Plus(other Vector3) Vector3 {
return Vector3{
x: v.x + other.x,
y: v.y + other.y,
z: v.z + other.z,
}
}
func (v Vector3) Minus(other Vector3) Vector3 {
return Vector3{
x: v.x - other.x,
y: v.y - other.y,
z: v.z - other.z,
}
}
func (v Vector3) Times(factor int) Vector3 {
return Vector3{
x: factor * v.x,
y: factor * v.y,
z: factor * v.z,
}
}
func (v Vector3) LengthSquared() int {
return v.x*v.x + v.y*v.y + v.z*v.z
}
func (v Vector3) ManhattenLength() int {
return abs(v.x) + abs(v.y) + abs(v.z)
}
func (v Vector3) DistanceSquared(o Vector3) int {
return v.Minus(o).LengthSquared()
}
func (v Vector3) ManhattenDistance(o Vector3) int {
return v.Minus(o).ManhattenLength()
}