From 4290632da5611012ab04dbfaa68338939c7fedeb Mon Sep 17 00:00:00 2001 From: Martin Prout Date: Mon, 2 Mar 2020 13:36:08 +0000 Subject: [PATCH] tidy up --- .../lsystems/library/grammar/grammar.rb | 4 +- .../lsystems/library/snowflake/snowflake.rb | 25 +++++----- processing_app/topics/lsystems/peano.rb | 4 +- processing_app/topics/lsystems/snake_kolam.rb | 48 ++++++++++--------- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/processing_app/topics/lsystems/library/grammar/grammar.rb b/processing_app/topics/lsystems/library/grammar/grammar.rb index 10df593..1b53ca5 100644 --- a/processing_app/topics/lsystems/library/grammar/grammar.rb +++ b/processing_app/topics/lsystems/library/grammar/grammar.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + ############################ # Simple lsystem grammar ############################ @@ -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 diff --git a/processing_app/topics/lsystems/library/snowflake/snowflake.rb b/processing_app/topics/lsystems/library/snowflake/snowflake.rb index 35167ba..20078be 100644 --- a/processing_app/topics/lsystems/library/snowflake/snowflake.rb +++ b/processing_app/topics/lsystems/library/snowflake/snowflake.rb @@ -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 @@ -20,22 +21,22 @@ 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 @@ -43,7 +44,7 @@ def render end ########################################## - # adjust draw length with number of repeats + # adjust draw length with number of repeat # uses grammar to set production string # see 'grammar.rb' ########################################## diff --git a/processing_app/topics/lsystems/peano.rb b/processing_app/topics/lsystems/peano.rb index 9701003..d10905c 100644 --- a/processing_app/topics/lsystems/peano.rb +++ b/processing_app/topics/lsystems/peano.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + ######################################################## # A Peano fractal implemented using a # Lindenmayer System in JRubyArt by Martin Prout @@ -80,7 +82,7 @@ def render(points) end def renderer - @renderer ||= GfxRender.new(self.g) + @renderer ||= GfxRender.new(g) end def settings diff --git a/processing_app/topics/lsystems/snake_kolam.rb b/processing_app/topics/lsystems/snake_kolam.rb index 51d490d..8a06529 100644 --- a/processing_app/topics/lsystems/snake_kolam.rb +++ b/processing_app/topics/lsystems/snake_kolam.rb @@ -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 @@ -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 @@ -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 -