Skip to content

Commit d314551

Browse files
committed
Refactor LSystems
1 parent 6ea530c commit d314551

File tree

9 files changed

+32
-137
lines changed

9 files changed

+32
-137
lines changed

processing_app/topics/lsystems/chequer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def initialize(xpos, ypos)
4444
end
4545

4646
def render
47-
production.each do |element|
47+
production.scan(/./) do |element|
4848
case element
4949
when 'F'
5050
x_temp = xpos

processing_app/topics/lsystems/david_tour.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def create_grammar(gen)
5959
def translate_rules(prod)
6060
swap = false
6161
[].tap do |points| # An array to store lines as a flat array of points
62-
prod.each do |ch|
62+
prod.scan(/./) do |ch|
6363
case ch
6464
when 'F'
6565
points << xpos << ypos << (@xpos += draw_length * Math.cos(theta)) << (@ypos -= draw_length * Math.sin(theta))

processing_app/topics/lsystems/library/grammar/grammar.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,21 @@ def initialize(axiom, rules)
88
@rules = rules
99
end
1010

11-
def expand(production, iterations, &block)
12-
production.each_char do |token|
13-
if rules.key?(token) && iterations > 0
14-
expand(rules[token], iterations - 1, &block)
15-
else
16-
yield token
17-
end
18-
end
19-
end
20-
21-
def each(gen)
22-
expand(axiom, gen) { |token| yield token }
11+
def apply_rules(prod)
12+
prod.gsub(/./) { |token| rules.fetch(token, token) }
2313
end
2414

2515
def generate(gen)
26-
[].tap do |output|
27-
each(gen) { |token| output << token }
16+
return axiom if gen.zero?
17+
18+
prod = axiom
19+
gen.times do
20+
prod = apply_rules(prod)
2821
end
22+
prod
2923
end
3024
end
3125

32-
3326
Turtle = Struct.new(:x, :y, :angle, :color)
3427

3528
#############################
@@ -75,7 +68,7 @@ def render
7568
repeated = %w(1 2 3 4)
7669
pen = Turtle.new(xpos, ypos, theta, :R) # simple Struct for pen, symbol :R = red
7770
stack = [] # simple array for stack
78-
production.each do |element|
71+
production.scan(/./) do |element|
7972
case element
8073
when 'F'
8174
pen = draw_line(pen, draw_length)

processing_app/topics/lsystems/library/koch/koch_fractal.rb

Lines changed: 0 additions & 102 deletions
This file was deleted.

processing_app/topics/lsystems/mpeano.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def generate(gen)
6262

6363
def translate_rules(prod)
6464
[].tap do |points| # An empty array to store line vertices
65-
prod.each do |ch|
65+
prod.scan(/./) do |ch|
6666
case ch
6767
when 'F'
6868
points << xpos << ypos << (@xpos -= draw_length * cos(theta)) << (@ypos -= draw_length * sin(theta))

processing_app/topics/lsystems/peano.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def translate_rules(prod)
6868
coss = ->(orig, alpha, len) { orig + len * DegLut.cos(alpha) }
6969
sinn = ->(orig, alpha, len) { orig - len * DegLut.sin(alpha) }
7070
[].tap do |pts| # An array to store line vertices as Vec2D
71-
prod.each do |ch|
71+
prod.scan(/./) do |ch|
7272
case ch
7373
when 'F'
7474
pts << vec.copy

processing_app/topics/lsystems/penrose_snowflake.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ class PenroseSnowflake
3535

3636
attr_accessor :axiom, :grammar, :start_length, :theta, :production,
3737
:draw_length, :xpos, :ypos
38-
DELTA = 18 # degrees as radians
38+
DELTA = 36 # degrees
3939

4040
def initialize(xpos, ypos)
41-
@axiom = 'F3-F3-F3-F3-F'
42-
@grammar = Grammar.new(axiom, 'F' => 'F3-F3-F45-F++F3-F')
41+
@axiom = 'F2-F2-F2-F2-F'
42+
@grammar = Grammar.new(axiom, 'F' => 'F2-F2-F5-F+F2-F')
4343
@start_length = 450.0
4444
@theta = 0
4545
@xpos = xpos
@@ -54,7 +54,7 @@ def initialize(xpos, ypos)
5454

5555
def render
5656
repeats = 1
57-
production.each do |element|
57+
production.scan(/./) do |element|
5858
case element
5959
when 'F'
6060
line(xpos, ypos, (@xpos -= multiplier(repeats, :cos)), (@ypos += multiplier(repeats, :sin)))
@@ -65,8 +65,8 @@ def render
6565
when '-'
6666
@theta -= DELTA * repeats
6767
repeats = 1
68-
when '3', '4', '5'
69-
repeats += element.to_i
68+
when '2', '5'
69+
repeats = element.to_i
7070
else
7171
puts "Character '#{element}' is not in grammar"
7272
end

processing_app/topics/lsystems/pentagonal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def make_shape
7979
shape.stroke 200
8080
shape.stroke_weight 3
8181
shape.vertex(xpos, ypos)
82-
production.each do |element|
82+
production.scan(/./) do |element|
8383
case element
8484
when 'F'
8585
shape.vertex(@xpos -= adjust(:cos), @ypos += adjust(:sin))

processing_app/topics/lsystems/snake_kolam.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def settings
2828
end
2929
end
3030

31+
SimpleTurtle = Struct.new(:x, :y, :angle)
32+
3133
# class SnakeKolam
3234
class SnakeKolam
3335
include Propane::Proxy
@@ -55,15 +57,15 @@ def setup_grammar
5557
end
5658

5759
def render # NB not using affine transforms here
58-
turtle = [xpos, ypos, 0]
59-
production.each do |element|
60+
turtle = SimpleTurtle.new(xpos, ypos, 0)
61+
production.scan(/./) do |element|
6062
case element
6163
when 'F'
6264
turtle = draw_line(turtle, draw_length)
6365
when '+'
64-
turtle[ANGLE] += DELTA
66+
turtle.angle += DELTA
6567
when '-'
66-
turtle[ANGLE] -= DELTA
68+
turtle.angle -= DELTA
6769
when 'X'
6870
else
6971
puts "Character '#{element}' is not in grammar"
@@ -90,10 +92,12 @@ def create_grammar(gen)
9092
######################################################
9193

9294
def draw_line(turtle, length)
93-
new_xpos = turtle[XPOS] + length * DegLut.cos(turtle[ANGLE])
94-
new_ypos = turtle[YPOS] + length * DegLut.sin(turtle[ANGLE])
95-
line(turtle[XPOS], turtle[YPOS], new_xpos, new_ypos)
96-
[new_xpos, new_ypos, turtle[ANGLE]]
95+
x_temp = turtle.x
96+
y_temp = turtle.y
97+
turtle.x += draw_length * DegLut.cos(turtle.angle)
98+
turtle.y += draw_length * DegLut.sin(turtle.angle)
99+
line(x_temp, y_temp, turtle.x, turtle.y)
100+
turtle
97101
end
98102
end
99103

0 commit comments

Comments
 (0)