-
Notifications
You must be signed in to change notification settings - Fork 7
/
coords.rb
76 lines (61 loc) · 1.37 KB
/
coords.rb
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
class PointAxial
attr_accessor :q, :r
def initialize(q, r)
@q = q
@r = r
end
def self.from_string(string)
coords = string.split.map(&:to_i)
PointAxial.new(coords[0], coords[1])
end
def to_cube
PointCube.new(@q, -@q-@r, @r)
end
def +(other)
if other.is_a?(PointAxial)
return PointAxial.new(@q+other.q, @r+other.r)
end
end
def -(other)
if other.is_a?(PointAxial)
return PointAxial.new(@q-other.q, @r-other.r)
end
end
def coerce(other)
return self, other
end
def to_s
"(%d,%d)" % [@q, @r]
end
end
class PointCube
attr_accessor :x, :y, :z
def initialize(x, y, z)
@x = x
@y = y
@z = z
end
def self.from_string(string)
coords = string.split.map(&:to_i)
PointCube.new(coords[0], coords[1], coords[2])
end
def to_axial
PointAxial.new(@x, @z)
end
def +(other)
if other.is_a?(PointCube)
return PointCube.new(@x+other.x, @y+other.y, @z+other.z)
end
end
def -(other)
if other.is_a?(PointCube)
return PointCube.new(@x-other.x, @y-other.y, @z-other.z)
end
end
def coerce(other)
return self, other
end
def to_s
"(%d,%d,%d)" % [@x, @y, @z]
end
end