Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Mar 2, 2020
1 parent fbbd75b commit 4290632
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
4 changes: 3 additions & 1 deletion processing_app/topics/lsystems/library/grammar/grammar.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

############################
# Simple lsystem grammar
############################
Expand All @@ -10,7 +12,7 @@ def initialize(axiom, rules)

def expand(production, iterations, &block)
production.each_char do |token|
if rules.key?(token) && iterations > 0
if rules.key?(token) && iterations.positive?
expand(rules[token], iterations - 1, &block)
else
yield token
Expand Down
25 changes: 13 additions & 12 deletions processing_app/topics/lsystems/library/snowflake/snowflake.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Using abbreviated lsystem syntax NB: 9 = 10 repeats and 3 = 4 repeats
class PenroseSnowflake
include Processing::Proxy

attr_accessor :axiom, :grammar, :start_length, :theta, :production,
:draw_length, :pos
DELTA = PI / 10
DELTA = PI / 5 # 36 degrees

def initialize(pos)
@axiom = 'F3-F3-F3-F3-F'
@grammar = Grammar.new(axiom, 'F' => 'F3-F3-F45-F++F3-F')
@axiom = 'F2-F2-F2-F2-F'
@grammar = Grammar.new(axiom, 'F' => 'F2-F2-F5-F+F2-F')
@start_length = 450.0
@theta = 0
@pos = pos
Expand All @@ -20,30 +21,30 @@ def initialize(pos)
##############################################################################

def render
repeats = 1
repeat = 1
production.each do |element|
case element
when 'F'
new_pos = pos + Vec2D.from_angle(theta) * draw_length
line(pos.x, pos.y, new_pos.x, new_pos.y)
@pos = new_pos
repeats = 1
repeat = 1
when '+'
@theta += DELTA * repeats
repeats = 1
@theta += DELTA * repeat
repeat = 1
when '-'
@theta -= DELTA * repeats
repeats = 1
when '3', '4', '5'
repeats += element.to_i
@theta -= DELTA * repeat
repeat = 1
when '2', '5'
repeat = element.to_i
else
puts "Character '#{element}' is not in grammar"
end
end
end

##########################################
# adjust draw length with number of repeats
# adjust draw length with number of repeat
# uses grammar to set production string
# see 'grammar.rb'
##########################################
Expand Down
4 changes: 3 additions & 1 deletion processing_app/topics/lsystems/peano.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

########################################################
# A Peano fractal implemented using a
# Lindenmayer System in JRubyArt by Martin Prout
Expand Down Expand Up @@ -80,7 +82,7 @@ def render(points)
end

def renderer
@renderer ||= GfxRender.new(self.g)
@renderer ||= GfxRender.new(g)
end

def settings
Expand Down
48 changes: 25 additions & 23 deletions processing_app/topics/lsystems/snake_kolam.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
# frozen_string_literal: true

#######################################################
# Lindenmayer System in ruby-procesDegLut.sing by Martin Prout
# snake_kolam.rb uDegLut.sing l-systems
#######################################################
load_library :grammar

attr_reader :kolam

def setup
sketch_title 'Snake Kolam'
@kolam = SnakeKolam.new width / 8, height * 0.8
kolam.create_grammar 3 # create grammar from rules
no_loop
end

def draw
background 0
stroke_weight 3
stroke 200
kolam.render
end

def settings
size 500, 500
end

# class SnakeKolam
class SnakeKolam
include Processing::Proxy
Expand All @@ -27,10 +49,11 @@ def setup_grammar
@axiom = 'FX+F+FX+F'
@grammar = Grammar.new(
axiom,
'X' => 'X-F-F+FX+F+FX-F-F+FX')
'X' => 'X-F-F+FX+F+FX-F-F+FX'
)
end

def render # NB not using affine transforms here
def render # NB not using affine transforms here
turtle = [xpos, ypos, 0.0]
production.each do |element|
case element
Expand Down Expand Up @@ -72,24 +95,3 @@ def draw_line(turtle, length)
[new_xpos, new_ypos, turtle[ANGLE]]
end
end

attr_reader :kolam

def setup
sketch_title 'Snake Kolam'
@kolam = SnakeKolam.new width / 8, height * 0.8
kolam.create_grammar 3 # create grammar from rules
no_loop
end

def draw
background 0
stroke_weight 3
stroke 200
kolam.render
end

def settings
size 500, 500
end

0 comments on commit 4290632

Please sign in to comment.