Class: Waxy::Geometry::Hex
- Inherits:
-
Object
- Object
- Waxy::Geometry::Hex
- Defined in:
- lib/waxy/geometry/hex.rb
Instance Attribute Summary collapse
-
#q ⇒ Object
Returns the value of attribute q.
-
#r ⇒ Object
Returns the value of attribute r.
-
#s ⇒ Object
Returns the value of attribute s.
-
#size ⇒ Array
Initialized here for foreseable geometry calculations, but in general this should be handled through a Meta.
Instance Method Summary collapse
- #!=(b) ⇒ Object
- #*(k) ⇒ Object
- #+(b) ⇒ Object
- #-(b) ⇒ Object
- #==(b) ⇒ Object
- #distance(b) ⇒ Object
- #hex_direction(i) ⇒ Object
- #hex_length(hex) ⇒ Object
-
#hex_neighbor(i) ⇒ Object
(0..5, requires modulo prior).
-
#initialize(_q, _r, _s = nil) ⇒ Hex
constructor
All params are Int.
Constructor Details
#initialize(_q, _r, _s = nil) ⇒ Hex
All params are Int
14 15 16 17 18 |
# File 'lib/waxy/geometry/hex.rb', line 14 def initialize(_q, _r, _s = nil) @q = _q @r = _r @s = _s end |
Instance Attribute Details
#q ⇒ Object
Returns the value of attribute q.
5 6 7 |
# File 'lib/waxy/geometry/hex.rb', line 5 def q @q end |
#r ⇒ Object
Returns the value of attribute r.
5 6 7 |
# File 'lib/waxy/geometry/hex.rb', line 5 def r @r end |
#s ⇒ Object
Returns the value of attribute s.
5 6 7 |
# File 'lib/waxy/geometry/hex.rb', line 5 def s @s end |
#size ⇒ Array
Returns initialized here for foreseable geometry calculations, but in general this should be handled through a Meta.
11 12 13 |
# File 'lib/waxy/geometry/hex.rb', line 11 def size @size end |
Instance Method Details
#!=(b) ⇒ Object
32 33 34 |
# File 'lib/waxy/geometry/hex.rb', line 32 def != (b) !(self == b) end |
#*(k) ⇒ Object
46 47 48 |
# File 'lib/waxy/geometry/hex.rb', line 46 def * (k) Hex.new(q * k, r * k, (s ? s * k : nil)) end |
#+(b) ⇒ Object
36 37 38 |
# File 'lib/waxy/geometry/hex.rb', line 36 def + (b) Hex.new(q + b.q, r + b.r, ( s ? s + b.s : nil)) end |
#-(b) ⇒ Object
40 41 42 |
# File 'lib/waxy/geometry/hex.rb', line 40 def - (b) Hex.new(q - b.q, r - b.r, (s ? s - b.s : nil)) end |
#==(b) ⇒ Object
28 29 30 |
# File 'lib/waxy/geometry/hex.rb', line 28 def == (b) q == b.q && r == b.r && s == b.s end |
#distance(b) ⇒ Object
54 55 56 |
# File 'lib/waxy/geometry/hex.rb', line 54 def distance(b) hex_length(self - b) end |
#hex_direction(i) ⇒ Object
58 59 60 61 |
# File 'lib/waxy/geometry/hex.rb', line 58 def hex_direction(i) raise if !(0..5).include?(i) HEX_DIRECTIONS_FLAT[i] end |
#hex_length(hex) ⇒ Object
50 51 52 |
# File 'lib/waxy/geometry/hex.rb', line 50 def hex_length(hex) ((hex.q.abs + hex.r.abs + hex.s.abs) / 2).to_i end |
#hex_neighbor(i) ⇒ Object
(0..5, requires modulo prior)
64 65 66 |
# File 'lib/waxy/geometry/hex.rb', line 64 def hex_neighbor(i) self + hex_direction(i) end |