Class: AxialHex
Overview
This class represents an hexagon stored in axial coordinate system.
Please read www.redblobgames.com/grids/hexagons/#coordinates to understand what an axial coordinates system is
Constant Summary collapse
- DIRECTIONS =
Directions around hex from top left clockwise
[ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ]
Instance Attribute Summary collapse
-
#q ⇒ Integer
readonly
the q coordinate of the hexagon.
-
#r ⇒ Integer
readonly
the r coordinate of the hexagon.
Attributes inherited from BaseHex
#border, #color, #data, #is, #the, #your
Instance Method Summary collapse
-
#!=(h) ⇒ Object
Test the inequality between two hexagons.
-
#==(h) ⇒ Object
Test the equality between two hexagons.
-
#distance(h) ⇒ Integer
Compute the distance (in hex) between two hexagons.
-
#hex_surrounding_hex?(hex) ⇒ Boolean
Check if an hexagon is around another hexagon.
-
#initialize(q, r, color: nil, border: false, data: nil) ⇒ AxialHex
constructor
Create an hexagon object.
-
#nearest_hex(hex_array) ⇒ AxialHex
From an array of hexagons, get the nearest.
-
#qr ⇒ Array<Integer>
Transform an hex to it’s q, r coordinates.
-
#round ⇒ AxialHex
Round an hexagon coordinates (useful after pixel to axial coordinate transformation).
-
#surrounding_hexes ⇒ Array<AxialHex>
Get all hexagons surrounding the current hexagon.
-
#to_cube ⇒ CubeHex
Transform an axial represented hexagon object to a cube represented hexagon object.
-
#to_hash ⇒ Hash
Return an hex as a hash object.
Constructor Details
#initialize(q, r, color: nil, border: false, data: nil) ⇒ AxialHex
Create an hexagon object
31 32 33 34 35 |
# File 'lib/hex/axial_hex.rb', line 31 def initialize( q, r, color: nil, border: false, data: nil ) @q = q @r = r super( color, border, data ) end |
Instance Attribute Details
#q ⇒ Integer (readonly)
the q coordinate of the hexagon
14 15 16 |
# File 'lib/hex/axial_hex.rb', line 14 def q @q end |
#r ⇒ Integer (readonly)
the r coordinate of the hexagon
14 15 16 |
# File 'lib/hex/axial_hex.rb', line 14 def r @r end |
Instance Method Details
#!=(h) ⇒ Object
Test the inequality between two hexagons
43 44 45 |
# File 'lib/hex/axial_hex.rb', line 43 def !=(h) @q!=h.q || @r!=h.r end |
#==(h) ⇒ Object
Test the equality between two hexagons
38 39 40 |
# File 'lib/hex/axial_hex.rb', line 38 def ==(h) @q==h.q && @r==h.r end |
#distance(h) ⇒ Integer
Compute the distance (in hex) between two hexagons
103 104 105 |
# File 'lib/hex/axial_hex.rb', line 103 def distance( h ) to_cube.distance(h.to_cube) end |
#hex_surrounding_hex?(hex) ⇒ Boolean
Check if an hexagon is around another hexagon
120 121 122 |
# File 'lib/hex/axial_hex.rb', line 120 def hex_surrounding_hex?(hex) distance(hex)==1 end |
#nearest_hex(hex_array) ⇒ AxialHex
From an array of hexagons, get the nearest
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/hex/axial_hex.rb', line 69 def nearest_hex( hex_array ) nearest_hex = nil current_distance = nil hex_array.each do |h| if nearest_hex dist = distance( h ) if distance( h ) < current_distance nearest_hex = h current_distance = dist end else nearest_hex = h current_distance = distance( h ) end end nearest_hex end |
#qr ⇒ Array<Integer>
Transform an hex to it’s q, r coordinates
136 137 138 |
# File 'lib/hex/axial_hex.rb', line 136 def qr [ q, r ] end |
#round ⇒ AxialHex
Round an hexagon coordinates (useful after pixel to axial coordinate transformation)
128 129 130 |
# File 'lib/hex/axial_hex.rb', line 128 def round to_cube.round.to_axial end |
#surrounding_hexes ⇒ Array<AxialHex>
Get all hexagons surrounding the current hexagon
111 112 113 114 |
# File 'lib/hex/axial_hex.rb', line 111 def surrounding_hexes # puts self.inspect, self.q.inspect, self.r.inspect DIRECTIONS.map{ |e| AxialHex.new( @q+e[0], @r+e[1] ) } end |
#to_cube ⇒ CubeHex
Transform an axial represented hexagon object to a cube represented hexagon object
51 52 53 |
# File 'lib/hex/axial_hex.rb', line 51 def to_cube CubeHex.new(@q, -@q-@r, @r) end |
#to_hash ⇒ Hash
Return an hex as a hash object
143 144 145 |
# File 'lib/hex/axial_hex.rb', line 143 def to_hash { q: @q, r: @r, color: @color, border: @border, data: @data.to_hash } end |