Class: CubeHex
Overview
This class represents an hexagon stored in a cube coordinate system.
Please read www.redblobgames.com/grids/hexagons/#coordinates to understand what a cube coordinates system is The cube class is only for computation. It is not intended to be used directly in your program.
Instance Attribute Summary collapse
-
#x ⇒ Integer
readonly
the x coordinate of the cube representation of the hexagon.
-
#y ⇒ Integer
readonly
the y coordinate of the cube representation of the hexagon.
-
#z ⇒ Integer
readonly
the z coordinate of the cube representation of the hexagon.
Attributes inherited from BaseHex
#border, #color, #data, #is, #the, #your
Instance Method Summary collapse
-
#distance(h) ⇒ Ingteger
Compute the distance between two hexagons (in hexagons).
-
#initialize(x, y, z, color: nil, border: nil, data: nil) ⇒ CubeHex
constructor
Create an hexagon object.
-
#round ⇒ AxialCube
Round the float coordinates to integer coordinates.
-
#to_axial ⇒ AxialHex
Transform a cube represented hexagon to an Hexagon::Axial represented hexagon.
Constructor Details
#initialize(x, y, z, color: nil, border: nil, data: nil) ⇒ CubeHex
Create an hexagon object
24 25 26 27 28 29 30 |
# File 'lib/hex/cube_hex.rb', line 24 def initialize( x, y, z, color: nil, border: nil, data: nil ) @x = x @y = y @z = z super( color, border, data ) end |
Instance Attribute Details
#x ⇒ Integer (readonly)
the x coordinate of the cube representation of the hexagon
14 15 16 |
# File 'lib/hex/cube_hex.rb', line 14 def x @x end |
#y ⇒ Integer (readonly)
the y coordinate of the cube representation of the hexagon
14 15 16 |
# File 'lib/hex/cube_hex.rb', line 14 def y @y end |
#z ⇒ Integer (readonly)
the z coordinate of the cube representation of the hexagon
14 15 16 |
# File 'lib/hex/cube_hex.rb', line 14 def z @z end |
Instance Method Details
#distance(h) ⇒ Ingteger
Compute the distance between two hexagons (in hexagons)
67 68 69 |
# File 'lib/hex/cube_hex.rb', line 67 def distance(h) [(@x - h.x).abs, (@y - h.y).abs, (@z - h.z).abs].max end |
#round ⇒ AxialCube
Round the float coordinates to integer coordinates
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hex/cube_hex.rb', line 44 def round rx=@x.round(0) ry=@y.round(0) rz=@z.round(0) x_diff=(rx-@x).abs y_diff=(ry-@y).abs z_diff=(rz-@z).abs if x_diff > y_diff and x_diff > z_diff rx = -ry-rz elsif y_diff > z_diff ry = -rx-rz else rz = -rx-ry end CubeHex.new(rx, ry, rz, color: @color, border: @border, data: @data) end |