Skip to content

Commit

Permalink
六角形における位置を取得するメソッドを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
nayutaya committed Oct 28, 2011
1 parent 7588c25 commit 7d80f01
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 117 deletions.
131 changes: 21 additions & 110 deletions src/ruby/lib/geodelta/hex.rb
Expand Up @@ -7,116 +7,27 @@ module Hex
def self.get_hex_position(ids)
level = ids.size
unit = 12.0 / (2 ** (level - 1))
x, y = GeoDelta::Geometry.get_center(ids)
#x, y = GeoDelta::Geometry.get_coordinates(ids)[1]
upper = GeoDelta::Geometry.upper_delta?(ids)

p({:ids => ids, :x => x, :y => y})

#p [ids, (y / unit).floor]
k = Math.sqrt((0.75 ** 2) + (0.5 ** 2)) * unit
i = Math.cos( Math.atan(6.0 / 9.0)) * x
j = Math.cos(Math::PI - Math.atan(6.0 / 9.0)) * y
xx = (x / (unit / 2)).floor
yy = (y / unit).floor
ii = (i / k).floor
jj = (j / k).floor
#p [ids, (j / k).floor]
#p [ids, [yy, ii, jj]]
#p [ids, [y, i, j]]
#p [[xx, yy, ii, jj], [xx % 2, yy % 2, ii % 2, jj % 2]]
p [xx, yy]

=begin
if true
case [xx % 2, yy % 2]
when [0, 0, 1] then return 0
when [1, 0, 1] then return 1
when [1, 1, 1] then return 2
when [0, 1, 1] then return 3
when [1, 1, 1] then return 4
when [1, 0, 1] then return 5
end
end
=end

case [xx, yy]
when [ 0, 0] then return 0 # center
when [ 0, 2] then return 0 # upper
when [ 0, -2] then return 0 # lower
when [ 3, 1] then return 0 # upper_right
when [ 3, -1] then return 0 # lower_right
when [-3, -1] then return 0 # lower_left
when [-3, 1] then return 0 # upper_left

when [ 1, 0] then return 1 # center
when [ 1, 2] then return 1 # upper
when [ 1, -2] then return 1 # lower
when [ 4, 1] then return 1 # upper_right
when [ 4, -1] then return 1 # lower_right
when [-2, -1] then return 1 # lower_left
when [-2, 1] then return 1 # upper_left

when [ 1, -1] then return 2 # center
when [ 1, 1] then return 2 # upper
when [ 1, -3] then return 2 # lower
when [ 4, 0] then return 2 # upper_right
when [ 4, -2] then return 2 # lower_right
when [-2, -2] then return 2 # lower_left
when [-2, 0] then return 2 # upper_left

when [ 0, -1] then return 3 # center
when [ 0, 1] then return 3 # upper
when [ 0, -3] then return 3 # lower
when [ 3, 0] then return 3 # upper_right
when [ 3, -2] then return 3 # lower_right
when [-3, -2] then return 3 # lower_left
when [-3, 0] then return 3 # upper_left

when [-1, -1] then return 4 # center
when [-1, 1] then return 4 # upper
when [-1, -3] then return 4 # lower
when [ 2, 0] then return 4 # upper_right
when [ 2, -2] then return 4 # lower_right
when [-4, -2] then return 4 # lower_left
when [-4, 0] then return 4 # upper_left

when [-1, 0] then return 5 # center
when [-1, 2] then return 5 # upper
when [-1, -2] then return 5 # lower
when [ 2, 1] then return 5 # upper_right
when [ 2, -1] then return 5 # lower_right
when [-4, -1] then return 5 # lower_left
when [-4, 1] then return 5 # upper_left
end

=begin
p [ids, [x, y], upper]
#p y % unit
k = Math.sqrt((unit ** 2) - ((unit / 2) ** 2))
sx = x
sy = y * (k / unit)
si = Math.sin(Math.atan(2)) * sx
sj = Math.cos(Math.atan(2)) * sy
#p [sx, sy, si, sj]
p [sy % k, si % k, sj % k]
#p [(sy % k) > 1.0, (si % k) > 1.0, (sj % k) > 1.0]
p [(sy % k) > 1.0, (si % k) > 1.0, (sj % k) > 1.0]
puts
#p [i, j]
#p [Math.sin(Math.atan(2)) * 12, Math.cos(Math.atan(2)) * 12]
=end

case ids
when [0, 1, 1] then 0
when [1, 3, 3] then 1
when [5, 2, 2] then 2
when [4, 1, 1] then 3
when [7, 3, 3] then 4
when [3, 2, 2] then 5
else -1
xu = unit / 2.0
yu = unit

x, y = GeoDelta::Geometry.get_center(ids)
i = (x / xu).floor % 6
j = (y / yu).floor % 2

case [i, j]
when [0, 0] then return 0
when [0, 1] then return 3
when [1, 0] then return 1
when [1, 1] then return 2
when [2, 0] then return 4
when [2, 1] then return 5
when [3, 0] then return 3
when [3, 1] then return 0
when [4, 0] then return 2
when [4, 1] then return 1
when [5, 0] then return 5
when [5, 1] then return 4
else raise "BUG [#{i}, #{j}]"
end
end
end
Expand Down
7 changes: 0 additions & 7 deletions src/ruby/test/hex_test.rb
Expand Up @@ -17,7 +17,6 @@ def test_get_hex_position__level3_center
[4, [7, 3, 3]],
[5, [3, 2, 2]],
]
p :center
assert_equal(
expected,
expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
Expand All @@ -32,7 +31,6 @@ def test_get_hex_position__level3_upper
[4, [0, 1, 2]],
[5, [0, 0, 3]],
]
p :upper
assert_equal(
expected,
expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
Expand All @@ -47,7 +45,6 @@ def test_get_hex_position__level3_upper_right
[4, [1, 3, 0]],
[5, [1, 3, 1]],
]
p :upper_right
assert_equal(
expected,
expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
Expand All @@ -62,7 +59,6 @@ def test_get_hex_position__level3_lower_right
[4, [5, 2, 1]],
[5, [5, 2, 0]],
]
p :lower_right
assert_equal(
expected,
expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
Expand All @@ -77,7 +73,6 @@ def test_get_hex_position__level3_lower
[4, [4, 0, 2]],
[5, [4, 1, 3]],
]
p :lower
assert_equal(
expected,
expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
Expand All @@ -92,7 +87,6 @@ def test_get_hex_position__level3_lower_left
[4, [7, 0, 0]],
[5, [7, 0, 1]],
]
p :lower_left
assert_equal(
expected,
expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
Expand All @@ -107,7 +101,6 @@ def test_get_hex_position__level3_upper_left
[4, [3, 0, 1]],
[5, [3, 0, 0]],
]
p :upper_left
assert_equal(
expected,
expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
Expand Down

0 comments on commit 7d80f01

Please sign in to comment.