rhex 1.0.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 +7 -0
- data/lib/hex/axial.rb +88 -0
- data/lib/hex/cube.rb +42 -0
- data/lib/hex/grid.rb +42 -0
- data/lib/rhex.rb +3 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4ac3d4d8d088a7768944a17333a84e919cccbe90
|
4
|
+
data.tar.gz: efe7f5882be4b63ab5d82f6a8c04c43412914700
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e810250b0a6685019323ca1b531650a388ea24c78669709bda722b806f9842523d8919ceafa1512c8d48fde754221433832c8116e9c088aeaaa95707d0a4c83e
|
7
|
+
data.tar.gz: d08dc31236178532f633264c7963e717d8ec381f90b36ebc1a350451864c397cc75ae1377cf1b35c4a3efa3bc82c7eed3798556fb7fc37b375befcf3851c64df
|
data/lib/hex/axial.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'hex/cube'
|
3
|
+
|
4
|
+
module Hex
|
5
|
+
class Axial
|
6
|
+
|
7
|
+
attr_reader :q, :r
|
8
|
+
|
9
|
+
R = 16
|
10
|
+
|
11
|
+
# Directions around hex from top left clockwise
|
12
|
+
DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ]
|
13
|
+
|
14
|
+
# Create a new flat topped axial represented hexagon object
|
15
|
+
def initialize(q,r)
|
16
|
+
@q = q
|
17
|
+
@r = r
|
18
|
+
end
|
19
|
+
|
20
|
+
# Equality between two hexagones
|
21
|
+
def ==(h)
|
22
|
+
@q==h.q && @r==h.r
|
23
|
+
end
|
24
|
+
|
25
|
+
# Create an hexagon object from (x,y) coordinate
|
26
|
+
def self.hex_at_xy(x, y)
|
27
|
+
x-=R
|
28
|
+
y-=R
|
29
|
+
q = x * 2/3 / R
|
30
|
+
r = (-x / 3 + Math.sqrt(3)/3 * y) / R
|
31
|
+
Hex::Axial.new(q, r).round
|
32
|
+
end
|
33
|
+
|
34
|
+
# Give the position of an hexagone object in pixel
|
35
|
+
def to_xy
|
36
|
+
x = R * 3/2 * @q
|
37
|
+
y = ( ( R.to_f * Math.sqrt(3) ).round * (@r.to_f + @q.to_f/2.to_f) )
|
38
|
+
[ x, y ]
|
39
|
+
end
|
40
|
+
|
41
|
+
# Transform flat topped axial represented hexagon object to flat topped cube represented hexagon object
|
42
|
+
def to_cube
|
43
|
+
Hex::Cube.new(@q,-@q-@r,@r)
|
44
|
+
end
|
45
|
+
|
46
|
+
# From an array of hexagones, get the nearest
|
47
|
+
def nearest_hex( hex_array )
|
48
|
+
nearest_hex = nil
|
49
|
+
current_distance = nil
|
50
|
+
hex_array.each do |h|
|
51
|
+
unless nearest_hex
|
52
|
+
nearest_hex = h
|
53
|
+
current_distance = distance( h )
|
54
|
+
else
|
55
|
+
nearest_hex = h if distance( h ) < current_distance
|
56
|
+
end
|
57
|
+
end
|
58
|
+
nearest_hex
|
59
|
+
end
|
60
|
+
|
61
|
+
# Get the distance (in hex) between two hexagons
|
62
|
+
def distance( h )
|
63
|
+
to_cube.distance(h.to_cube)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Get all hexagons surrounding the current hexagon
|
67
|
+
def get_surrounding_hexs
|
68
|
+
# puts self.inspect, self.q.inspect, self.r.inspect
|
69
|
+
DIRECTIONS.map{ |e| Hex::Axial.new( @q+e[0], @r+e[1] ) }
|
70
|
+
end
|
71
|
+
|
72
|
+
# Check if an hexagon is around another exagon
|
73
|
+
def hex_surrounding_hex?(hex)
|
74
|
+
distance(hex)==1
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
# Round an hexagone coordinates (useful after pixel to axial coordinate transformation)
|
80
|
+
def round
|
81
|
+
to_cube.round.to_axial
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
data/lib/hex/cube.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module Hex
|
2
|
+
class Cube
|
3
|
+
|
4
|
+
attr_reader :x,:y,:z
|
5
|
+
|
6
|
+
def initialize(x,y,z)
|
7
|
+
@x = x
|
8
|
+
@y = y
|
9
|
+
@z = z
|
10
|
+
end
|
11
|
+
|
12
|
+
# Transform a cube represented hexagon to an Hexagon::Axial represented hexagon
|
13
|
+
def to_axial
|
14
|
+
Hex::Axial.new(@x,@z)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Round an float coordonate hexagon to an integer hexagon
|
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
|
+
|
37
|
+
def distance(h)
|
38
|
+
[(@x - h.x).abs, (@y - h.y).abs, (@z - h.z).abs].max
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/lib/hex/grid.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'hex/axial'
|
2
|
+
|
3
|
+
module Hex
|
4
|
+
class Grid
|
5
|
+
def initialize
|
6
|
+
@hexes={}
|
7
|
+
end
|
8
|
+
|
9
|
+
# Position aa value at hexagon coordinate
|
10
|
+
# You can use the form set( q, r, value )
|
11
|
+
# Or set( hex, value )
|
12
|
+
# In both case value can be nil.
|
13
|
+
def set( *args )
|
14
|
+
raise ArgumentError, 'Incorrect number of arguments' if args.length < 1 || args.length > 3
|
15
|
+
fa=args.shift
|
16
|
+
if fa.class == Hex::Axial
|
17
|
+
@hexes[ [ fa.q, fa.r ] ] = args.shift
|
18
|
+
else
|
19
|
+
q=fa
|
20
|
+
r=args.shift
|
21
|
+
@hexes[ [ q, r ] ] = args.shift
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# 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.
|
29
|
+
def get( *args )
|
30
|
+
raise( ArgumentError, 'Incorrect number of arguments' ) if args.length < 1 || args.length > 2
|
31
|
+
fa=args.shift
|
32
|
+
if fa.class == Hex::Axial
|
33
|
+
@hexes[ [ fa.q, fa.r ] ]
|
34
|
+
else
|
35
|
+
q=fa
|
36
|
+
r=args.shift
|
37
|
+
@hexes[ [ q, r ] ]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/lib/rhex.rb
ADDED
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rhex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cédric ZUGER
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A library designed to provide hexagonal grids for ruby
|
14
|
+
email: zuger.cedric@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/hex/axial.rb
|
20
|
+
- lib/hex/cube.rb
|
21
|
+
- lib/hex/grid.rb
|
22
|
+
- lib/rhex.rb
|
23
|
+
homepage: http://rubygems.org/gems/rhex
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.4.1
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Ruby HEXagonal grid
|
47
|
+
test_files: []
|