rhex 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/hex/axial_grid.rb +60 -33
- data/lib/hex/axial_hex.rb +48 -32
- data/lib/hex/base_hex.rb +9 -2
- data/lib/hex/cube_hex.rb +17 -7
- data/lib/hex/modules/ascii_to_grid.rb +10 -5
- data/lib/hex/modules/grid_to_pic.rb +14 -12
- data/lib/hex/square_grid.rb +43 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34f24c5201b8121902c9444446ba117b6ea44796
|
4
|
+
data.tar.gz: e66cdc381f1d92a64a5c55da8b89d1cc2c8c0602
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e58d0a6d7c1bb83d1d4c13fe53059095d08f170e88db9b9da976d9fbbfb5e704b559334991ecdbf3f3d4e3df237df492f9f03d04083012d4d247b119e31e6497
|
7
|
+
data.tar.gz: 0a1fd3adf58cc2f0991977e127f00debf25697d4eb90a35106c1f5397e5486854a9794e6796602ff536de5dbf6260bb5e2fc2a8d914d4fae19a3232013e20936
|
data/lib/hex/axial_grid.rb
CHANGED
@@ -5,104 +5,131 @@ require_relative 'modules/grid_to_pic'
|
|
5
5
|
# This class represents a grid of hexagons stored in an axial coordinate system.
|
6
6
|
#
|
7
7
|
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates to understand what an axial coordinates system is.
|
8
|
+
#
|
9
|
+
# @author Cédric ZUGER
|
10
|
+
#
|
11
|
+
# @attr_reader [Float] hex_ray the ray of an hexagon
|
12
|
+
# @attr_reader [Float] hex_height the height of an hexagon
|
13
|
+
# @attr_reader [Float] hex_width the width of an hexagon
|
14
|
+
# @attr_reader [Float] quarter_height the quarter of the height of an hexagon
|
15
|
+
# @attr_reader [Float] half_height the half of the height of an hexagon
|
16
|
+
# @attr_reader [Float] half_width the half of the width of an hexagon
|
17
|
+
#
|
8
18
|
class AxialGrid
|
9
19
|
|
10
|
-
|
11
|
-
include AsciiToGrid
|
20
|
+
attr_reader :hex_ray, :hex_height, :hex_width, :quarter_height, :half_width
|
12
21
|
|
13
|
-
# Create an hexagon
|
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.
|
22
|
+
# Create an axial hexagon grid
|
16
23
|
#
|
17
|
-
#
|
24
|
+
# @param hex_ray [Integer] the size of an hexagon. Please read : http://www.redblobgames.com/grids/hexagons/#basics for information about the size of an hexagon
|
18
25
|
#
|
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
26
|
def initialize( hex_ray: 16, element_to_color_hash: {} )
|
29
27
|
@hexes={}
|
30
28
|
@element_to_color_hash = element_to_color_hash
|
31
29
|
@hex_ray = hex_ray
|
32
|
-
set_hex_dimensions
|
33
30
|
end
|
34
31
|
|
35
|
-
# Set the hex
|
32
|
+
# Set the hex color to color conversion hash
|
33
|
+
#
|
34
|
+
# @param element_to_color_hash [Hash] see initialize
|
36
35
|
#
|
37
|
-
# *Returns* : nothing.
|
38
36
|
def set_element_to_color_hash( element_to_color_hash )
|
39
37
|
@element_to_color_hash = element_to_color_hash
|
40
38
|
end
|
41
39
|
|
42
40
|
# Create an hexagon at a given position (q, r)
|
43
41
|
#
|
44
|
-
#
|
42
|
+
# @param q [Integer] the q coordinate of the hexagon
|
43
|
+
# @param r [Integer] the r coordinate of the hexagon
|
44
|
+
# @param color [String] a colorstring that can be used by ImageMagic
|
45
|
+
# @param border [Boolean] is the hex on the border of the screen (not fully draw)
|
46
|
+
# @param data [Unknown] some data associated with the hexagone. Everything you want, it is up to you.
|
47
|
+
#
|
48
|
+
# @return [AxialHex] an hexagon
|
45
49
|
#
|
46
|
-
# *Returns* : an Hex::Axial object.
|
47
50
|
def cset( q, r, color: nil, border: false, data: nil )
|
48
51
|
@hexes[ [ q, r ] ] = AxialHex.new( q, r, color: color, border: border, data: data )
|
49
52
|
end
|
50
53
|
|
51
|
-
#
|
54
|
+
# Insert an hexagon into the grid
|
55
|
+
#
|
56
|
+
# @param hex [AxialHex] the hexagon you want to add into the grid
|
57
|
+
#
|
58
|
+
# @return [AxialHex] the hexagon you inserted
|
52
59
|
#
|
53
|
-
# *Returns* : the created Hex::Axial object.
|
54
60
|
def hset( hex )
|
55
61
|
@hexes[ [ hex.q, hex.r ] ] = hex
|
56
62
|
end
|
57
63
|
|
58
64
|
# Get the hexagon at a given position (q, r)
|
59
65
|
#
|
60
|
-
#
|
66
|
+
# @param q [Integer] the q coordinate of the hexagon
|
67
|
+
# @param r [Integer] the r coordinate of the hexagon
|
68
|
+
#
|
69
|
+
# @return [AxialHex] the hexagon at the requested position. nil if nothing
|
70
|
+
#
|
61
71
|
def cget( q, r )
|
62
72
|
@hexes[ [ q, r ] ]
|
63
73
|
end
|
64
74
|
|
65
|
-
#
|
75
|
+
# Get the hexagon at a given position (q, r)
|
76
|
+
#
|
77
|
+
# @param hex [AxialHex] the hexagon containing the position you want to read
|
78
|
+
#
|
79
|
+
# @return [AxialHex] the hexagon at the requested position. nil if nothing
|
66
80
|
#
|
67
|
-
# *Returns* : the created Hex::Axial object.
|
68
81
|
def hget( hex )
|
69
82
|
@hexes[ [ hex.q, hex.r ] ]
|
70
83
|
end
|
71
84
|
|
72
85
|
# Call the block for each Hex in the grid
|
73
86
|
#
|
74
|
-
#
|
87
|
+
# @example
|
88
|
+
# mygrid.each{ |h| p h }
|
89
|
+
#
|
75
90
|
def each
|
76
91
|
@hexes.sort.each{ |h| yield h[1] }
|
77
92
|
end
|
78
93
|
|
79
94
|
# Return all surrounding hexes from grid
|
80
95
|
#
|
81
|
-
#
|
96
|
+
# @param h [AxialHex] the hexagon you want to get surronding hexes
|
97
|
+
#
|
98
|
+
# @return [Array<AxialHex>] all surrounding hexes
|
82
99
|
def h_surrounding_hexes( h )
|
83
100
|
h.surrounding_hexes.map{ |sh| hget( sh ) }
|
84
101
|
end
|
85
102
|
|
86
103
|
# Get the hexagon at (x,y) coordinate.
|
87
104
|
#
|
88
|
-
#
|
105
|
+
# @param x [Integer] the x coordinate of the hexagon you want to get
|
106
|
+
# @param y [Integer] the y coordinate of the hexagon you want to get
|
107
|
+
#
|
108
|
+
# @return [AxialGrid] the corresponding hex
|
109
|
+
#
|
89
110
|
def hex_at_xy(x, y)
|
90
111
|
q = (x * Math.sqrt(3)/3.0 - y/3.0) / @hex_ray
|
91
112
|
r = y * 2.0/3.0 / @hex_ray
|
92
113
|
hex = AxialHex.new(q, r).round
|
93
114
|
cget( hex.q, hex.r )
|
94
115
|
end
|
116
|
+
|
117
|
+
#
|
118
|
+
# Get the (x, y) position of an hexagon object
|
119
|
+
#
|
120
|
+
# @param hex [AxialHex] the hexagon you want to get the position
|
95
121
|
#
|
96
|
-
#
|
122
|
+
# @return [Array<Integer>] an array of two integers corrsponding respectively to the x, y values
|
97
123
|
#
|
98
|
-
# *Returns* : an array of x, y positions.
|
99
124
|
def to_xy( hex )
|
100
125
|
|
101
|
-
set_hex_dimensions
|
102
|
-
|
103
126
|
tmp_q = hex.q
|
127
|
+
# x = ( @hex_ray * Math.sqrt(3) * ( tmp_q + hex.r/2.0 ) ) - @hex_width
|
128
|
+
# y = ( @hex_ray * 3.0/2.0 * hex.r ) - ( @quarter_height * 2 )
|
129
|
+
|
104
130
|
x = ( @hex_ray * Math.sqrt(3) * ( tmp_q + hex.r/2.0 ) )
|
105
|
-
y = ( @hex_ray * 3.0/2.0 * hex.r )
|
131
|
+
y = ( @hex_ray * 3.0/2.0 * hex.r ) - ( @quarter_height * 2 )
|
132
|
+
|
106
133
|
[ x, y ]
|
107
134
|
end
|
108
135
|
|
data/lib/hex/axial_hex.rb
CHANGED
@@ -4,55 +4,66 @@ require_relative 'cube_hex'
|
|
4
4
|
|
5
5
|
# This class represents an hexagon stored in axial coordinate system.
|
6
6
|
#
|
7
|
-
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates
|
8
|
-
#
|
7
|
+
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates to understand what an axial coordinates system is
|
8
|
+
#
|
9
|
+
# @attr_reader [Integer] q the q coordinate of the hexagon
|
10
|
+
# @attr_reader [Integer] r the r coordinate of the hexagon
|
11
|
+
#
|
9
12
|
class AxialHex < BaseHex
|
10
13
|
|
11
|
-
attr_reader :q, :r
|
14
|
+
attr_reader :q, :r
|
12
15
|
|
13
16
|
# Directions around hex from top left clockwise
|
14
|
-
DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ]
|
17
|
+
DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ]
|
15
18
|
|
16
19
|
# 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
20
|
#
|
21
|
-
#
|
21
|
+
# @param q [Integer] the q coordinate of the hexagon
|
22
|
+
# @param r [Integer] the r coordinate of the hexagon
|
23
|
+
# @param color [String] the color of the hexagon
|
24
|
+
# @param border [Boolean] true if the the hexagon is on the border
|
25
|
+
# @param data [Object] a data object associated with the hexagon. Anything you want
|
26
|
+
#
|
27
|
+
# @return [AxialHex] the AxialHex you created
|
28
|
+
#
|
22
29
|
def initialize( q, r, color: nil, border: false, data: nil )
|
23
30
|
@q = q
|
24
31
|
@r = r
|
25
32
|
super( color, border, data )
|
26
33
|
end
|
27
34
|
|
28
|
-
#
|
29
|
-
def ==(h)
|
35
|
+
# Test the equality between two hexagons
|
36
|
+
def ==(h)
|
30
37
|
@q==h.q && @r==h.r
|
31
38
|
end
|
32
39
|
|
33
|
-
|
40
|
+
# Test the inequality between two hexagons
|
41
|
+
def !=(h)
|
34
42
|
@q!=h.q || @r!=h.r
|
35
43
|
end
|
36
44
|
|
37
|
-
# Transform an axial represented hexagon object to a cube represented hexagon object
|
45
|
+
# Transform an axial represented hexagon object to a cube represented hexagon object
|
46
|
+
#
|
47
|
+
# @return [CubeHex] a new CubeHex object
|
38
48
|
#
|
39
|
-
# *Returns* : a new Hex::Cube object.
|
40
49
|
def to_cube
|
41
50
|
CubeHex.new(@q, -@q-@r, @r)
|
42
51
|
end
|
43
52
|
|
44
53
|
# From an array of hexagons, get the nearest
|
45
|
-
# - +hex_array+ : and array of Hex::Axial objects
|
46
54
|
#
|
47
|
-
#
|
55
|
+
# @param hex_array [Array<AxialHex>] an array of AxialHex objects
|
56
|
+
#
|
57
|
+
# @example
|
58
|
+
#
|
59
|
+
# hext_to_test = AxialHex.new( 5, 5 )
|
60
|
+
# nearest_hex = AxialHex.new( 5, 6 )
|
61
|
+
# far_hex = AxialHex.new( 20, 20 )
|
48
62
|
#
|
49
|
-
# hext_to_test
|
50
|
-
# nearest_hex = Hex::Axial.new( 5, 6 )
|
51
|
-
# far_hex = Hex::Axial.new( 20, 20 )
|
63
|
+
# hext_to_test.nearset_hex( [ nearest_hex, far_hex ] ) #=> #<AxialHex @q=5, @r=6>
|
52
64
|
#
|
53
|
-
#
|
65
|
+
# @return [AxialHex] the nearset hex as a AxialHex object
|
54
66
|
#
|
55
|
-
# *Returns* : the nearset hex as a Hex::Axial object.
|
56
67
|
def nearest_hex( hex_array )
|
57
68
|
nearest_hex = nil
|
58
69
|
current_distance = nil
|
@@ -71,28 +82,30 @@ class AxialHex < BaseHex
|
|
71
82
|
nearest_hex
|
72
83
|
end
|
73
84
|
|
74
|
-
##
|
75
85
|
# Compute the distance (in hex) between two hexagons
|
76
|
-
# - +h+ : an Hex::Axial object
|
77
86
|
#
|
78
|
-
#
|
87
|
+
# @param h [AxialHex] a AxialHex object
|
79
88
|
#
|
80
|
-
#
|
81
|
-
#
|
89
|
+
# @example
|
90
|
+
#
|
91
|
+
# h1 = AxialHex.new( 5, 5 )
|
92
|
+
# h2 = AxialHex.new( 20, 20 )
|
82
93
|
#
|
83
94
|
# h1.distance( h2 ) #=> 30
|
84
95
|
#
|
85
|
-
#
|
86
|
-
#
|
96
|
+
# AxialHex.new( 5, 5 ).distance( AxialHex.new( 5, 5 ) ) #=> 0
|
97
|
+
# AxialHex.new( 5, 5 ).distance( AxialHex.new( 5, 1 ) ) #=> 1
|
98
|
+
#
|
99
|
+
# @return [Integer] the distance between the two hexes (in hexes)
|
87
100
|
#
|
88
|
-
# *Returns* : the distance between the two hexes as an integer.
|
89
101
|
def distance( h )
|
90
102
|
to_cube.distance(h.to_cube)
|
91
103
|
end
|
92
104
|
|
93
105
|
# Get all hexagons surrounding the current hexagon
|
94
106
|
#
|
95
|
-
#
|
107
|
+
# @return [Array<AxialHex>] an array of AxialHex
|
108
|
+
#
|
96
109
|
def surrounding_hexes
|
97
110
|
# puts self.inspect, self.q.inspect, self.r.inspect
|
98
111
|
DIRECTIONS.map{ |e| AxialHex.new( @q+e[0], @r+e[1] ) }
|
@@ -100,21 +113,24 @@ class AxialHex < BaseHex
|
|
100
113
|
|
101
114
|
# Check if an hexagon is around another hexagon
|
102
115
|
#
|
103
|
-
#
|
116
|
+
# @return [Boolean] true if the hexagon is adjacent to the other, false otherwise. Note, h.hex_surrounding_hex?( h ) == false
|
117
|
+
#
|
104
118
|
def hex_surrounding_hex?(hex)
|
105
119
|
distance(hex)==1
|
106
120
|
end
|
107
121
|
|
108
122
|
# Round an hexagon coordinates (useful after pixel to axial coordinate transformation)
|
109
123
|
#
|
110
|
-
#
|
124
|
+
# @return [AxialHex] an hex with coords rounded
|
125
|
+
#
|
111
126
|
def round
|
112
127
|
to_cube.round.to_axial
|
113
128
|
end
|
114
129
|
|
115
130
|
# Transform an hex to it's q, r coordinates
|
116
131
|
#
|
117
|
-
#
|
132
|
+
# @return [Array<Integer>] an array [ q, r ]
|
133
|
+
#
|
118
134
|
def qr
|
119
135
|
[ q, r ]
|
120
136
|
end
|
data/lib/hex/base_hex.rb
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
+
# This is the base hexagon class designed to be derived into axial and cube hexagons.
|
2
|
+
# Sould never been instancied.
|
3
|
+
#
|
4
|
+
# @attr_reader [String] the color of the hexagon an ImageMagic compatible string
|
5
|
+
# @attr_reader [Boolean] is the hexagon cut by the border of the picture
|
6
|
+
# @attr_reader [Object] your data, anything you want
|
7
|
+
#
|
1
8
|
class BaseHex
|
2
|
-
attr_accessor :color, :border, :data
|
9
|
+
attr_accessor :color, :border, :data
|
3
10
|
|
4
11
|
def initialize( color = nil, border = nil, data = nil )
|
5
12
|
@color = color if color
|
6
|
-
@border =
|
13
|
+
@border = border if border
|
7
14
|
@data = data
|
8
15
|
end
|
9
16
|
end
|
data/lib/hex/cube_hex.rb
CHANGED
@@ -6,14 +6,21 @@ require_relative 'base_hex'
|
|
6
6
|
# to understand what a cube coordinates system is
|
7
7
|
# The cube class is only for computation.
|
8
8
|
# It is not intended to be used directly in your program.
|
9
|
+
#
|
10
|
+
# @attr_reader [Integer] x the x coordinate of the cube representation of the hexagon
|
11
|
+
# @attr_reader [Integer] y the y coordinate of the cube representation of the hexagon
|
12
|
+
# @attr_reader [Integer] z the z coordinate of the cube representation of the hexagon
|
13
|
+
#
|
9
14
|
class CubeHex < BaseHex
|
10
15
|
|
11
|
-
attr_reader :x,:y,:z
|
16
|
+
attr_reader :x,:y,:z
|
12
17
|
|
13
18
|
# Create an hexagon object
|
14
|
-
# - +x+, +y+, +z+ are the coordinates in the axial coords system
|
15
19
|
#
|
16
|
-
#
|
20
|
+
# @param x [Integer] x coordinate
|
21
|
+
# @param y [Integer] y coordinate
|
22
|
+
# @param z [Integer] z coordinate
|
23
|
+
#
|
17
24
|
def initialize( x, y, z, color: nil, border: nil, data: nil )
|
18
25
|
@x = x
|
19
26
|
@y = y
|
@@ -24,14 +31,16 @@ class CubeHex < BaseHex
|
|
24
31
|
|
25
32
|
# Transform a cube represented hexagon to an Hexagon::Axial represented hexagon
|
26
33
|
#
|
27
|
-
#
|
34
|
+
# @return [AxialHex] a new AxialHex object
|
35
|
+
#
|
28
36
|
def to_axial
|
29
37
|
AxialHex.new(@x, @z, color: @color, border: @border, data: @data)
|
30
38
|
end
|
31
39
|
|
32
|
-
# Round the float coordinates to integer coordinates
|
40
|
+
# Round the float coordinates to integer coordinates
|
41
|
+
#
|
42
|
+
# @return [AxialCube] a new CubeHex object
|
33
43
|
#
|
34
|
-
# *Returns* : a new Hex::Cube object.
|
35
44
|
def round
|
36
45
|
rx=@x.round(0)
|
37
46
|
ry=@y.round(0)
|
@@ -53,7 +62,8 @@ class CubeHex < BaseHex
|
|
53
62
|
|
54
63
|
# Compute the distance between two hexagons (in hexagons)
|
55
64
|
#
|
56
|
-
#
|
65
|
+
# @return [Ingteger] the distance between hex in hexagons
|
66
|
+
#
|
57
67
|
def distance(h)
|
58
68
|
[(@x - h.x).abs, (@y - h.y).abs, (@z - h.z).abs].max
|
59
69
|
end
|
@@ -1,8 +1,12 @@
|
|
1
1
|
# This module contains the methods relatives to ascii map reading
|
2
2
|
module AsciiToGrid
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
4
|
+
# Read an ascii file and load it into the hexagon grid.
|
5
|
+
#
|
6
|
+
# @param file_path [String] the name of the ascii file to read. For how to create this file, please see : https://github.com/czuger/rhex#reading-a-grid-from-an-ascii-file
|
7
|
+
#
|
8
|
+
# @see https://github.com/czuger/rhex#reading-a-grid-from-an-ascii-file
|
9
|
+
#
|
6
10
|
def read_ascii_file( file_path )
|
7
11
|
File.open( file_path ) do |file|
|
8
12
|
|
@@ -12,9 +16,11 @@ module AsciiToGrid
|
|
12
16
|
elements = line.split
|
13
17
|
q = 0
|
14
18
|
elements.each do |element|
|
19
|
+
# puts "r = #{r} q = #{q}, test = #{( r == 0 || q == 0 )}"
|
15
20
|
border = true if ( r == 0 || q == 0 )
|
16
|
-
shifted_q = q - ( r/2 )
|
17
|
-
|
21
|
+
# shifted_q = q - ( r/2 )
|
22
|
+
shifted_q = q
|
23
|
+
cset( shifted_q, r, color: element.to_sym, border: border )
|
18
24
|
q += 1
|
19
25
|
end
|
20
26
|
r += 1
|
@@ -28,7 +34,6 @@ module AsciiToGrid
|
|
28
34
|
end
|
29
35
|
|
30
36
|
@hexes.each{ |key, e| e.border = true if e.r == ( max_r - 1 ) }
|
31
|
-
|
32
37
|
end
|
33
38
|
end
|
34
39
|
|
@@ -7,17 +7,15 @@ rescue Gem::LoadError
|
|
7
7
|
puts 'Caution : Rmagick is not installed'
|
8
8
|
end
|
9
9
|
|
10
|
-
# This module contain methods to draw a grid to a picture file.
|
11
|
-
# It is included in Grid.
|
10
|
+
# This module contain the methods to draw a grid to a picture file.
|
12
11
|
module GridToPic
|
13
12
|
|
14
|
-
attr_reader :hex_height, :hex_width #:nodoc:
|
15
|
-
attr_reader :quarter_height, :half_width #:nodoc:
|
16
|
-
|
17
13
|
# Draw the hex grid in a Magick::Image object
|
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
14
|
#
|
20
|
-
#
|
15
|
+
# @param exit_on_error [Boolean] 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.
|
16
|
+
#
|
17
|
+
# @return [Magick::Image] a Magick::Image object
|
18
|
+
#
|
21
19
|
def to_rmagick_image( exit_on_error = true )
|
22
20
|
unless defined?( Magick::Image ) && defined?( Magick::HatchFill ) && defined?( Magick::Draw )
|
23
21
|
puts 'Rmagick is not installed !!! You can\'t dump hex grid to pic'
|
@@ -45,10 +43,12 @@ module GridToPic
|
|
45
43
|
end
|
46
44
|
|
47
45
|
# 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
46
|
#
|
51
|
-
#
|
47
|
+
# @param exit_on_error [Boolean] 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
|
48
|
+
# @param pic_name [String] the name of the picture file (can be *.bmp, *.png, *.jpg)
|
49
|
+
#
|
50
|
+
# @return [Boolean] true if the file was created successfully, false otherwise
|
51
|
+
#
|
52
52
|
def to_pic( pic_name, exit_on_error = true )
|
53
53
|
canvas = to_rmagick_image( exit_on_error )
|
54
54
|
canvas.write( pic_name )
|
@@ -58,6 +58,8 @@ module GridToPic
|
|
58
58
|
|
59
59
|
def set_hex_dimensions
|
60
60
|
|
61
|
+
# p :call
|
62
|
+
|
61
63
|
@hex_height = @hex_ray * 2.0
|
62
64
|
@hex_width = Math.sqrt(3)/2.0 * @hex_height
|
63
65
|
|
@@ -74,8 +76,8 @@ module GridToPic
|
|
74
76
|
def draw_hex( gc, hex )
|
75
77
|
x, y = to_xy( hex )
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
+
# p hex
|
80
|
+
# puts [ x, y ]
|
79
81
|
|
80
82
|
color = get_color( hex )
|
81
83
|
gc.fill( color.to_s )
|
data/lib/hex/square_grid.rb
CHANGED
@@ -1,12 +1,55 @@
|
|
1
1
|
require_relative 'axial_grid'
|
2
2
|
require_relative 'cube_hex'
|
3
3
|
|
4
|
+
# This class represents a grid of hexagons stored in an axial coordinate system but manage the conversion to a square representation (what finally you want)
|
5
|
+
#
|
6
|
+
# @author Cédric ZUGER
|
7
|
+
#
|
4
8
|
class SquareGrid < AxialGrid
|
5
9
|
|
10
|
+
include AsciiToGrid
|
11
|
+
include GridToPic
|
12
|
+
|
13
|
+
# Create an axial hexagon grid
|
14
|
+
#
|
15
|
+
# @param hex_ray [Integer] the size of an hexagon.
|
16
|
+
# @param element_to_color_hash [Hash] a hash that relate color (see BaseHex::Axial.new) to a color. This is used to dump your grid to a bitmap field
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# @g = Hex::Grid.new(
|
20
|
+
# element_to_color_hash: {
|
21
|
+
# m: :brown, g: :green, w: :blue
|
22
|
+
# }
|
23
|
+
# )
|
24
|
+
# Assuming you want all hex with a color of m are drawn in brown,g in green, etc ... (see GridToPic for drawing a grid)
|
25
|
+
#
|
26
|
+
def initialize( hex_ray: 16, element_to_color_hash: {} )
|
27
|
+
super( hex_ray: hex_ray )
|
28
|
+
@element_to_color_hash = element_to_color_hash
|
29
|
+
set_hex_dimensions
|
30
|
+
end
|
31
|
+
|
32
|
+
# Create an hexagon at a given position (col, row)
|
33
|
+
#
|
34
|
+
# @param col [Integer] the col coordinate of the hexagon
|
35
|
+
# @param row [Integer] the row coordinate of the hexagon
|
36
|
+
# @param color [String] a colorstring that can be used by ImageMagic
|
37
|
+
# @param border [Boolean] is the hex on the border of the screen (not fully draw)
|
38
|
+
# @param data [Unknown] some data associated with the hexagone. Everything you want, it is up to you
|
39
|
+
#
|
40
|
+
# @return [AxialHex] an hexagon
|
41
|
+
#
|
6
42
|
def cset( col, row, color: nil, border: false, data: nil )
|
7
43
|
hset( even_q_to_axial_hex( col, row, color: color, border: border, data: data ) )
|
8
44
|
end
|
9
45
|
|
46
|
+
# Get the hexagon at a given position (col, row)
|
47
|
+
#
|
48
|
+
# @param col [Integer] the col coordinate of the hexagon
|
49
|
+
# @param row [Integer] the row coordinate of the hexagon
|
50
|
+
#
|
51
|
+
# @return [AxialHex] the hexagon at the requested position. nil if nothing
|
52
|
+
#
|
10
53
|
def cget( col, row )
|
11
54
|
hget( even_q_to_axial_hex( col, row ) )
|
12
55
|
end
|