rhex 1.2.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hex/axial_grid.rb +109 -0
- data/lib/hex/axial_hex.rb +122 -0
- data/lib/hex/base_hex.rb +9 -0
- data/lib/hex/cube_hex.rb +61 -0
- data/lib/hex/{ascii_to_grid.rb → modules/ascii_to_grid.rb} +4 -3
- data/lib/hex/{grid_to_pic.rb → modules/grid_to_pic.rb} +26 -7
- data/lib/hex/square_grid.rb +27 -0
- data/lib/rhex.rb +4 -3
- metadata +12 -10
- data/lib/hex/axial.rb +0 -129
- data/lib/hex/cube.rb +0 -60
- data/lib/hex/grid.rb +0 -88
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ddd109e085e6fd0019a06bcddeef878e5132045
|
4
|
+
data.tar.gz: 1693182acc5400722ae6598934f71d7dd85acf89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6db25e6a50b842d46d001f3c479c2e800f96dc0d96d66fd68e51297f6d2d8efdc52cc3c1f4abc34dc95ff1c10df3436be662b5e852a997d9c277ba18baf1411c
|
7
|
+
data.tar.gz: 0b40396657b73c09556af4923f6862a2df2b41bbce686c945ae9ab352780d4793c5b0a60a12a1401f457c72420947065b884ed52286bc2289fdf6d73ad2df25d
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require_relative 'axial_hex'
|
2
|
+
require_relative 'modules/ascii_to_grid'
|
3
|
+
require_relative 'modules/grid_to_pic'
|
4
|
+
|
5
|
+
# This class represents a grid of hexagons stored in an axial coordinate system.
|
6
|
+
#
|
7
|
+
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates to understand what an axial coordinates system is.
|
8
|
+
class AxialGrid
|
9
|
+
|
10
|
+
include GridToPic
|
11
|
+
include AsciiToGrid
|
12
|
+
|
13
|
+
# Create an hexagon object
|
14
|
+
# - +hex_ray+ is the size of an hexagon. Please read : http://www.redblobgames.com/grids/hexagons/#basics for information about the size of an hexagon.
|
15
|
+
# - +element_to_color_hash+ : is a hash that relate color (see BaseHex::Axial.new) to a color. This is used to dump your grid to a bitmap field.
|
16
|
+
#
|
17
|
+
# Example
|
18
|
+
#
|
19
|
+
# @g = Hex::Grid.new(
|
20
|
+
# element_to_color_hash: {
|
21
|
+
# m: :brown, g: :green, w: :blue
|
22
|
+
# }
|
23
|
+
# )
|
24
|
+
#
|
25
|
+
# Assuming you want all hex with a colorue of m are drawn in brown,g in green, etc ... (see GridToPic for drawin a grid)
|
26
|
+
#
|
27
|
+
# *Returns* : a new Hex::Grid object.
|
28
|
+
def initialize( hex_ray: 16, element_to_color_hash: {} )
|
29
|
+
@hexes={}
|
30
|
+
@element_to_color_hash = element_to_color_hash
|
31
|
+
@hex_ray = hex_ray
|
32
|
+
set_hex_dimensions
|
33
|
+
end
|
34
|
+
|
35
|
+
# Set the hex colorue to color conversion hash
|
36
|
+
#
|
37
|
+
# *Returns* : nothing.
|
38
|
+
def set_element_to_color_hash( element_to_color_hash )
|
39
|
+
@element_to_color_hash = element_to_color_hash
|
40
|
+
end
|
41
|
+
|
42
|
+
# Create an hexagon at a given position (q, r)
|
43
|
+
#
|
44
|
+
# You can set a color for the hexagon and set the hex as a border hex or not
|
45
|
+
#
|
46
|
+
# *Returns* : an Hex::Axial object.
|
47
|
+
def cset( q, r, color: nil, border: false, data: nil )
|
48
|
+
@hexes[ [ q, r ] ] = AxialHex.new( q, r, color: color, border: border, data: data )
|
49
|
+
end
|
50
|
+
|
51
|
+
# Same method, but accept an hexagon instead of (q, r) coords
|
52
|
+
#
|
53
|
+
# *Returns* : the created Hex::Axial object.
|
54
|
+
def hset( hex )
|
55
|
+
@hexes[ [ hex.q, hex.r ] ] = hex
|
56
|
+
end
|
57
|
+
|
58
|
+
# Get the hexagon at a given position (q, r)
|
59
|
+
#
|
60
|
+
# *Returns* : the created Hex::Axial object.
|
61
|
+
def cget( q, r )
|
62
|
+
@hexes[ [ q, r ] ]
|
63
|
+
end
|
64
|
+
|
65
|
+
# Same method, but accept an hexagon instead of (q, r) coords
|
66
|
+
#
|
67
|
+
# *Returns* : the created Hex::Axial object.
|
68
|
+
def hget( hex )
|
69
|
+
@hexes[ [ hex.q, hex.r ] ]
|
70
|
+
end
|
71
|
+
|
72
|
+
# Call the block for each Hex in the grid
|
73
|
+
#
|
74
|
+
# *Returns* : nothing
|
75
|
+
def each
|
76
|
+
@hexes.sort.each{ |h| yield h[1] }
|
77
|
+
end
|
78
|
+
|
79
|
+
# Return all surrounding hexes from grid
|
80
|
+
#
|
81
|
+
# *Returns* : Array of AxialHex
|
82
|
+
def h_surrounding_hexes( h )
|
83
|
+
h.surrounding_hexes.map{ |sh| hget( sh ) }
|
84
|
+
end
|
85
|
+
|
86
|
+
# Get the hexagon at (x,y) coordinate.
|
87
|
+
#
|
88
|
+
# *Returns* : the Hex::Axial object at x, y pos.
|
89
|
+
def hex_at_xy(x, y)
|
90
|
+
q = (x * Math.sqrt(3)/3.0 - y/3.0) / @hex_ray
|
91
|
+
r = y * 2.0/3.0 / @hex_ray
|
92
|
+
hex = AxialHex.new(q, r).round
|
93
|
+
cget( hex.q, hex.r )
|
94
|
+
end
|
95
|
+
#
|
96
|
+
# Give the position of an hexagon object in pixel.
|
97
|
+
#
|
98
|
+
# *Returns* : an array of x, y positions.
|
99
|
+
def to_xy( hex )
|
100
|
+
|
101
|
+
set_hex_dimensions
|
102
|
+
|
103
|
+
tmp_q = hex.q
|
104
|
+
x = ( @hex_ray * Math.sqrt(3) * ( tmp_q + hex.r/2.0 ) )
|
105
|
+
y = ( @hex_ray * 3.0/2.0 * hex.r )
|
106
|
+
[ x, y ]
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require_relative 'cube_hex'
|
4
|
+
|
5
|
+
# This class represents an hexagon stored in axial coordinate system.
|
6
|
+
#
|
7
|
+
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates
|
8
|
+
# to understand what an axial coordinates system is
|
9
|
+
class AxialHex < BaseHex
|
10
|
+
|
11
|
+
attr_reader :q, :r #:nodoc:
|
12
|
+
|
13
|
+
# Directions around hex from top left clockwise
|
14
|
+
DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ] #:nodoc:
|
15
|
+
|
16
|
+
# Create an hexagon object
|
17
|
+
# - +q+ and +r+ are the coordinates in the axial coords system
|
18
|
+
# - +color+ : is a colorue anything you want.
|
19
|
+
# - +border+ is a boolean and mean that the hex is at the border of the map.
|
20
|
+
#
|
21
|
+
# *Returns* : a new Hex::Axial object.
|
22
|
+
def initialize( q, r, color: nil, border: false, data: nil )
|
23
|
+
@q = q
|
24
|
+
@r = r
|
25
|
+
super( color, border, data )
|
26
|
+
end
|
27
|
+
|
28
|
+
# Check the equality between two hexagons.
|
29
|
+
def ==(h) #:nodoc:
|
30
|
+
@q==h.q && @r==h.r
|
31
|
+
end
|
32
|
+
|
33
|
+
def !=(h) #:nodoc:
|
34
|
+
@q!=h.q || @r!=h.r
|
35
|
+
end
|
36
|
+
|
37
|
+
# Transform an axial represented hexagon object to a cube represented hexagon object.
|
38
|
+
#
|
39
|
+
# *Returns* : a new Hex::Cube object.
|
40
|
+
def to_cube
|
41
|
+
CubeHex.new(@q, -@q-@r, @r)
|
42
|
+
end
|
43
|
+
|
44
|
+
# From an array of hexagons, get the nearest
|
45
|
+
# - +hex_array+ : and array of Hex::Axial objects
|
46
|
+
#
|
47
|
+
# Example
|
48
|
+
#
|
49
|
+
# hext_to_test = Hex::Axial.new( 5, 5 )
|
50
|
+
# nearest_hex = Hex::Axial.new( 5, 6 )
|
51
|
+
# far_hex = Hex::Axial.new( 20, 20 )
|
52
|
+
#
|
53
|
+
# nearest_hex.nearset_hex( [ hext_to_test, far_hex ] ) #=> #<Hex::Axial @q=5, @r=6>
|
54
|
+
#
|
55
|
+
# *Returns* : the nearset hex as a Hex::Axial object.
|
56
|
+
def nearest_hex( hex_array )
|
57
|
+
nearest_hex = nil
|
58
|
+
current_distance = nil
|
59
|
+
hex_array.each do |h|
|
60
|
+
if nearest_hex
|
61
|
+
dist = distance( h )
|
62
|
+
if distance( h ) < current_distance
|
63
|
+
nearest_hex = h
|
64
|
+
current_distance = dist
|
65
|
+
end
|
66
|
+
else
|
67
|
+
nearest_hex = h
|
68
|
+
current_distance = distance( h )
|
69
|
+
end
|
70
|
+
end
|
71
|
+
nearest_hex
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Compute the distance (in hex) between two hexagons
|
76
|
+
# - +h+ : an Hex::Axial object
|
77
|
+
#
|
78
|
+
# Example
|
79
|
+
#
|
80
|
+
# h1 = Hex::Axial.new( 5, 5 )
|
81
|
+
# h2 = Hex::Axial.new( 20, 20 )
|
82
|
+
#
|
83
|
+
# h1.distance( h2 ) #=> 30
|
84
|
+
#
|
85
|
+
# Hex::Axial.new( 5, 5 ).distance( Hex::Axial.new( 5, 5 ) ) #=> 0
|
86
|
+
# Hex::Axial.new( 5, 5 ).distance( Hex::Axial.new( 5, 1 ) ) #=> 1
|
87
|
+
#
|
88
|
+
# *Returns* : the distance between the two hexes as an integer.
|
89
|
+
def distance( h )
|
90
|
+
to_cube.distance(h.to_cube)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Get all hexagons surrounding the current hexagon
|
94
|
+
#
|
95
|
+
# *Returns* : an array of Hex::Axial.
|
96
|
+
def surrounding_hexes
|
97
|
+
# puts self.inspect, self.q.inspect, self.r.inspect
|
98
|
+
DIRECTIONS.map{ |e| AxialHex.new( @q+e[0], @r+e[1] ) }
|
99
|
+
end
|
100
|
+
|
101
|
+
# Check if an hexagon is around another hexagon
|
102
|
+
#
|
103
|
+
# *Returns* : true if the hexagon is adjacent to the other, false otherwise. Note, h.hex_surrounding_hex?( h ) == false
|
104
|
+
def hex_surrounding_hex?(hex)
|
105
|
+
distance(hex)==1
|
106
|
+
end
|
107
|
+
|
108
|
+
# Round an hexagon coordinates (useful after pixel to axial coordinate transformation)
|
109
|
+
#
|
110
|
+
# *Returns* : an Hex::Axial with coords rounded.
|
111
|
+
def round
|
112
|
+
to_cube.round.to_axial
|
113
|
+
end
|
114
|
+
|
115
|
+
# Transform an hex to it's q, r coordinates
|
116
|
+
#
|
117
|
+
# *Returns* : an array [ q, r ]
|
118
|
+
def qr
|
119
|
+
[ q, r ]
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
data/lib/hex/base_hex.rb
ADDED
data/lib/hex/cube_hex.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require_relative 'base_hex'
|
2
|
+
|
3
|
+
# This class represents an hexagon stored in a cube coordinate system.
|
4
|
+
#
|
5
|
+
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates
|
6
|
+
# to understand what a cube coordinates system is
|
7
|
+
# The cube class is only for computation.
|
8
|
+
# It is not intended to be used directly in your program.
|
9
|
+
class CubeHex < BaseHex
|
10
|
+
|
11
|
+
attr_reader :x,:y,:z #:nodoc:
|
12
|
+
|
13
|
+
# Create an hexagon object
|
14
|
+
# - +x+, +y+, +z+ are the coordinates in the axial coords system
|
15
|
+
#
|
16
|
+
# *Returns* : a new Hex::Cube object.
|
17
|
+
def initialize( x, y, z, color: nil, border: nil, data: nil )
|
18
|
+
@x = x
|
19
|
+
@y = y
|
20
|
+
@z = z
|
21
|
+
|
22
|
+
super( color, border, data )
|
23
|
+
end
|
24
|
+
|
25
|
+
# Transform a cube represented hexagon to an Hexagon::Axial represented hexagon
|
26
|
+
#
|
27
|
+
# *Returns* : a new Hex::Axial object.
|
28
|
+
def to_axial
|
29
|
+
AxialHex.new(@x, @z, color: @color, border: @border, data: @data)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Round the float coordinates to integer coordinates.
|
33
|
+
#
|
34
|
+
# *Returns* : a new Hex::Cube object.
|
35
|
+
def round
|
36
|
+
rx=@x.round(0)
|
37
|
+
ry=@y.round(0)
|
38
|
+
rz=@z.round(0)
|
39
|
+
|
40
|
+
x_diff=(rx-@x).abs
|
41
|
+
y_diff=(ry-@y).abs
|
42
|
+
z_diff=(rz-@z).abs
|
43
|
+
|
44
|
+
if x_diff > y_diff and x_diff > z_diff
|
45
|
+
rx = -ry-rz
|
46
|
+
elsif y_diff > z_diff
|
47
|
+
ry = -rx-rz
|
48
|
+
else
|
49
|
+
rz = -rx-ry
|
50
|
+
end
|
51
|
+
CubeHex.new(rx, ry, rz, color: @color, border: @border, data: @data)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Compute the distance between two hexagons (in hexagons)
|
55
|
+
#
|
56
|
+
# *Returns* : an integer : the distance between hex in hexagons.
|
57
|
+
def distance(h)
|
58
|
+
[(@x - h.x).abs, (@y - h.y).abs, (@z - h.z).abs].max
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -14,7 +14,7 @@ module AsciiToGrid
|
|
14
14
|
elements.each do |element|
|
15
15
|
border = true if ( r == 0 || q == 0 )
|
16
16
|
shifted_q = q - ( r/2 )
|
17
|
-
cset( shifted_q, r,
|
17
|
+
cset( shifted_q, r, color: element, border: border )
|
18
18
|
q += 1
|
19
19
|
end
|
20
20
|
r += 1
|
@@ -24,11 +24,12 @@ module AsciiToGrid
|
|
24
24
|
0.upto( max_r - 1 ).each do |row|
|
25
25
|
maxq = @hexes.map{ |key, e| e.q if e.r == row }.compact.max
|
26
26
|
hex = cget( maxq, row )
|
27
|
-
hex.border
|
27
|
+
hex.border = true if hex
|
28
28
|
end
|
29
29
|
|
30
|
-
@hexes.each{ |key, e| e.border
|
30
|
+
@hexes.each{ |key, e| e.border = true if e.r == ( max_r - 1 ) }
|
31
31
|
|
32
32
|
end
|
33
33
|
end
|
34
|
+
|
34
35
|
end
|
@@ -12,29 +12,45 @@ end
|
|
12
12
|
module GridToPic
|
13
13
|
|
14
14
|
attr_reader :hex_height, :hex_width #:nodoc:
|
15
|
+
attr_reader :quarter_height, :half_width #:nodoc:
|
15
16
|
|
16
|
-
# Draw
|
17
|
-
# - +pic_name+ : the name of the picture file (can be *.bmp, *.png, *.jpg)
|
17
|
+
# Draw the hex grid in a Magick::Image object
|
18
18
|
# - +exit_on_error+ : by default, if you call this method and rmagic is not installed, the program exit with an error. You can disable it and make the program continue.
|
19
19
|
#
|
20
|
-
# *Returns* :
|
21
|
-
def
|
20
|
+
# *Returns* : the Magick::Image object
|
21
|
+
def to_rmagick_image( exit_on_error = true )
|
22
22
|
unless defined?( Magick::Image ) && defined?( Magick::HatchFill ) && defined?( Magick::Draw )
|
23
23
|
puts 'Rmagick is not installed !!! You can\'t dump hex grid to pic'
|
24
24
|
exit if exit_on_error
|
25
25
|
end
|
26
26
|
|
27
|
-
maxx = @hexes.keys.map{ |k| k[0] }.max * @hex_width
|
28
|
-
maxy = @hexes.keys.map{ |k| k[1] }.max * @hex_height * 3.0/4.0
|
27
|
+
maxx = ( @hexes.keys.map{ |k| k[0] + k[1]/2 }.max ) * @hex_width - ( @hex_width / 2 ) + 1
|
28
|
+
maxy = @hexes.keys.map{ |k| k[1] }.max * ( ( @hex_height * 3.0 )/ 4.0 ).ceil - ( @hex_width / 4 ).ceil - 2
|
29
|
+
|
30
|
+
# maxx = ( ( @hexes.keys.map{ |k| k[0] + k[1]/2 }.max ) + 0.5 ) * @hex_width
|
31
|
+
# maxy = ( ( @hexes.keys.map{ |k| k[1] }.max * 3.0/4.0 ) + 0.5 ) + @hex_heig
|
29
32
|
|
30
33
|
canvas = Magick::Image.new( maxx, maxy )
|
34
|
+
|
31
35
|
gc = Magick::Draw.new
|
32
36
|
gc.stroke_antialias( true )
|
33
37
|
gc.stroke( 'black' )
|
38
|
+
gc.stroke_opacity( '60%' )
|
34
39
|
|
35
40
|
@hexes.each{ | _, hex| draw_hex( gc, hex ) }
|
36
41
|
|
37
42
|
gc.draw(canvas)
|
43
|
+
|
44
|
+
canvas
|
45
|
+
end
|
46
|
+
|
47
|
+
# Draw the hex grid on a Magick::Object and save it to a file
|
48
|
+
# - +pic_name+ : the name of the picture file (can be *.bmp, *.png, *.jpg)
|
49
|
+
# - +exit_on_error+ : by default, if you call this method and rmagic is not installed, the program exit with an error. You can disable it and make the program continue.
|
50
|
+
#
|
51
|
+
# *Returns* : true if the file was created successfully, false otherwise.
|
52
|
+
def to_pic( pic_name, exit_on_error = true )
|
53
|
+
canvas = to_rmagick_image( exit_on_error )
|
38
54
|
canvas.write( pic_name )
|
39
55
|
end
|
40
56
|
|
@@ -51,13 +67,16 @@ module GridToPic
|
|
51
67
|
end
|
52
68
|
|
53
69
|
def get_color( hex )
|
54
|
-
color = @element_to_color_hash[ hex.
|
70
|
+
color = @element_to_color_hash[ hex.color ]
|
55
71
|
color ? color : :white
|
56
72
|
end
|
57
73
|
|
58
74
|
def draw_hex( gc, hex )
|
59
75
|
x, y = to_xy( hex )
|
60
76
|
|
77
|
+
x -= @hex_width
|
78
|
+
y -= @quarter_height * 2
|
79
|
+
|
61
80
|
color = get_color( hex )
|
62
81
|
gc.fill( color.to_s )
|
63
82
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'axial_grid'
|
2
|
+
require_relative 'cube_hex'
|
3
|
+
|
4
|
+
class SquareGrid < AxialGrid
|
5
|
+
|
6
|
+
def cset( col, row, color: nil, border: false, data: nil )
|
7
|
+
hset( even_q_to_axial_hex( col, row, color: color, border: border, data: data ) )
|
8
|
+
end
|
9
|
+
|
10
|
+
def cget( col, row )
|
11
|
+
hget( even_q_to_axial_hex( col, row ) )
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def even_q_to_axial_hex( col, row, color: nil, border: false, data: nil )
|
17
|
+
# convert odd-r offset to cube
|
18
|
+
x = col - (row - (row&1)) / 2
|
19
|
+
z = row
|
20
|
+
y = -x-z
|
21
|
+
|
22
|
+
tmp_cube = CubeHex.new( x, y, z, color: color, border: border, data: data )
|
23
|
+
|
24
|
+
tmp_cube.to_axial
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/rhex.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
-
require_relative 'hex/
|
2
|
-
require_relative 'hex/
|
3
|
-
require_relative 'hex/
|
1
|
+
require_relative 'hex/axial_hex'
|
2
|
+
require_relative 'hex/cube_hex'
|
3
|
+
require_relative 'hex/axial_grid'
|
4
|
+
require_relative 'hex/square_grid'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cédric ZUGER
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
A library providing hexagons management and hexagonal grids for ruby.
|
@@ -19,11 +19,13 @@ executables: []
|
|
19
19
|
extensions: []
|
20
20
|
extra_rdoc_files: []
|
21
21
|
files:
|
22
|
-
- lib/hex/
|
23
|
-
- lib/hex/
|
24
|
-
- lib/hex/
|
25
|
-
- lib/hex/
|
26
|
-
- lib/hex/
|
22
|
+
- lib/hex/axial_grid.rb
|
23
|
+
- lib/hex/axial_hex.rb
|
24
|
+
- lib/hex/base_hex.rb
|
25
|
+
- lib/hex/cube_hex.rb
|
26
|
+
- lib/hex/modules/ascii_to_grid.rb
|
27
|
+
- lib/hex/modules/grid_to_pic.rb
|
28
|
+
- lib/hex/square_grid.rb
|
27
29
|
- lib/rhex.rb
|
28
30
|
homepage: https://github.com/czuger/rhex
|
29
31
|
licenses:
|
@@ -35,17 +37,17 @@ require_paths:
|
|
35
37
|
- lib
|
36
38
|
required_ruby_version: !ruby/object:Gem::Requirement
|
37
39
|
requirements:
|
38
|
-
- -
|
40
|
+
- - '>='
|
39
41
|
- !ruby/object:Gem::Version
|
40
42
|
version: 2.0.0
|
41
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
44
|
requirements:
|
43
|
-
- -
|
45
|
+
- - '>='
|
44
46
|
- !ruby/object:Gem::Version
|
45
47
|
version: '0'
|
46
48
|
requirements: []
|
47
49
|
rubyforge_project:
|
48
|
-
rubygems_version: 2.4.
|
50
|
+
rubygems_version: 2.4.8
|
49
51
|
signing_key:
|
50
52
|
specification_version: 4
|
51
53
|
summary: Ruby HEXagonal grid
|
data/lib/hex/axial.rb
DELETED
@@ -1,129 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
|
3
|
-
require_relative 'cube'
|
4
|
-
|
5
|
-
# This module contains is the container for all hexagon classes.
|
6
|
-
module Hex
|
7
|
-
|
8
|
-
# This class represents an hexagon stored in axial coordinate system.
|
9
|
-
#
|
10
|
-
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates
|
11
|
-
# to understand what an axial coordinates system is
|
12
|
-
class Axial
|
13
|
-
|
14
|
-
attr_reader :q, :r, :val #:nodoc:
|
15
|
-
|
16
|
-
# Directions around hex from top left clockwise
|
17
|
-
DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ] #:nodoc:
|
18
|
-
|
19
|
-
# Create an hexagon object
|
20
|
-
# - +q+ and +r+ are the coordinates in the axial coords system
|
21
|
-
# - +val+ : is a value anything you want.
|
22
|
-
# - +border+ is a boolean and mean that the hex is at the border of the map.
|
23
|
-
#
|
24
|
-
# *Returns* : a new Hex::Axial object.
|
25
|
-
def initialize( q, r, val: nil, border: false )
|
26
|
-
@q = q
|
27
|
-
@r = r
|
28
|
-
@val = val.to_sym if val
|
29
|
-
@border = border
|
30
|
-
end
|
31
|
-
|
32
|
-
# Check the equality between two hexagons.
|
33
|
-
def ==(h) #:nodoc:
|
34
|
-
@q==h.q && @r==h.r
|
35
|
-
end
|
36
|
-
|
37
|
-
# Force the border status of the hex.
|
38
|
-
#
|
39
|
-
# *Returns* : true.
|
40
|
-
def border!
|
41
|
-
@border = true
|
42
|
-
end
|
43
|
-
|
44
|
-
# Get the border status of the hex.
|
45
|
-
#
|
46
|
-
# *Returns* : true if the hex is at the border of the grid, false otherwise (only if border have been set. Return false otherwise).
|
47
|
-
def border?
|
48
|
-
!@border.nil?
|
49
|
-
end
|
50
|
-
|
51
|
-
# Transform an axial represented hexagon object to a cube represented hexagon object.
|
52
|
-
#
|
53
|
-
# *Returns* : a new Hex::Cube object.
|
54
|
-
def to_cube
|
55
|
-
Hex::Cube.new(@q,-@q-@r,@r)
|
56
|
-
end
|
57
|
-
|
58
|
-
# From an array of hexagons, get the nearest
|
59
|
-
# - +hex_array+ : and array of Hex::Axial objects
|
60
|
-
#
|
61
|
-
# Example
|
62
|
-
#
|
63
|
-
# hext_to_test = Hex::Axial.new( 5, 5 )
|
64
|
-
# nearest_hex = Hex::Axial.new( 5, 6 )
|
65
|
-
# far_hex = Hex::Axial.new( 20, 20 )
|
66
|
-
#
|
67
|
-
# nearest_hex.nearset_hex( [ hext_to_test, far_hex ] ) #=> #<Hex::Axial @q=5, @r=6>
|
68
|
-
#
|
69
|
-
# *Returns* : the nearset hex as a Hex::Axial object.
|
70
|
-
def nearest_hex( hex_array )
|
71
|
-
nearest_hex = nil
|
72
|
-
current_distance = nil
|
73
|
-
hex_array.each do |h|
|
74
|
-
unless nearest_hex
|
75
|
-
nearest_hex = h
|
76
|
-
current_distance = distance( h )
|
77
|
-
else
|
78
|
-
nearest_hex = h if distance( h ) < current_distance
|
79
|
-
end
|
80
|
-
end
|
81
|
-
nearest_hex
|
82
|
-
end
|
83
|
-
|
84
|
-
##
|
85
|
-
# Compute the distance (in hex) between two hexagons
|
86
|
-
# - +h+ : an Hex::Axial object
|
87
|
-
#
|
88
|
-
# Example
|
89
|
-
#
|
90
|
-
# h1 = Hex::Axial.new( 5, 5 )
|
91
|
-
# h2 = Hex::Axial.new( 20, 20 )
|
92
|
-
#
|
93
|
-
# h1.distance( h2 ) #=> 30
|
94
|
-
#
|
95
|
-
# Hex::Axial.new( 5, 5 ).distance( Hex::Axial.new( 5, 5 ) ) #=> 0
|
96
|
-
# Hex::Axial.new( 5, 5 ).distance( Hex::Axial.new( 5, 1 ) ) #=> 1
|
97
|
-
#
|
98
|
-
# *Returns* : the distance between the two hexes as an integer.
|
99
|
-
def distance( h )
|
100
|
-
to_cube.distance(h.to_cube)
|
101
|
-
end
|
102
|
-
|
103
|
-
# Get all hexagons surrounding the current hexagon
|
104
|
-
#
|
105
|
-
# *Returns* : an array of Hex::Axial.
|
106
|
-
def get_surrounding_hexs
|
107
|
-
# puts self.inspect, self.q.inspect, self.r.inspect
|
108
|
-
DIRECTIONS.map{ |e| Hex::Axial.new( @q+e[0], @r+e[1] ) }
|
109
|
-
end
|
110
|
-
|
111
|
-
# Check if an hexagon is around another hexagon
|
112
|
-
#
|
113
|
-
# *Returns* : true if the hexagon is adjacent to the other, false otherwise. Note, h.hex_surrounding_hex?( h ) == false
|
114
|
-
def hex_surrounding_hex?(hex)
|
115
|
-
distance(hex)==1
|
116
|
-
end
|
117
|
-
|
118
|
-
# Round an hexagon coordinates (useful after pixel to axial coordinate transformation)
|
119
|
-
#
|
120
|
-
# *Returns* : an Hex::Axial with coords rounded.
|
121
|
-
def round
|
122
|
-
to_cube.round.to_axial
|
123
|
-
end
|
124
|
-
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
|
129
|
-
|
data/lib/hex/cube.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
module Hex
|
2
|
-
|
3
|
-
# This class represents an hexagon stored in a cube coordinate system.
|
4
|
-
#
|
5
|
-
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates
|
6
|
-
# to understand what a cube coordinates system is
|
7
|
-
# The cube class is only for computation.
|
8
|
-
# It is not intended to be used directly in your program.
|
9
|
-
class Cube
|
10
|
-
|
11
|
-
attr_reader :x,:y,:z #:nodoc:
|
12
|
-
|
13
|
-
# Create an hexagon object
|
14
|
-
# - +x+, +y+, +z+ are the coordinates in the axial coords system
|
15
|
-
#
|
16
|
-
# *Returns* : a new Hex::Cube object.
|
17
|
-
def initialize(x,y,z)
|
18
|
-
@x = x
|
19
|
-
@y = y
|
20
|
-
@z = z
|
21
|
-
end
|
22
|
-
|
23
|
-
# Transform a cube represented hexagon to an Hexagon::Axial represented hexagon
|
24
|
-
#
|
25
|
-
# *Returns* : a new Hex::Axial object.
|
26
|
-
def to_axial
|
27
|
-
Hex::Axial.new(@x,@z)
|
28
|
-
end
|
29
|
-
|
30
|
-
# Round the float coordinates to integer coordinates.
|
31
|
-
#
|
32
|
-
# *Returns* : a new Hex::Cube object.
|
33
|
-
def round
|
34
|
-
rx=@x.round(0)
|
35
|
-
ry=@y.round(0)
|
36
|
-
rz=@z.round(0)
|
37
|
-
|
38
|
-
x_diff=(rx-@x).abs
|
39
|
-
y_diff=(ry-@y).abs
|
40
|
-
z_diff=(rz-@z).abs
|
41
|
-
|
42
|
-
if x_diff > y_diff and x_diff > z_diff
|
43
|
-
rx = -ry-rz
|
44
|
-
elsif y_diff > z_diff
|
45
|
-
ry = -rx-rz
|
46
|
-
else
|
47
|
-
rz = -rx-ry
|
48
|
-
end
|
49
|
-
Hex::Cube.new(rx,ry,rz)
|
50
|
-
end
|
51
|
-
|
52
|
-
# Compute the distance between two hexagons (in hexagons)
|
53
|
-
#
|
54
|
-
# *Returns* : an integer : the distance between hex in hexagons.
|
55
|
-
def distance(h)
|
56
|
-
[(@x - h.x).abs, (@y - h.y).abs, (@z - h.z).abs].max
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
data/lib/hex/grid.rb
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
require_relative 'axial'
|
2
|
-
require_relative 'grid_to_pic'
|
3
|
-
require_relative 'ascii_to_grid'
|
4
|
-
|
5
|
-
module Hex
|
6
|
-
|
7
|
-
# This class represents a grid of hexagons stored in an axial coordinate system.
|
8
|
-
#
|
9
|
-
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates to understand what an axial coordinates system is.
|
10
|
-
class Grid
|
11
|
-
|
12
|
-
include GridToPic
|
13
|
-
include AsciiToGrid
|
14
|
-
|
15
|
-
# Create an hexagon object
|
16
|
-
# - +hex_ray+ is the size of an hexagon. Please read : http://www.redblobgames.com/grids/hexagons/#basics for information about the size of an hexagon.
|
17
|
-
# - +element_to_color_hash+ : is a hash that relate val (see Hex::Axial.new) to a color. This is used to dump your grid to a bitmap field.
|
18
|
-
#
|
19
|
-
# Example
|
20
|
-
#
|
21
|
-
# @g = Hex::Grid.new(
|
22
|
-
# element_to_color_hash: {
|
23
|
-
# m: :brown, g: :green, w: :blue
|
24
|
-
# }
|
25
|
-
# )
|
26
|
-
#
|
27
|
-
# Assuming you want all hex with a value of m are drawn in brown,g in green, etc ... (see GridToPic for drawin a grid)
|
28
|
-
#
|
29
|
-
# *Returns* : a new Hex::Grid object.
|
30
|
-
def initialize( hex_ray: 16, element_to_color_hash: {} )
|
31
|
-
@hexes={}
|
32
|
-
@element_to_color_hash = element_to_color_hash
|
33
|
-
@hex_ray = hex_ray
|
34
|
-
set_hex_dimensions
|
35
|
-
end
|
36
|
-
|
37
|
-
# Create an hexagon at a given position (q, r)
|
38
|
-
#
|
39
|
-
# You can set a value for the hexagon and set the hex as a border hex or not
|
40
|
-
#
|
41
|
-
# *Returns* : an Hex::Axial object.
|
42
|
-
def cset( q, r, val: nil, border: false )
|
43
|
-
@hexes[ [ q, r ] ] = Hex::Axial.new( q, r, val: val, border: border )
|
44
|
-
end
|
45
|
-
|
46
|
-
# Same method, but accept an hexagon instead of (q, r) coords
|
47
|
-
#
|
48
|
-
# *Returns* : the created Hex::Axial object.
|
49
|
-
def hset( hex, val: nil, border: false )
|
50
|
-
@hexes[ [ hex.q, hex.r ] ] = Hex::Axial.new( hex.q, hex.r, val: val, border: border )
|
51
|
-
end
|
52
|
-
|
53
|
-
# Get the hexagon at a given position (q, r)
|
54
|
-
#
|
55
|
-
# *Returns* : the created Hex::Axial object.
|
56
|
-
def cget( q, r )
|
57
|
-
@hexes[ [ q, r ] ]
|
58
|
-
end
|
59
|
-
|
60
|
-
# Same method, but accept an hexagon instead of (q, r) coords
|
61
|
-
#
|
62
|
-
# *Returns* : the created Hex::Axial object.
|
63
|
-
def hget( hex )
|
64
|
-
@hexes[ [ hex.q, hex.r ] ]
|
65
|
-
end
|
66
|
-
|
67
|
-
# Get the hexagon at (x,y) coordinate.
|
68
|
-
#
|
69
|
-
# *Returns* : the Hex::Axial object at x, y pos.
|
70
|
-
def hex_at_xy(x, y)
|
71
|
-
q = (x * Math.sqrt(3)/3.0 - y/3.0) / @hex_ray
|
72
|
-
r = y * 2.0/3.0 / @hex_ray
|
73
|
-
hex = Hex::Axial.new(q, r).round
|
74
|
-
cget( hex.q, hex.r )
|
75
|
-
end
|
76
|
-
|
77
|
-
# Give the position of an hexagon object in pixel.
|
78
|
-
#
|
79
|
-
# *Returns* : an array of x, y positions.
|
80
|
-
def to_xy( hex )
|
81
|
-
tmp_q = hex.q
|
82
|
-
x = @hex_ray * Math.sqrt(3) * ( tmp_q + hex.r/2.0 )
|
83
|
-
y = @hex_ray * 3.0/2.0 * hex.r
|
84
|
-
[ x, y ]
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
end
|