-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexGrid.scala
39 lines (32 loc) · 1.35 KB
/
HexGrid.scala
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
package competitivescala.utils
object HexGrid {
case class HexPoint(x: Int = 0, y: Int = 0, z: Int = 0) {
def pointwise(f: (Int, Int) => Int)(that: HexPoint): HexPoint = HexPoint(f(x, that.x), f(y, that.y), f(z, that.z))
def map(f: Int => Int): HexPoint = HexPoint(f(x), f(y), f(z))
def +(that: HexPoint): HexPoint = pointwise(_ + _)(that)
def -(that: HexPoint): HexPoint = pointwise(_ - _)(that)
def unary_- : HexPoint = map(-_)
def hexEuclidean: Int = (x.abs + y.abs + z.abs) / 2
}
object HexRing {
val northeast: HexPoint = HexPoint(y = 1, z = -1)
val east: HexPoint = HexPoint(x = 1, z = -1)
val southeast: HexPoint = HexPoint(x = 1, y = -1)
val southwest: HexPoint = -northeast
val west: HexPoint = -east
val northwest: HexPoint = -southeast
val directions: Seq[HexPoint] = Seq(northeast, east, southeast, southwest, west, northwest)
}
// Alternative naming
/*
object HexRingAlt {
val north: HexPoint = HexPoint(y = 1, z = -1)
val northeast: HexPoint = HexPoint(x = 1, z = -1)
val southeast: HexPoint = HexPoint(x = 1, y = -1)
val south: HexPoint = HexPoint(y = -1, z = 1)
val southwest: HexPoint = HexPoint(x = -1, z = 1)
val northwest: HexPoint = HexPoint(x = -1, y = 1)
val directions: Seq[HexPoint] = Seq(north, northeast, southeast, south, southwest, northwest)
}
*/
}