Skip to content

Commit

Permalink
rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Mar 5, 2020
1 parent 9888c93 commit 877d592
Show file tree
Hide file tree
Showing 19 changed files with 169 additions and 142 deletions.
2 changes: 2 additions & 0 deletions processing_app/topics/lsystems/Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Simple demo Rakefile to autorun samples in current directory

desc 'run demo'
Expand Down
5 changes: 3 additions & 2 deletions processing_app/topics/lsystems/chequer.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env jruby
# frozen_string_literal: true

require 'propane'

class ChequerSketch < Propane::App
Expand Down Expand Up @@ -35,7 +37,7 @@ class Chequer
def initialize(xpos, ypos)
@xpos = xpos
@ypos = ypos
@axiom = 'F-F-F-F' # Axiom
@axiom = 'F-F-F-F' # Axiom
@grammar = Grammar.new(axiom, 'F' => 'FF-F-F-F-FF')
@draw_length = 500
stroke 0, 255, 0
Expand Down Expand Up @@ -73,5 +75,4 @@ def create_grammar(gen)
end
end


ChequerSketch.new
4 changes: 3 additions & 1 deletion processing_app/topics/lsystems/csplant.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env jruby
# frozen_string_literal: true

require 'propane'
require 'arcball'

Expand Down Expand Up @@ -42,7 +44,7 @@ class CSPlant

IGNORE = '[]+-^&3'
attr_reader :grammar, :axiom, :production, :premis, :rule,
:theta, :scale_factor, :len, :phi, :len
:theta, :scale_factor, :len, :phi, :len

def initialize(len)
@axiom = 'F'
Expand Down
10 changes: 6 additions & 4 deletions processing_app/topics/lsystems/cstest.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env jruby
# frozen_string_literal: true

require 'propane'

class CSTest < Propane::App
Expand All @@ -20,17 +22,17 @@ def setup
def draw
(0..7).each do |i|
grammar = Grammar.new(
'baaaaaa',
'b<a' => 'b', # context sensitive rule replace a when preceded by b
'b' => 'a'
'baaaaaa',
'b<a' => 'b', # context sensitive rule replace a when preceded by b
'b' => 'a'
)
text grammar.generate(i), 30, i * 25
end
end

def settings
size 125, 250
end
end
end

CSTest.new
15 changes: 7 additions & 8 deletions processing_app/topics/lsystems/david_tour.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env jruby
# frozen_string_literal: true

require 'propane'

class DavidTourSketch < Propane::App
load_library :grammar



attr_reader :points

def setup
Expand All @@ -27,7 +27,6 @@ def draw
def settings
size(800, 900, P2D)
end

end

########################################################
Expand All @@ -39,12 +38,12 @@ class DavidTour
DELTA = Math::PI / 3 # 60 degrees

def initialize(xpos, ypos)
@axiom = 'FX-XFX-XFX-XFX-XFX-XF' # Axiom
@theta = 0
@axiom = 'FX-XFX-XFX-XFX-XFX-XF' # Axiom
@theta = 0
@grammar = Grammar.new(
axiom,
'F' => '!F!-F-!F!', # Rules
'X' => '!X'
axiom,
'F' => '!F!-F-!F!', # Rules
'X' => '!X'
)
@draw_length = 15
@xpos = xpos
Expand Down
4 changes: 3 additions & 1 deletion processing_app/topics/lsystems/koch.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env jruby
# frozen_string_literal: true

require 'propane'

class Koch < Propane::App
Expand All @@ -7,7 +9,7 @@ class Koch < Propane::App
def setup
sketch_title 'Koch'
background(255)
frame_rate(1) # Animate slowly
frame_rate(1) # Animate slowly
@k = KochFractal.new(width, height)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

##################################
# The grammar class stores rules
# in two Hashes, one for cs rules,
Expand Down Expand Up @@ -39,7 +41,7 @@ def generate(repeat = 0) # repeat iteration grammar rules
prod
end

def new_production(prod) # single iteration grammar rules
def new_production(prod) # single iteration grammar rules
@idx = -1
prod.gsub!(/./) do |ch|
get_rule(prod, ch)
Expand Down
95 changes: 2 additions & 93 deletions 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 @@ -22,96 +24,3 @@ def generate(gen)
prod
end
end

Turtle = Struct.new(:x, :y, :angle, :color)

#############################
# PenroseColored class
#############################
class PenroseColored
include Propane::Proxy

attr_reader :axiom, :grammar, :start_length, :theta, :production,
:draw_length, :repeats, :xpos, :ypos

DELTA = 36 # degrees
RED = 70<<24|200<<16|0<<8|0 # using bit operations to set color int
BLUE = 70<<24|0<<16|0<<8|200

def initialize(xpos, ypos) # Note use of abbreviated grammar
@axiom = '[X]2+[X]2+[X]2+[X]2+[X]' # nos, used to indicate repeats
@grammar = Grammar.new(
axiom,
'F' => '', # a so called deletion rule
'W' => 'YBF2+ZRF4-XBF[-YBF4-WRF]2+',
'X' => '+YBF2-ZRF[3-WRF2-XBF]+',
'Y' => '-WRF2+XBF[3+YBF2+ZRF]-',
'Z' => '2-YBF4+WRF[+ZRF4+XBF]2-XBF')
@start_length = 1000.0
@theta = 0
@xpos = xpos
@ypos = ypos
@production = axiom.split('')
@draw_length = start_length
end

##############################################################################
# Not strictly in the spirit of either processing in my render
# function I have ignored the processing translate/rotate functions in favour
# of the direct calculation of the new x and y positions, thus avoiding such
# affine transformations.
##############################################################################

def render
repeats = 1
ignored = %w(W X Y Z)
repeated = %w(1 2 3 4)
pen = Turtle.new(xpos, ypos, theta, :R) # simple Struct for pen, symbol :R = red
stack = [] # simple array for stack
production.scan(/./) do |element|
case element
when 'F'
pen = draw_line(pen, draw_length)
when '+'
pen.angle += DELTA * repeats
repeats = 1
when '-'
pen.angle -= DELTA * repeats
repeats = 1
when '['
stack << pen.dup # push a copy current pen to stack
when ']'
pen = stack.pop # assign current pen to instance off the stack
when 'R', 'B'
pen.color = element.to_sym # set pen color as symbol
when *ignored
when *repeated
repeats = element.to_i
else puts format('Character %s not in grammar', element)
end
end
end
#####################################################
# create grammar from axiom and # rules (adjust scale)
#####################################################

def create_grammar(gen)
@draw_length *= 0.5**gen
@production = grammar.generate gen
end

private

####################################################################
# draws line using current pen position, color and length parameters
# returns a pen corresponding to the new position
###################################################################

def draw_line(pen, length)
stroke(pen.color == :R ? RED : BLUE)
new_xpos = pen.x - length * DegLut.cos(pen.angle)
new_ypos = pen.y - length * DegLut.sin(pen.angle)
line(pen.x, pen.y, new_xpos, new_ypos) # draw line
Turtle.new(new_xpos, new_ypos, pen.angle, pen.color) # return pen @ new pos
end
end
4 changes: 3 additions & 1 deletion processing_app/topics/lsystems/library/koch/koch.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Kochline class
class KochLine
include Propane::Proxy
Expand Down Expand Up @@ -83,7 +85,7 @@ def render
# for the structure. As we do this over and over again, each line gets broken
# into 4 lines, which gets broken into 4 lines, and so on. . .
def iterate(before)
[].tap do |now| # Create empty list
[].tap do |now| # Create empty list
before.each do |l|
# Calculate 5 koch vectors (done for us by the line object)
a = l.start
Expand Down
95 changes: 95 additions & 0 deletions processing_app/topics/lsystems/library/penrose/penrose.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# frozen_string_literal: true

Pen = Struct.new(:x, :y, :angle, :color)

#############################
# PenroseColored class
#############################
class PenroseColored
include Propane::Proxy

attr_reader :axiom, :grammar, :start_length, :theta, :production,
:draw_length, :repeats, :xpos, :ypos

DELTA = 36 # degrees
RED = 70 << 24 | 200 << 16 | 0 << 8 | 0 # using bit operations to set color int
BLUE = 70 << 24 | 0 << 16 | 0 << 8 | 200

def initialize(xpos, ypos) # Note use of abbreviated grammar
@axiom = '[X]2+[X]2+[X]2+[X]2+[X]' # nos, used to indicate repeats
@grammar = Grammar.new(
axiom,
'F' => '', # a so called deletion rule
'W' => 'YBF2+ZRF4-XBF[-YBF4-WRF]2+',
'X' => '+YBF2-ZRF[3-WRF2-XBF]+',
'Y' => '-WRF2+XBF[3+YBF2+ZRF]-',
'Z' => '2-YBF4+WRF[+ZRF4+XBF]2-XBF'
)
@start_length = 1000.0
@theta = 0
@xpos = xpos
@ypos = ypos
@production = axiom.split('')
@draw_length = start_length
end

##############################################################################
# Not strictly in the spirit of either processing in my render
# function I have ignored the processing translate/rotate functions in favour
# of the direct calculation of the new x and y positions, thus avoiding such
# affine transformations.
##############################################################################

def render
repeats = 1
ignored = %w[W X Y Z]
repeated = %w[1 2 3 4]
pen = Pen.new(xpos, ypos, theta, :R) # simple Struct for pen, symbol :R = red
stack = [] # simple array for stack
production.scan(/./) do |element|
case element
when 'F'
pen = draw_line(pen, draw_length)
when '+'
pen.angle += DELTA * repeats
repeats = 1
when '-'
pen.angle -= DELTA * repeats
repeats = 1
when '['
stack << pen.dup # push a copy current pen to stack
when ']'
pen = stack.pop # assign current pen to instance off the stack
when 'R', 'B'
pen.color = element.to_sym # set pen color as symbol
when *ignored
when *repeated
repeats = element.to_i
else puts format('Character %<ch>s not in grammar', ch: element)
end
end
end
#####################################################
# create grammar from axiom and # rules (adjust scale)
#####################################################

def create_grammar(gen)
@draw_length *= 0.5**gen
@production = grammar.generate gen
end

private

####################################################################
# draws line using current pen position, color and length parameters
# returns a pen corresponding to the new position
###################################################################

def draw_line(pen, length)
stroke(pen.color == :R ? RED : BLUE)
new_xpos = pen.x - length * DegLut.cos(pen.angle)
new_ypos = pen.y - length * DegLut.sin(pen.angle)
line(pen.x, pen.y, new_xpos, new_ypos) # draw line
Pen.new(new_xpos, new_ypos, pen.angle, pen.color) # return pen @ new pos
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

########################
# stochastic_grammar.rb
# unweighted rules accepted
Expand All @@ -22,6 +24,7 @@ def stochastic_rule(rules)
chance = rand(0..total)
rules.each do |item, weight|
return item unless chance > weight

chance -= weight
end
end
Expand All @@ -39,7 +42,7 @@ def add_rule(pre, rule, weight = 1.0) # default weighting 1
end
end

def new_production(prod) # note the use of gsub!
def new_production(prod) # note the use of gsub!
prod.gsub!(/./) do |ch|
rule?(ch) ? stochastic_rule(srules[ch]) : ch
end
Expand Down
Loading

0 comments on commit 877d592

Please sign in to comment.