Class: AxialHex

Inherits:
BaseHex show all
Defined in:
lib/hex/axial_hex.rb

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

Attributes inherited from BaseHex

#border, #color, #data, #is, #the, #your

Instance Method Summary collapse

Constructor Details

#initialize(q, r, color: nil, border: false, data: nil) ⇒ AxialHex

Create an hexagon object

Parameters:

  • q (Integer)

    the q coordinate of the hexagon

  • r (Integer)

    the r coordinate of the hexagon

  • color (String) (defaults to: nil)

    the color of the hexagon

  • border (Boolean) (defaults to: false)

    true if the the hexagon is on the border

  • data (Object) (defaults to: nil)

    a data object associated with the hexagon. Anything you want



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

#qInteger (readonly)

the q coordinate of the hexagon

Returns:

  • (Integer)

    the current value of q



14
15
16
# File 'lib/hex/axial_hex.rb', line 14

def q
  @q
end

#rInteger (readonly)

the r coordinate of the hexagon

Returns:

  • (Integer)

    the current value of r



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

Examples:


h1 = AxialHex.new( 5, 5 )
h2 = AxialHex.new( 20, 20 )

h1.distance( h2 )  #=> 30

AxialHex.new( 5, 5 ).distance( AxialHex.new( 5, 5 ) )  #=> 0
AxialHex.new( 5, 5 ).distance( AxialHex.new( 5, 1 ) )  #=> 1

Parameters:

Returns:

  • (Integer)

    the distance between the two hexes (in hexes)



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

Returns:

  • (Boolean)

    true if the hexagon is adjacent to the other, false otherwise. Note, h.hex_surrounding_hex?( h ) == false



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

Examples:


hext_to_test = AxialHex.new( 5, 5 )
nearest_hex = AxialHex.new( 5, 6 )
far_hex = AxialHex.new( 20, 20 )

hext_to_test.nearset_hex( [ nearest_hex, far_hex ] )  #=> #<AxialHex @q=5, @r=6>

Parameters:

  • hex_array (Array<AxialHex>)

    an array of AxialHex objects

Returns:

  • (AxialHex)

    the nearset hex as a AxialHex object



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

#qrArray<Integer>

 Transform an hex to it’s q, r coordinates

Returns:

  • (Array<Integer>)

    an array [ q, r ]



136
137
138
# File 'lib/hex/axial_hex.rb', line 136

def qr
  [ q, r ]
end

#roundAxialHex

 Round an hexagon coordinates (useful after pixel to axial coordinate transformation)

Returns:

  • (AxialHex)

    an hex with coords rounded



128
129
130
# File 'lib/hex/axial_hex.rb', line 128

def round
  to_cube.round.to_axial
end

#surrounding_hexesArray<AxialHex>

Get all hexagons surrounding the current hexagon

Returns:

  • (Array<AxialHex>)

    an array of AxialHex



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_cubeCubeHex

Transform an axial represented hexagon object to a cube represented hexagon object

Returns:

  • (CubeHex)

    a new CubeHex object



51
52
53
# File 'lib/hex/axial_hex.rb', line 51

def to_cube
  CubeHex.new(@q, -@q-@r, @r)
end

#to_hashHash

Return an hex as a hash object

Returns:

  • (Hash)

    the 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