Class: AxialGrid
- Inherits:
-
Object
- Object
- AxialGrid
- Defined in:
- lib/hex/axial_grid.rb
Overview
This class represents a grid of hexagons stored in an axial coordinate system.
Please read www.redblobgames.com/grids/hexagons/#coordinates to understand what an axial coordinates system is.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#half_height ⇒ Float
readonly
the half of the height of an hexagon.
-
#half_width ⇒ Float
readonly
the half of the width of an hexagon.
-
#hex_height ⇒ Float
readonly
the height of an hexagon.
-
#hex_ray ⇒ Float
readonly
the ray of an hexagon.
-
#hex_width ⇒ Float
readonly
the width of an hexagon.
-
#quarter_height ⇒ Float
readonly
the quarter of the height of an hexagon.
Instance Method Summary collapse
-
#cget(q, r) ⇒ AxialHex
Get the hexagon at a given position (q, r).
-
#cset(q, r, color: nil, border: false, data: nil) ⇒ AxialHex
Create an hexagon at a given position (q, r).
-
#each ⇒ Object
Call the block for each Hex in the grid.
-
#h_surrounding_hexes(h) ⇒ Array<AxialHex>
Return all surrounding hexes from grid.
-
#hex_at_xy(x, y) ⇒ AxialGrid
Get the hexagon at (x,y) coordinate.
-
#hget(hex) ⇒ AxialHex
Get the hexagon at a given position (q, r).
-
#hset(hex) ⇒ AxialHex
Insert an hexagon into the grid.
-
#initialize(hex_ray: 16, element_to_color_hash: {}) ⇒ AxialGrid
constructor
Create an axial hexagon grid.
-
#set_element_to_color_hash(element_to_color_hash) ⇒ Object
Set the hex color to color conversion hash.
-
#to_xy(hex) ⇒ Array<Integer>
Get the (x, y) position of an hexagon object.
Constructor Details
#initialize(hex_ray: 16, element_to_color_hash: {}) ⇒ AxialGrid
Create an axial hexagon grid
26 27 28 29 30 |
# File 'lib/hex/axial_grid.rb', line 26 def initialize( hex_ray: 16, element_to_color_hash: {} ) @hexes={} @element_to_color_hash = element_to_color_hash @hex_ray = hex_ray end |
Instance Attribute Details
#half_height ⇒ Float (readonly)
the half of the height of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def half_height @half_height end |
#half_width ⇒ Float (readonly)
the half of the width of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def half_width @half_width end |
#hex_height ⇒ Float (readonly)
the height of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def hex_height @hex_height end |
#hex_ray ⇒ Float (readonly)
the ray of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def hex_ray @hex_ray end |
#hex_width ⇒ Float (readonly)
the width of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def hex_width @hex_width end |
#quarter_height ⇒ Float (readonly)
the quarter of the height of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def quarter_height @quarter_height end |
Instance Method Details
#cget(q, r) ⇒ AxialHex
Get the hexagon at a given position (q, r)
71 72 73 |
# File 'lib/hex/axial_grid.rb', line 71 def cget( q, r ) @hexes[ [ q, r ] ] end |
#cset(q, r, color: nil, border: false, data: nil) ⇒ AxialHex
Create an hexagon at a given position (q, r)
50 51 52 |
# File 'lib/hex/axial_grid.rb', line 50 def cset( q, r, color: nil, border: false, data: nil ) @hexes[ [ q, r ] ] = AxialHex.new( q, r, color: color, border: border, data: data ) end |
#each ⇒ Object
Call the block for each Hex in the grid
90 91 92 |
# File 'lib/hex/axial_grid.rb', line 90 def each @hexes.sort.each{ |h| yield h[1] } end |
#h_surrounding_hexes(h) ⇒ Array<AxialHex>
Return all surrounding hexes from grid
99 100 101 |
# File 'lib/hex/axial_grid.rb', line 99 def h_surrounding_hexes( h ) h.surrounding_hexes.map{ |sh| hget( sh ) } end |
#hex_at_xy(x, y) ⇒ AxialGrid
Get the hexagon at (x,y) coordinate.
110 111 112 113 114 115 |
# File 'lib/hex/axial_grid.rb', line 110 def hex_at_xy(x, y) q = (x * Math.sqrt(3)/3.0 - y/3.0) / @hex_ray r = y * 2.0/3.0 / @hex_ray hex = AxialHex.new(q, r).round cget( hex.q, hex.r ) end |
#hget(hex) ⇒ AxialHex
Get the hexagon at a given position (q, r)
81 82 83 |
# File 'lib/hex/axial_grid.rb', line 81 def hget( hex ) @hexes[ [ hex.q, hex.r ] ] end |
#hset(hex) ⇒ AxialHex
Insert an hexagon into the grid
60 61 62 |
# File 'lib/hex/axial_grid.rb', line 60 def hset( hex ) @hexes[ [ hex.q, hex.r ] ] = hex end |
#set_element_to_color_hash(element_to_color_hash) ⇒ Object
Set the hex color to color conversion hash
36 37 38 |
# File 'lib/hex/axial_grid.rb', line 36 def set_element_to_color_hash( element_to_color_hash ) @element_to_color_hash = element_to_color_hash end |
#to_xy(hex) ⇒ Array<Integer>
Get the (x, y) position of an hexagon object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/hex/axial_grid.rb', line 124 def to_xy( hex ) tmp_q = hex.q # x = ( @hex_ray * Math.sqrt(3) * ( tmp_q + hex.r/2.0 ) ) - @hex_width # y = ( @hex_ray * 3.0/2.0 * hex.r ) - ( @quarter_height * 2 ) x = ( @hex_ray * Math.sqrt(3) * ( tmp_q + hex.r/2.0 ) ) y = ( @hex_ray * 3.0/2.0 * hex.r ) - ( @quarter_height * 2 ) [ x, y ] end |