Skip to content

Commit 65f91ac

Browse files
committed
refactor penrose
1 parent bd27abc commit 65f91ac

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

processing_app/library/vecmath/vec2d/library/tile/tile.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
module Tiler
2-
@acute = false
1+
# frozen_string_literal: true
2+
# Tile factory
3+
class TileFactory
4+
attr_reader :acute
35

4-
def self.acute(x)
5-
@acute = x
6+
def initialize(acute = false)
7+
@acute = acute
68
end
79

810
# setup the initial tiling with all red tiles
9-
def self.tile(a, b, c)
10-
tile = @acute ? ATile.new(0, a, b, c) : Tile.new(0, a, b, c)
11+
def tile(a, b, c)
12+
return ATile.new(0, a, b, c) if acute
13+
Tile.new(0, a, b, c)
1114
end
1215
end
1316

17+
# Regular Tile class
1418
class Tile
1519
include Processing::Proxy
1620
PHI = (1.0 + Math.sqrt(5)) / 2.0 # golden ratio
17-
RED = [255, 0, 0]
18-
BLUE = [0, 0, 255]
19-
COLORS = [RED, BLUE]
21+
COLORS = %w(#ff0000 #0000ff).freeze # red blue
2022
attr_reader :a, :b, :c, :col
2123

22-
def initialize(col, a, b, c)
24+
def initialize(col, a, b, c)
2325
@col, @a, @b, @c = col, a, b, c
2426
end
2527

2628
def display
2729
no_stroke
28-
fill(*COLORS[col])
30+
fill(color(COLORS[col]))
2931
triangle(a.x, a.y, b.x, b.y, c.x, c.y)
3032
# fill(0, 0, 255)
3133
# ellipse(a.x, a.y, 4,4)
@@ -35,7 +37,7 @@ def display
3537

3638
def subdivide
3739
result = []
38-
if (col == 0)
40+
if col.zero?
3941
# Subdivide red triangle
4042
p = b - a
4143
p /= PHI
@@ -58,10 +60,11 @@ def subdivide
5860
end
5961
end
6062

63+
# Acute Tile class
6164
class ATile < Tile
6265
def subdivide
6366
result = []
64-
if (col == 0)
67+
if col.zero?
6568
# Subdivide red (half kite) triangle
6669
q = b - a
6770
q /= PHI
@@ -80,6 +83,6 @@ def subdivide
8083
result << ATile.new(1, b, p, a)
8184
result << ATile.new(0, p, c, b)
8285
end
83-
result
86+
result
8487
end
8588
end

processing_app/library/vecmath/vec2d/penrose.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Translated (and refactored) to JRubyArt July 2015 by Martin Prout
66

77
load_libraries :tile, :control_panel
8-
attr_reader :tris, :s, :panel, :hide, :acute
8+
attr_reader :tris, :s, :panel, :hide, :acute, :tiler
99

1010
def setup
1111
sketch_title 'Penrose'
@@ -19,6 +19,7 @@ def setup
1919
@panel = c
2020
end
2121
@hide = false
22+
@tiler = TileFactory.new(acute) # set the Tiler first
2223
init false # defaults to regular penrose
2324
end
2425

@@ -30,9 +31,7 @@ def draw
3031
end
3132
background(255)
3233
translate(width / 2, height / 2)
33-
tris.each do |t|
34-
t.display
35-
end
34+
tris.each(&:display)
3635
end
3736

3837
def generate
@@ -47,26 +46,22 @@ def generate
4746
end
4847

4948
def reset!
50-
Tiler.acute(acute) # set the Tiler first
49+
@tiler = TileFactory.new(acute) # set the Tiler first
5150
init @seed
5251
java.lang.System.gc # but does it do any good?
5352
end
5453

5554
def init(alt_seed)
5655
@tris = []
57-
10.times do |i| # create 36 degree segments
56+
10.times do |i| # create 36 degree segments
5857
a = Vec2D.new
5958
b = Vec2D.from_angle((2 * i - 1) * PI / 10)
6059
c = Vec2D.from_angle((2 * i + 1) * PI / 10)
6160
b *= 370
6261
c *= 370
63-
if alt_seed
64-
tile = i.even? ? Tiler.tile(b, a, c) : Tiler.tile(c, a, b)
65-
tris << tile
66-
else
67-
tile = i.even? ? Tiler.tile(a, b, c) : Tiler.tile(a, c, b)
68-
tris << tile
69-
end
62+
tile = i.even? ? tiler.tile(b, a, c) : tiler.tile(c, a, b) if alt_seed
63+
tile = i.even? ? tiler.tile(a, b, c) : tiler.tile(a, c, b) unless alt_seed
64+
tris << tile
7065
end
7166
end
7267

0 commit comments

Comments
 (0)