rhex 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ac3d4d8d088a7768944a17333a84e919cccbe90
4
- data.tar.gz: efe7f5882be4b63ab5d82f6a8c04c43412914700
3
+ metadata.gz: 6deb19b3fa7658e30adc918242cfd55c6c96a831
4
+ data.tar.gz: e9c1ac59f2faec0c63ba0515d45552964d4eb114
5
5
  SHA512:
6
- metadata.gz: e810250b0a6685019323ca1b531650a388ea24c78669709bda722b806f9842523d8919ceafa1512c8d48fde754221433832c8116e9c088aeaaa95707d0a4c83e
7
- data.tar.gz: d08dc31236178532f633264c7963e717d8ec381f90b36ebc1a350451864c397cc75ae1377cf1b35c4a3efa3bc82c7eed3798556fb7fc37b375befcf3851c64df
6
+ metadata.gz: 98ea9582bb07c5312714d37f0e36be129eca200e1c538d8a9a728adafee915dc64209bcc26d708576fd01f0cae17f6a3927e870bf71c1ecde81b9f1500bbb746
7
+ data.tar.gz: c7a8f7d08d20ab059c16f553b72a0b72784e57d7509f89f9d509518d96455cef935a9877188d8952b0f76416adcb9c4c69b6b98d8463dc38cf2e75609cfe55d7
@@ -0,0 +1,18 @@
1
+ module AsciiToGrid
2
+
3
+ def read_ascii_file( file_path )
4
+ File.open( file_path ) do |file|
5
+ r = 0
6
+ file.each_line do |line|
7
+ elements = line.split
8
+ q = 0
9
+ elements.each do |element|
10
+ set( q, r, element )
11
+ q += 1
12
+ end
13
+ r += 1
14
+ end
15
+ end
16
+ end
17
+
18
+ end
data/lib/hex/axial.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  # -*- encoding : utf-8 -*-
2
- require 'hex/cube'
2
+
3
+ require_relative 'cube'
3
4
 
4
5
  module Hex
5
6
  class Axial
6
7
 
7
- attr_reader :q, :r
8
+ attr_reader :q, :r, :val
8
9
 
9
10
  R = 16
10
11
 
@@ -12,12 +13,13 @@ module Hex
12
13
  DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ]
13
14
 
14
15
  # Create a new flat topped axial represented hexagon object
15
- def initialize(q,r)
16
+ def initialize( q, r, val = nil)
16
17
  @q = q
17
18
  @r = r
19
+ @val = val.to_sym if val
18
20
  end
19
21
 
20
- # Equality between two hexagones
22
+ # Equality between two hexagons
21
23
  def ==(h)
22
24
  @q==h.q && @r==h.r
23
25
  end
@@ -33,8 +35,9 @@ module Hex
33
35
 
34
36
  # Give the position of an hexagone object in pixel
35
37
  def to_xy
38
+ pix_r = r - ( @q/2 )
36
39
  x = R * 3/2 * @q
37
- y = ( ( R.to_f * Math.sqrt(3) ).round * (@r.to_f + @q.to_f/2.to_f) )
40
+ y = ( ( R.to_f * Math.sqrt(3) ).round * (pix_r.to_f + @q.to_f/2.to_f) )
38
41
  [ x, y ]
39
42
  end
40
43
 
data/lib/hex/grid.rb CHANGED
@@ -1,9 +1,16 @@
1
- require 'hex/axial'
1
+ require_relative 'axial'
2
+ require_relative 'grid_to_pic'
3
+ require_relative 'ascii_to_grid'
2
4
 
3
5
  module Hex
4
6
  class Grid
5
- def initialize
7
+
8
+ include GridToPic
9
+ include AsciiToGrid
10
+
11
+ def initialize( element_to_color_hash = {} )
6
12
  @hexes={}
13
+ @element_to_color_hash = element_to_color_hash
7
14
  end
8
15
 
9
16
  # Position aa value at hexagon coordinate
@@ -14,18 +21,17 @@ module Hex
14
21
  raise ArgumentError, 'Incorrect number of arguments' if args.length < 1 || args.length > 3
15
22
  fa=args.shift
16
23
  if fa.class == Hex::Axial
17
- @hexes[ [ fa.q, fa.r ] ] = args.shift
24
+ @hexes[ [ fa.q, fa.r ] ] = Hex::Axial.new( fa.q, fa.r, args.shift )
18
25
  else
19
26
  q=fa
20
27
  r=args.shift
21
- @hexes[ [ q, r ] ] = args.shift
28
+ @hexes[ [ q, r ] ] = Hex::Axial.new( q, r, args.shift )
22
29
  end
23
30
  end
24
31
 
25
32
  # Get the value of stored at hexagon coordinate
26
- # You can use the form set( q, r, value )
27
- # Or set( hex, value )
28
- # In both case value can be nil.
33
+ # You can use the form get( q, r )
34
+ # Or get( hex )
29
35
  def get( *args )
30
36
  raise( ArgumentError, 'Incorrect number of arguments' ) if args.length < 1 || args.length > 2
31
37
  fa=args.shift
@@ -0,0 +1,46 @@
1
+ # A module to transform hex grid to bitmap
2
+
3
+ begin
4
+ gem 'rmagick'
5
+ require 'rmagick'
6
+ rescue Gem::LoadError
7
+ puts 'Caution : Rmagick is not installed'
8
+ end
9
+
10
+ module GridToPic
11
+
12
+ def to_pic( pic_name, exit_on_error = true )
13
+ unless defined?( Magick::Image ) && defined?( Magick::HatchFill ) && defined?( Magick::Draw )
14
+ puts 'Rmagick is not installed !!! You can\'t dump hex grid to pic'
15
+ exit if exit_on_error
16
+ end
17
+
18
+ canvas = Magick::Image.new( 500, 500 )
19
+ gc = Magick::Draw.new
20
+ width = Hex::Axial::R * 2
21
+ quarter_width = width / 4.0
22
+ height = Math.sqrt(3)/2 * width
23
+ half_height = height / 2.0
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
34
+
35
+ gc.draw(canvas)
36
+ canvas.write( pic_name )
37
+ end
38
+
39
+ private
40
+
41
+ def get_color( hex )
42
+ color = @element_to_color_hash[ hex.val ]
43
+ color ? color : :white
44
+ end
45
+
46
+ end
metadata CHANGED
@@ -1,24 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhex
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.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-06-23 00:00:00.000000000 Z
11
+ date: 2015-12-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A library designed to provide hexagonal grids for ruby
14
- email: zuger.cedric@gmail.com
14
+ email:
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - lib/hex/ascii_to_grid.rb
19
20
  - lib/hex/axial.rb
20
21
  - lib/hex/cube.rb
21
22
  - lib/hex/grid.rb
23
+ - lib/hex/grid_to_pic.rb
22
24
  - lib/rhex.rb
23
25
  homepage: http://rubygems.org/gems/rhex
24
26
  licenses:
@@ -40,8 +42,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
42
  version: '0'
41
43
  requirements: []
42
44
  rubyforge_project:
43
- rubygems_version: 2.4.1
45
+ rubygems_version: 2.4.8
44
46
  signing_key:
45
47
  specification_version: 4
46
48
  summary: Ruby HEXagonal grid
47
49
  test_files: []
50
+ has_rdoc: