This repository was archived by the owner on Jun 11, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathInt2.go
More file actions
99 lines (82 loc) · 1.92 KB
/
Copy pathInt2.go
File metadata and controls
99 lines (82 loc) · 1.92 KB
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
package hebiten
import "hgo"
type Int2 struct {
X, Y int
}
func (this Int2) GetLinearIndex(width int) int {
return this.X + this.Y*width
}
func (this Int2) RunForEach(f func(position Int2)) {
for y := 0; y < this.Y; y++ {
for x := 0; x < this.X; x++ {
f(Int2{x, y})
}
}
}
func (this Int2) GetProduct() int {
return this.X * this.Y
}
func (this Int2) ToString() string {
return hgo.IntToStr(this.X) + "," + hgo.IntToStr(this.Y)
}
func (this Int2) GetProductByInt(multiplier int) BigFloat2 {
var m = float64(multiplier)
return BigFloat2{X: float64(this.X) * m, Y: float64(this.Y) * m}
}
func (this Int2) ToBigFloat() BigFloat2 {
return BigFloat2{
X: float64(this.X),
Y: float64(this.Y),
}
}
func (this Int2) AddInts(x, y int) Int2 {
return Int2{X: this.X + x, Y: this.Y + y}
}
func (this Int2) GetNearest4() []Int2 {
return []Int2{
this.AddInts(-1, 0),
this.AddInts(1, 0),
this.AddInts(0, -1),
this.AddInts(0, 1),
}
}
func (this Int2) Equals(a Int2) bool {
return this.X == a.X && this.Y == a.Y
}
func (this Int2) CheckIfNearest4(a Int2) (result bool) {
for _, item := range this.GetNearest4() {
if item.Equals(a) {
result = true
break
}
}
return
}
func (this Int2) GetFromDirection(a Direction) (result Int2) {
switch a {
case DirectionUp:
result = Int2{X: 0, Y: -1}
case DirectionDown:
result = Int2{X: 0, Y: 1}
case DirectionLeft:
result = Int2{X: -1, Y: 0}
case DirectionRight:
result = Int2{X: 1, Y: 0}
}
return
}
func (this Int2) Add(a Int2) Int2 {
return Int2{X: this.X + a.X, Y: this.Y + a.Y}
}
func (this Int2) MoveByDirection(direction Direction) (result Int2) {
result = this.Add(Int2{}.GetFromDirection(direction))
return
}
// Check pos is a valid position in 2D array of size = this
func (this Int2) CheckInRange(pos Int2) bool {
return 0 <= pos.X && pos.X < this.X &&
0 <= pos.Y && pos.Y < this.Y
}
func (this Int2) CheckIf0() bool {
return this.X == 0 && this.Y == 0
}