Permalink
Browse files

Merge branch 'hex'

  • Loading branch information...
nayutaya committed Oct 28, 2011
2 parents 87c9efc + 7d80f01 commit f7596228a1c875fc602197fc0c9c5389b58b065a
Showing with 142 additions and 0 deletions.
  1. +34 −0 src/ruby/lib/geodelta/hex.rb
  2. +108 −0 src/ruby/test/hex_test.rb
@@ -0,0 +1,34 @@
+# encoding: utf-8
+
+require_relative "geometry"
+
+module GeoDelta
+ module Hex
+ def self.get_hex_position(ids)
+ level = ids.size
+ unit = 12.0 / (2 ** (level - 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
+end
View
@@ -0,0 +1,108 @@
+# encoding: utf-8
+
+require "test/unit"
+require_relative "../lib/geodelta/hex"
+
+class GeoDeltaHexTest < Test::Unit::TestCase
+ def setup
+ @mod = GeoDelta::Hex
+ end
+
+ def test_get_hex_position__level3_center
+ expected = [
+ [0, [0, 1, 1]],
+ [1, [1, 3, 3]],
+ [2, [5, 2, 2]],
+ [3, [4, 1, 1]],
+ [4, [7, 3, 3]],
+ [5, [3, 2, 2]],
+ ]
+ assert_equal(
+ expected,
+ expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
+ end
+
+ def test_get_hex_position__level3_upper
+ expected = [
+ [0, [0, 0, 0]],
+ [1, [0, 0, 2]],
+ [2, [0, 1, 3]],
+ [3, [0, 1, 0]],
+ [4, [0, 1, 2]],
+ [5, [0, 0, 3]],
+ ]
+ assert_equal(
+ expected,
+ expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
+ end
+
+ def test_get_hex_position__level3_upper_right
+ expected = [
+ [0, [1, 0, 2]],
+ [1, [1, 0, 0]],
+ [2, [1, 0, 1]],
+ [3, [1, 3, 2]],
+ [4, [1, 3, 0]],
+ [5, [1, 3, 1]],
+ ]
+ assert_equal(
+ expected,
+ expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
+ end
+
+ def test_get_hex_position__level3_lower_right
+ expected = [
+ [0, [5, 2, 3]],
+ [1, [5, 0, 1]],
+ [2, [5, 0, 0]],
+ [3, [5, 0, 3]],
+ [4, [5, 2, 1]],
+ [5, [5, 2, 0]],
+ ]
+ assert_equal(
+ expected,
+ expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
+ end
+
+ def test_get_hex_position__level3_lower
+ expected = [
+ [0, [4, 1, 0]],
+ [1, [4, 1, 2]],
+ [2, [4, 0, 3]],
+ [3, [4, 0, 0]],
+ [4, [4, 0, 2]],
+ [5, [4, 1, 3]],
+ ]
+ assert_equal(
+ expected,
+ expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
+ end
+
+ def test_get_hex_position__level3_lower_left
+ expected = [
+ [0, [7, 3, 2]],
+ [1, [7, 3, 0]],
+ [2, [7, 3, 1]],
+ [3, [7, 0, 2]],
+ [4, [7, 0, 0]],
+ [5, [7, 0, 1]],
+ ]
+ assert_equal(
+ expected,
+ expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
+ end
+
+ def test_get_hex_position__level3_upper_left
+ expected = [
+ [0, [3, 0, 3]],
+ [1, [3, 2, 1]],
+ [2, [3, 2, 0]],
+ [3, [3, 2, 3]],
+ [4, [3, 0, 1]],
+ [5, [3, 0, 0]],
+ ]
+ assert_equal(
+ expected,
+ expected.map { |pos, ids| [@mod.get_hex_position(ids), ids] })
+ end
+end

0 comments on commit f759622

Please sign in to comment.