rhex 1.1.0 → 1.2.0
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/ascii_to_grid.rb +17 -3
- data/lib/hex/axial.rb +14 -24
- data/lib/hex/cube.rb +18 -18
- data/lib/hex/grid.rb +40 -28
- data/lib/hex/grid_to_pic.rb +34 -15
- data/lib/rhex.rb +3 -3
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06af15a3d397116a537acb467bbc2689f637c08c
|
4
|
+
data.tar.gz: aac1063765276f0585e0cde65a53ae9fcc9c3b6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3b945daad5e4b6a0398effd1d4d14d817789725a27cd0b1b121da3b2677500035e91bd294715b79c56bd97c3f2dc7a3a5359eff64f91a27ebd779c15425f889
|
7
|
+
data.tar.gz: c9f595a299135cdbada16d8e9329e90bfb37ef1fb5e5f821a956b328ecebb8daec97c3ec5f8568c64bbc010d1dc93fabcc1f91d21a412bfb1c7bb1112ef9c187
|
data/lib/hex/ascii_to_grid.rb
CHANGED
@@ -2,17 +2,31 @@ module AsciiToGrid
|
|
2
2
|
|
3
3
|
def read_ascii_file( file_path )
|
4
4
|
File.open( file_path ) do |file|
|
5
|
-
|
5
|
+
|
6
|
+
r = max_q = max_r = 0
|
6
7
|
file.each_line do |line|
|
7
8
|
elements = line.split
|
8
9
|
q = 0
|
9
10
|
elements.each do |element|
|
10
|
-
|
11
|
+
border = true if ( r == 0 || q == 0 )
|
12
|
+
cset( q, r, val: element, border: border )
|
11
13
|
q += 1
|
14
|
+
max_q = [ max_q, q ].max
|
12
15
|
end
|
13
16
|
r += 1
|
17
|
+
max_r = [ max_r, r ].max
|
18
|
+
end
|
19
|
+
|
20
|
+
0.upto( max_q - 1 ).each do |q|
|
21
|
+
hex = cget( q, max_r - 1 )
|
22
|
+
hex.border! if hex
|
14
23
|
end
|
24
|
+
|
25
|
+
0.upto( max_r - 1 ).each do |row|
|
26
|
+
hex = cget( max_q - 1, row )
|
27
|
+
hex.border! if hex
|
28
|
+
end
|
29
|
+
|
15
30
|
end
|
16
31
|
end
|
17
|
-
|
18
32
|
end
|
data/lib/hex/axial.rb
CHANGED
@@ -7,16 +7,15 @@ module Hex
|
|
7
7
|
|
8
8
|
attr_reader :q, :r, :val
|
9
9
|
|
10
|
-
R = 16
|
11
|
-
|
12
10
|
# Directions around hex from top left clockwise
|
13
11
|
DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ]
|
14
12
|
|
15
|
-
# Create a new
|
16
|
-
def initialize( q, r, val
|
13
|
+
# Create a new pointy topped axial represented hexagon object
|
14
|
+
def initialize( q, r, val: nil, border: false )
|
17
15
|
@q = q
|
18
16
|
@r = r
|
19
17
|
@val = val.to_sym if val
|
18
|
+
@border = border
|
20
19
|
end
|
21
20
|
|
22
21
|
# Equality between two hexagons
|
@@ -24,21 +23,14 @@ module Hex
|
|
24
23
|
@q==h.q && @r==h.r
|
25
24
|
end
|
26
25
|
|
27
|
-
#
|
28
|
-
def
|
29
|
-
|
30
|
-
y-=R
|
31
|
-
q = x * 2/3 / R
|
32
|
-
r = (-x / 3 + Math.sqrt(3)/3 * y) / R
|
33
|
-
Hex::Axial.new(q, r).round
|
26
|
+
# Force the border status of the hex
|
27
|
+
def border!
|
28
|
+
@border = true
|
34
29
|
end
|
35
30
|
|
36
|
-
#
|
37
|
-
def
|
38
|
-
|
39
|
-
x = R * 3/2 * @q
|
40
|
-
y = ( ( R.to_f * Math.sqrt(3) ).round * (pix_r.to_f + @q.to_f/2.to_f) )
|
41
|
-
[ x, y ]
|
31
|
+
# Get the border status of the hex
|
32
|
+
def border?
|
33
|
+
!@border.nil?
|
42
34
|
end
|
43
35
|
|
44
36
|
# Transform flat topped axial represented hexagon object to flat topped cube represented hexagon object
|
@@ -72,17 +64,15 @@ module Hex
|
|
72
64
|
DIRECTIONS.map{ |e| Hex::Axial.new( @q+e[0], @r+e[1] ) }
|
73
65
|
end
|
74
66
|
|
75
|
-
# Check if an hexagon is around another
|
67
|
+
# Check if an hexagon is around another hexagon
|
76
68
|
def hex_surrounding_hex?(hex)
|
77
69
|
distance(hex)==1
|
78
70
|
end
|
79
71
|
|
80
|
-
|
81
|
-
|
82
|
-
#
|
83
|
-
|
84
|
-
to_cube.round.to_axial
|
85
|
-
end
|
72
|
+
# Round an hexagon coordinates (useful after pixel to axial coordinate transformation)
|
73
|
+
# def round
|
74
|
+
# to_cube.round.to_axial
|
75
|
+
# end
|
86
76
|
|
87
77
|
end
|
88
78
|
end
|
data/lib/hex/cube.rb
CHANGED
@@ -15,24 +15,24 @@ module Hex
|
|
15
15
|
end
|
16
16
|
|
17
17
|
# Round an float coordonate hexagon to an integer hexagon
|
18
|
-
def round
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
18
|
+
# def round
|
19
|
+
# rx=@x.round(0)
|
20
|
+
# ry=@y.round(0)
|
21
|
+
# rz=@z.round(0)
|
22
|
+
#
|
23
|
+
# x_diff=(rx-@x).abs
|
24
|
+
# y_diff=(ry-@y).abs
|
25
|
+
# z_diff=(rz-@z).abs
|
26
|
+
#
|
27
|
+
# if x_diff > y_diff and x_diff > z_diff
|
28
|
+
# rx = -ry-rz
|
29
|
+
# elsif y_diff > z_diff
|
30
|
+
# ry = -rx-rz
|
31
|
+
# else
|
32
|
+
# rz = -rx-ry
|
33
|
+
# end
|
34
|
+
# Hex::Cube.new(rx,ry,rz)
|
35
|
+
# end
|
36
36
|
|
37
37
|
def distance(h)
|
38
38
|
[(@x - h.x).abs, (@y - h.y).abs, (@z - h.z).abs].max
|
data/lib/hex/grid.rb
CHANGED
@@ -8,40 +8,52 @@ module Hex
|
|
8
8
|
include GridToPic
|
9
9
|
include AsciiToGrid
|
10
10
|
|
11
|
-
def initialize( element_to_color_hash
|
11
|
+
def initialize( hex_ray: 16, element_to_color_hash: {} )
|
12
12
|
@hexes={}
|
13
13
|
@element_to_color_hash = element_to_color_hash
|
14
|
+
@hex_ray = hex_ray
|
15
|
+
set_hex_dimensions
|
14
16
|
end
|
15
17
|
|
16
|
-
#
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
def set( *args )
|
21
|
-
raise ArgumentError, 'Incorrect number of arguments' if args.length < 1 || args.length > 3
|
22
|
-
fa=args.shift
|
23
|
-
if fa.class == Hex::Axial
|
24
|
-
@hexes[ [ fa.q, fa.r ] ] = Hex::Axial.new( fa.q, fa.r, args.shift )
|
25
|
-
else
|
26
|
-
q=fa
|
27
|
-
r=args.shift
|
28
|
-
@hexes[ [ q, r ] ] = Hex::Axial.new( q, r, args.shift )
|
29
|
-
end
|
18
|
+
# Create an hexagon at a given position (q, r)
|
19
|
+
# You can set a value for the hexagon and set the hex as a border hex or not
|
20
|
+
def cset( q, r, val: nil, border: false )
|
21
|
+
@hexes[ [ q, r ] ] = Hex::Axial.new( q, r, val: val, border: border )
|
30
22
|
end
|
31
23
|
|
32
|
-
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
24
|
+
# Same method, but accept an hexagon instead of (q, r) coords
|
25
|
+
def hset( hex, val: nil, border: false )
|
26
|
+
@hexes[ [ hex.q, hex.r ] ] = Hex::Axial.new( hex.q, hex.r, val: val, border: border )
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get the hexagon at a given position (q, r)
|
30
|
+
def cget( q, r )
|
31
|
+
@hexes[ [ q, r ] ]
|
32
|
+
end
|
33
|
+
|
34
|
+
# Same method, but accept an hexagon instead of (q, r) coords
|
35
|
+
def hget( hex )
|
36
|
+
@hexes[ [ hex.q, hex.r ] ]
|
37
|
+
end
|
38
|
+
|
39
|
+
# Create an hexagon object from (x,y) coordinate
|
40
|
+
# q = (x * sqrt(3)/3 - y / 3) / size
|
41
|
+
# r = y * 2/3 / size
|
42
|
+
# return hex_round(Hex(q, r))
|
43
|
+
# def hex_at_xy(x, y)
|
44
|
+
# # x-=@hex_width/2.0
|
45
|
+
# # y-=@hex_height/2.0
|
46
|
+
# q = (x * Math.sqrt(3)/3.0 - y/3.0) / @hex_ray
|
47
|
+
# r = y * 2.0/3.0 / @hex_ray
|
48
|
+
# Hex::Axial.new(q, r).round
|
49
|
+
# end
|
50
|
+
|
51
|
+
# Give the position of an hexagon object in pixel.
|
52
|
+
def to_xy( hex )
|
53
|
+
tmp_q = hex.q - ( hex.r/2.0 ).floor
|
54
|
+
x = @hex_ray * Math.sqrt(3) * ( tmp_q + hex.r/2.0 )
|
55
|
+
y = @hex_ray * 3.0/2.0 * hex.r
|
56
|
+
[ x, y ]
|
45
57
|
end
|
46
58
|
|
47
59
|
end
|
data/lib/hex/grid_to_pic.rb
CHANGED
@@ -9,33 +9,38 @@ end
|
|
9
9
|
|
10
10
|
module GridToPic
|
11
11
|
|
12
|
+
attr_reader :hex_height, :hex_width
|
13
|
+
|
12
14
|
def to_pic( pic_name, exit_on_error = true )
|
13
15
|
unless defined?( Magick::Image ) && defined?( Magick::HatchFill ) && defined?( Magick::Draw )
|
14
16
|
puts 'Rmagick is not installed !!! You can\'t dump hex grid to pic'
|
15
17
|
exit if exit_on_error
|
16
18
|
end
|
17
19
|
|
18
|
-
|
20
|
+
maxx = @hexes.keys.map{ |k| k[0] }.max * @hex_width
|
21
|
+
maxy = @hexes.keys.map{ |k| k[1] }.max * @hex_height * 3.0/4.0
|
22
|
+
|
23
|
+
canvas = Magick::Image.new( maxx, maxy )
|
19
24
|
gc = Magick::Draw.new
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@hexes.each do |pos, hex|
|
26
|
-
x, y = hex.to_xy
|
27
|
-
|
28
|
-
color = get_color( hex )
|
29
|
-
gc.stroke( 'black' )
|
30
|
-
gc.fill( color.to_s )
|
31
|
-
gc.polygon( x-quarter_width, y-half_height, x+quarter_width, y-half_height, x+quarter_width*2, y,
|
32
|
-
x+quarter_width, y+half_height, x-quarter_width, y+half_height, x-quarter_width*2, y )
|
33
|
-
end
|
25
|
+
gc.stroke_antialias( true )
|
26
|
+
gc.stroke( 'black' )
|
27
|
+
|
28
|
+
@hexes.each{ | _, hex| draw_hex( gc, hex ) }
|
34
29
|
|
35
30
|
gc.draw(canvas)
|
36
31
|
canvas.write( pic_name )
|
37
32
|
end
|
38
33
|
|
34
|
+
def set_hex_dimensions
|
35
|
+
|
36
|
+
@hex_height = @hex_ray * 2.0
|
37
|
+
@hex_width = Math.sqrt(3)/2.0 * @hex_height
|
38
|
+
|
39
|
+
@half_width = @hex_width / 2.0
|
40
|
+
@quarter_height = @hex_height / 4.0
|
41
|
+
|
42
|
+
end
|
43
|
+
|
39
44
|
private
|
40
45
|
|
41
46
|
def get_color( hex )
|
@@ -43,4 +48,18 @@ module GridToPic
|
|
43
48
|
color ? color : :white
|
44
49
|
end
|
45
50
|
|
51
|
+
def draw_hex( gc, hex )
|
52
|
+
x, y = to_xy( hex )
|
53
|
+
|
54
|
+
color = get_color( hex )
|
55
|
+
gc.fill( color.to_s )
|
56
|
+
|
57
|
+
gc.polygon( x - @half_width, y + @quarter_height,
|
58
|
+
x, y + 2*@quarter_height,
|
59
|
+
x + @half_width, y + @quarter_height,
|
60
|
+
x + @half_width, y - @quarter_height,
|
61
|
+
x, y - 2*@quarter_height,
|
62
|
+
x - @half_width, y - @quarter_height )
|
63
|
+
end
|
64
|
+
|
46
65
|
end
|
data/lib/rhex.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require_relative 'hex/axial'
|
2
|
+
require_relative 'hex/cube'
|
3
|
+
require_relative 'hex/grid'
|
metadata
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.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: 2015-12-
|
11
|
+
date: 2015-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: |2
|
14
|
+
A library designed to provide hexagonal grids for ruby.
|
15
|
+
- Coordinate system is axial, bitmap representation is pointy topped.
|
16
|
+
- Provide various hex methods (surrounding hexes, nearest hex, distance between two hexes)
|
17
|
+
- Provide a method to create a grid from an ascii map
|
18
|
+
- Provide a method to dump your hex grid to a bitmap file (require rmagick)
|
14
19
|
email:
|
15
20
|
executables: []
|
16
21
|
extensions: []
|
@@ -34,7 +39,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
34
39
|
requirements:
|
35
40
|
- - ">="
|
36
41
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
42
|
+
version: 2.0.0
|
38
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
44
|
requirements:
|
40
45
|
- - ">="
|
@@ -42,9 +47,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
47
|
version: '0'
|
43
48
|
requirements: []
|
44
49
|
rubyforge_project:
|
45
|
-
rubygems_version: 2.4.
|
50
|
+
rubygems_version: 2.4.1
|
46
51
|
signing_key:
|
47
52
|
specification_version: 4
|
48
53
|
summary: Ruby HEXagonal grid
|
49
54
|
test_files: []
|
50
|
-
has_rdoc:
|