Class: AxialGrid

Inherits:
Object
  • Object
show all
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.

Author:

  • Cédric ZUGER

Direct Known Subclasses

SquareGrid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex_ray: 16, element_to_color_hash: {}) ⇒ AxialGrid

Create an axial hexagon grid

Parameters:



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_heightFloat (readonly)

the half of the height of an hexagon

Returns:

  • (Float)

    the current value of half_height



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def half_height
  @half_height
end

#half_widthFloat (readonly)

the half of the width of an hexagon

Returns:

  • (Float)

    the current value of half_width



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def half_width
  @half_width
end

#hex_heightFloat (readonly)

the height of an hexagon

Returns:

  • (Float)

    the current value of hex_height



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def hex_height
  @hex_height
end

#hex_rayFloat (readonly)

the ray of an hexagon

Returns:

  • (Float)

    the current value of hex_ray



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def hex_ray
  @hex_ray
end

#hex_widthFloat (readonly)

the width of an hexagon

Returns:

  • (Float)

    the current value of hex_width



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def hex_width
  @hex_width
end

#quarter_heightFloat (readonly)

the quarter of the height of an hexagon

Returns:

  • (Float)

    the current value of quarter_height



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)

Parameters:

  • q (Integer)

    the q coordinate of the hexagon

  • r (Integer)

    the r coordinate of the hexagon

Returns:

  • (AxialHex)

    the hexagon at the requested position. nil if nothing



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)

Parameters:

  • q (Integer)

    the q coordinate of the hexagon

  • r (Integer)

    the r coordinate of the hexagon

  • color (String) (defaults to: nil)

    a colorstring that can be used by ImageMagic

  • border (Boolean) (defaults to: false)

    is the hex on the border of the screen (not fully draw)

  • data (Unknown) (defaults to: nil)

    some data associated with the hexagone. Everything you want, it is up to you.

Returns:



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

#eachObject

Call the block for each Hex in the grid

Examples:

mygrid.each{ |h| p h }


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

Parameters:

  • h (AxialHex)

    the hexagon you want to get surronding hexes

Returns:

  • (Array<AxialHex>)

    all surrounding hexes



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.

Parameters:

  • x (Integer)

    the x coordinate of the hexagon you want to get

  • y (Integer)

    the y coordinate of the hexagon you want to get

Returns:



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)

Parameters:

  • hex (AxialHex)

    the hexagon containing the position you want to read

Returns:

  • (AxialHex)

    the hexagon at the requested position. nil if nothing



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

Parameters:

  • hex (AxialHex)

    the hexagon you want to add into the grid

Returns:

  • (AxialHex)

    the hexagon you inserted



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

Parameters:

  • element_to_color_hash (Hash)

    see initialize



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

Parameters:

  • hex (AxialHex)

    the hexagon you want to get the position

Returns:

  • (Array<Integer>)

    an array of two integers corrsponding respectively to the x, y values



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