Permalink
Browse files

annotate polygons with basic tricoloring data

  • Loading branch information...
1 parent 5a0981e commit 59dad44351f967702336cc0259b98aa8daa38b85 @jamis committed Feb 18, 2011
Showing with 47 additions and 7 deletions.
  1. +0 −2 TODO
  2. +2 −1 lib/kaleidoscope/pattern.rb
  3. +3 −2 lib/kaleidoscope/polygon.rb
  4. +8 −2 lib/kaleidoscope/tile.rb
  5. +6 −0 test/pattern_test.rb
  6. +8 −0 test/polygon_test.rb
  7. +20 −0 test/tile_test.rb
View
2 TODO
@@ -1,3 +1 @@
-incorporate coloring data for generated polygons
-
SPEED. It's pretty slow right now.
View
3 lib/kaleidoscope/pattern.rb
@@ -79,8 +79,9 @@ def build_at(seed, increment, &validator)
end
if valid_edges.any?
+ color = data[:colors][center]
center = @dict.canonical(trans.apply(center))
- poly = (@poly_map[center] ||= Polygon.new(self, center))
+ poly = (@poly_map[center] ||= Polygon.new(self, center, color))
poly.outside! unless inside
valid_edges.each { |edge, neighbor| poly.edge_map[edge] = neighbor }
valid_edges_generated = true
View
5 lib/kaleidoscope/polygon.rb
@@ -1,11 +1,12 @@
module Kaleidoscope
class Polygon
- attr_reader :master, :center
+ attr_reader :master, :center, :color
attr_reader :edge_map
- def initialize(master, center)
+ def initialize(master, center, color=nil)
@master = master
@center = center
+ @color = color
@edge_map = {}
@inside = true
View
10 lib/kaleidoscope/tile.rb
@@ -6,7 +6,7 @@
module Kaleidoscope
class Tile
attr_reader :triangle, :u, :v
- attr_reader :edges, :polygons
+ attr_reader :edges, :polygons, :colors
def initialize(p, q, u=nil, v=nil)
@triangle = Triangle.new(p, q)
@@ -33,7 +33,7 @@ def phase(n)
t.translate(-@triangle.q_length, 0)
t.rotate(phase_angle * n) if n != 0
- data = { :edges => [], :polygons => {} }
+ data = { :edges => [], :polygons => {}, :colors => {} }
edge_map = {}
@edges.each do |edge|
@@ -44,6 +44,7 @@ def phase(n)
@polygons.each do |center, edges|
c1 = t.apply(center)
+ data[:colors][c1] = @colors[center]
data[:polygons][c1] = edges.inject({}) do |hash, (e, c)|
hash[edge_map[e]] = t.apply(c)
hash
@@ -105,6 +106,9 @@ def build_polygons!
j => { e3 => c, e7 => i, e8 => g }
}
+ # polygon color map
+ color_map = { c => 0, g => 1, h => 2, i => 1, j => 2 }
+
edges = {}
center_ok = proc { |list| list.include?(e1) && list.include?(e2) }
@@ -117,11 +121,13 @@ def build_polygons!
}
@polygons = {}
+ @colors = {}
edge_map.each do |center, map|
keepers = map.keys.select { |e| e.length.abs > 0.001 }
if constraints[center][keepers]
@polygons[center] = {}
+ @colors[center] = color_map[center]
keepers.each do |edge|
edges[edge] = true
@polygons[center][edge] = map[edge]
View
6 test/pattern_test.rb
@@ -37,6 +37,12 @@ def test_build_at_should_tile_all_phases_at_the_given_point
assert_equal 42, pattern.edges.length
end
+ def test_build_at_should_set_color_data_on_generated_polygons
+ pattern = Pattern.new(6, 3)
+ pattern.build_at(Point.new(0, 0), 0)
+ assert pattern.polygons.all? { |poly| poly.color }
+ end
+
def test_build_at_with_even_increment_should_not_rotate_tile_for_odd_p
origin, pattern = Point.new(0, 0), Pattern.new(3, 6)
neighbors = pattern.build_at(origin, 0)
View
8 test/polygon_test.rb
@@ -31,6 +31,14 @@ def test_polygon_constructor_expects_master_object_and_center_point
assert_equal @point, poly.center
end
+ def test_polygon_constructor_accepts_optional_coloring_parameter
+ poly = Polygon.new(@master, @point)
+ assert_nil poly.color
+
+ poly = Polygon.new(@master, @point, 2)
+ assert_equal 2, poly.color
+ end
+
def test_polygon_initially_has_no_edges
poly = Polygon.new(@master, @point)
assert poly.edges.empty?
View
20 test/tile_test.rb
@@ -53,6 +53,20 @@ def test_tile_should_group_edges_by_polygon_centerpoint
assert_equal 3, tile.polygons[p4].length
end
+ def test_tile_should_generate_coloring_data_for_each_polygon
+ tile = Tile.new(6, 3)
+ assert_equal tile.colors.length, tile.polygons.length
+ assert_equal 3, tile.colors.values.uniq.length
+
+ tile = Tile.new(6, 3, 0, 0)
+ assert_equal tile.colors.length, tile.polygons.length
+ assert_equal 2, tile.colors.values.uniq.length
+
+ tile = Tile.new(6, 3, 1, 0)
+ assert_equal tile.colors.length, tile.polygons.length
+ assert_equal 1, tile.colors.values.uniq.length
+ end
+
def test_polygon_edges_should_map_to_centerpoint_of_neighboring_polygon
tile = Tile.new(6, 3)
@@ -135,6 +149,12 @@ def test_phase_should_report_polygons
end
end
+ def test_phase_should_report_coloring
+ tile = Tile.new(6, 3)
+ data = tile.phase(0)
+ assert_equal data[:colors].length, data[:polygons].length
+ end
+
private
def expected_data_for(tile)

0 comments on commit 59dad44

Please sign in to comment.