Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active November 28, 2016 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/658bf02dbe76032b317f1f67303627d2 to your computer and use it in GitHub Desktop.
Save JoshCheek/658bf02dbe76032b317f1f67303627d2 to your computer and use it in GitHub Desktop.
Non-Parametric version of a parametric L-System
# encoding: utf-8
# A non-parametric version of this L-System:
# http://codepen.io/josh_cheek/details/yVoqvO/
# Install deps: $ gem install graphics -v 1.0.0b6
# Run: $ rsdl thisfile.rb
require 'graphics'
width, height = 800, 600
simulation = Graphics::Drawing.new width, height, 24
rules = {'A'=>'F-AA-F', 'F'=>'GG', 'G'=>'F'}
production = 12.times.reduce('A') { |prod| prod.gsub /#{rules.keys.join '|'}/, rules }
x, y = width/2, 108
ø, ∆ø = Math::PI/2, Math::PI/2 # start facing up with right-angle turns
stride = 3
simulation.register_color :edgy_blue, 50, 150, 255
simulation.clear :black # canvas background
simulation.rect 180, 80, 440, 440, :edgy_blue, true # image frame
simulation.rect 190, 90, 420, 420, :gray10, true # image background
production.each_char do |char|
case char
when '-' then ø += ∆ø
when '+' then ø -= ∆ø
when 'F', 'G'
old_x, old_y = x, y
x += stride*Math.cos(ø).round
y += stride*Math.sin(ø).round
simulation.line old_x, old_y, x, y, :edgy_blue
end
end
simulation.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment