Skip to content

Commit 336a6ea

Browse files
author
monkstone
committed
some cleanups
1 parent 5ddd8f2 commit 336a6ea

File tree

21 files changed

+276
-320
lines changed

21 files changed

+276
-320
lines changed

samples/processing_app/demos/performance/cubic_grid_retained.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def setup
1919
no_stroke
2020
@grid = create_shape(GROUP)
2121
# Build grid using multiple translations
22-
(-(DEPTH/2 + MARGIN) ... (DEPTH/2 - MARGIN)).step(BOX_SIZE) do |i|
23-
(-(height + MARGIN) ... (height - MARGIN)).step(BOX_SIZE) do |j|
24-
(-(width + MARGIN) ... (width - MARGIN)).step(BOX_SIZE) do |k|
22+
(-(DEPTH/2 + MARGIN)...(DEPTH/2 - MARGIN)).step(BOX_SIZE) do |i|
23+
(-(height + MARGIN)...(height - MARGIN)).step(BOX_SIZE) do |j|
24+
(-(width + MARGIN)...(width - MARGIN)).step(BOX_SIZE) do |k|
2525
# Base fill color on counter values, abs function
2626
# ensures values stay within legal range
2727
@box_fill = color(i.abs.to_i, j.abs.to_i, k.abs.to_i, 50)
@@ -38,21 +38,21 @@ def draw
3838
background(255)
3939
hint(DISABLE_DEPTH_TEST)
4040
# Center and spin grid
41-
push_matrix()
42-
translate(width/2, height/2, -DEPTH)
41+
push_matrix
42+
translate(width / 2, height / 2, -DEPTH)
4343
rotate_y(frame_count * 0.01)
4444
rotate_x(frame_count * 0.01)
4545
shape(grid)
46-
pop_matrix()
46+
pop_matrix
4747
hint(ENABLE_DEPTH_TEST)
4848
@fcount += 1
49-
m = millis()
49+
m = millis
5050
if (m - lastm > 1000 * FINT)
5151
@frate = fcount / FINT
5252
@fcount = 0
5353
@lastm = m
54-
puts("fps: #{frate}")
54+
puts(format('fps: %s', frate))
5555
end
5656
fill(0)
57-
text("fps: #{frate}", 10, 20)
57+
text(format('fps: %s', frate), 10, 20)
5858
end

samples/processing_app/demos/performance/esfera.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setup
2323
@rx = 0
2424
@ry =0
2525
no_smooth
26-
@radius = height/3.5
26+
@radius = height / 3.5
2727
@orb = HairyOrb.new(self, radius)
2828
QUANTITY.times do
2929
orb << create_hair(radius)
@@ -37,9 +37,7 @@ def draw
3737
no_stroke
3838
sphere(radius)
3939
orb.render
40-
if (frame_count % 10 == 0)
41-
puts(frame_rate)
42-
end
40+
puts(frame_rate) if (frame_count % 10 == 0)
4341
end
4442

4543
def create_hair radius
@@ -90,4 +88,4 @@ def render
9088
end
9189
end
9290

93-
Hair = Struct.new(:z, :phi, :len, :theta )
91+
Hair = Struct.new(:z, :phi, :len, :theta )

samples/processing_app/topics/lsystems/chequer.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
def setup
1111
size 600, 600
12-
@chequer = Chequer.new width * 0.9, height/10
13-
chequer.create_grammar 4
12+
@chequer = Chequer.new width * 0.9, height / 10
13+
chequer.create_grammar 4
1414
no_loop
1515
end
1616

@@ -19,29 +19,30 @@ def draw
1919
chequer.render
2020
end
2121

22+
# Chequer class
2223
class Chequer
2324
include Processing::Proxy
2425
attr_accessor :axiom, :grammar, :production, :draw_length, :theta, :xpos, :ypos
2526
DELTA = HALF_PI
26-
27+
2728
def initialize(xpos, ypos)
28-
@xpos = xpos
29-
@ypos = ypos
29+
@xpos = xpos
30+
@ypos = ypos
3031
@axiom = 'F-F-F-F' # Axiom
3132
@grammar = Grammar.new(axiom, 'F' => 'FF-F-F-F-FF')
3233
@draw_length = 500
3334
stroke 0, 255, 0
34-
stroke_weight 2
35+
stroke_weight 2
3536
@theta = 0
3637
end
37-
38+
3839
def render
3940
production.each do |element|
4041
case element
41-
when 'F'
42+
when 'F'
4243
x_temp = xpos
4344
y_temp = ypos
44-
@xpos -= draw_length * cos(theta)
45+
@xpos -= draw_length * cos(theta)
4546
@ypos -= draw_length * sin(theta)
4647
line(x_temp, y_temp, xpos, ypos)
4748
when '+'
@@ -53,14 +54,14 @@ def render
5354
end
5455
end
5556
end
56-
57+
5758
##############################
5859
# create grammar from axiom and
5960
# rules (adjust scale)
6061
##############################
61-
62+
6263
def create_grammar(gen)
63-
@draw_length /= 3**gen
64+
@draw_length /= 3**gen
6465
@production = @grammar.generate gen
6566
end
6667
end

samples/processing_app/topics/lsystems/csplant.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
########################################################
22
# csplant.rb
33
# A 3D Plant implemented using a Context Sensitive
4-
# Lindenmayer System in ruby-processing
4+
# Lindenmayer System in ruby-processing
55
# by Martin Prout (30 January 2013)
66
# Hold down 'y' key and drag mouse to rotate about y axis
77
########################################################
@@ -20,29 +20,25 @@ def setup
2020
def draw
2121
background 0
2222
lights
23-
translate(0, height*0.3)
23+
translate(0, height * 0.3)
2424
csplant.render
2525
end
2626

2727
############
28-
# CSPlant
28+
# CSPlant
2929
############
30-
3130
class CSPlant
3231
include Processing::Proxy
3332

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

3837
def initialize(len)
3938
@axiom = 'F'
4039
@grammar = Grammar.new(
4140
axiom,
42-
{
43-
'F' => 'F[-EF[3&A]]E[+F[3^A]]',
44-
'F<E' => 'F[&F[3+A]][^F[3-A]]'
45-
},
41+
{ 'F' => 'F[-EF[3&A]]E[+F[3^A]]', 'F<E' => 'F[&F[3+A]][^F[3-A]]' },
4642
IGNORE
4743
)
4844
@production = axiom

samples/processing_app/topics/lsystems/cstest.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ def setup
1313
(0..7).each do |i|
1414
grammar = Grammar.new(
1515
'baaaaaa',
16-
{
17-
'b<a' => 'b', # context sensitive rule replace a when preceded by b
18-
'b' =>'a'
19-
}
20-
)
16+
'b<a' => 'b', # context sensitive rule replace a when preceded by b
17+
'b' => 'a'
18+
)
2119
text grammar.generate(i), 30, i * 25
2220
end
2321
end
24-
25-
Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
####################################################
2-
# The DavidTour fractal has been used to study the
3-
# Euclidean travelling salesman problem
4-
####################################################
51
load_library :grammar
62

3+
########################################################
4+
# A David Tour fractal implemented using a
5+
# Lindenmayer System in ruby-processing by Martin Prout
6+
########################################################
77
class DavidTour
88
import 'grammar'
99
attr_reader :draw_length, :xpos, :ypos, :theta, :axiom, :grammar
10-
DELTA = Math::PI/3 # 60 degrees
10+
DELTA = Math::PI / 3 # 60 degrees
1111

1212
def initialize(xpos, ypos)
1313
@axiom = 'FX-XFX-XFX-XFX-XFX-XF' # Axiom
@@ -16,7 +16,7 @@ def initialize(xpos, ypos)
1616
axiom,
1717
'F' => '!F!-F-!F!', # Rules
1818
'X' => '!X'
19-
)
19+
)
2020
@draw_length = 15
2121
@xpos = xpos
2222
@ypos = ypos
@@ -29,38 +29,31 @@ def create_grammar(gen)
2929

3030
def translate_rules(prod)
3131
swap = false
32-
points = [] # An empty array to store lines as a flat array of points
33-
prod.each do |ch|
34-
case(ch)
35-
when 'F'
36-
points << xpos << ypos << (@xpos += draw_length * Math.cos(theta)) << (@ypos -= draw_length * Math.sin(theta))
37-
when '+'
38-
@theta += DELTA
39-
when '-'
40-
@theta += swap ? DELTA : -DELTA
41-
when '!'
42-
swap = !swap
43-
when 'X'
44-
else
45-
puts("character '#{ch}' not in grammar")
32+
[].tap do |points| # An array to store lines as a flat array of points
33+
prod.each do |ch|
34+
case ch
35+
when 'F'
36+
points << xpos << ypos << (@xpos += draw_length * Math.cos(theta)) << (@ypos -= draw_length * Math.sin(theta))
37+
when '+'
38+
@theta += DELTA
39+
when '-'
40+
@theta += swap ? DELTA : -DELTA
41+
when '!'
42+
swap = !swap
43+
when 'X'
44+
else
45+
puts("character '#{ch}' not in grammar")
46+
end
4647
end
4748
end
48-
return points
4949
end
5050
end
5151

52-
53-
54-
########################################################
55-
# A David Tour fractal implemented using a
56-
# Lindenmayer System in ruby-processing by Martin Prout
57-
########################################################
58-
5952
attr_reader :points
6053

6154
def setup
6255
size(800, 900)
63-
david = DavidTour.new(width * 0.6, height/4)
56+
david = DavidTour.new(width * 0.6, height / 4)
6457
production = david.create_grammar(5)
6558
@points = david.translate_rules(production)
6659
no_loop
@@ -72,4 +65,4 @@ def draw
7265
points.each_slice(4) do |point|
7366
line(*point)
7467
end
75-
end
68+
end
Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
######################################
2-
# cs_grammar.rb a context sensitive
3-
# 1-L lsystem grammar for
4-
# ruby/ruby-processing
5-
# by Martin Prout (January 2013)
6-
######################################
7-
8-
9-
101
##################################
112
# The grammar class stores rules
123
# in two Hashes, one for cs rules,
134
# one for context free rules. Rules
145
# are filtered on input, and context
156
# is checked using get_rule in production
167
##################################
17-
188
class Grammar
19-
209
attr_reader :axiom, :context, :no_context, :idx, :ignore
10+
2111
def initialize(axiom, rules, ignore = '')
2212
@axiom = axiom
2313
@no_context = {}
@@ -39,44 +29,43 @@ def add_rule(pre, rule)
3929
elsif pre.length == 1
4030
@no_context[pre] = rule # key length == 1
4131
else
42-
print 'unrecognized grammar '#{pre}''
32+
puts "unrecognized grammar '#{pre}'"
4333
end
4434
end
4535

4636
def generate(repeat = 0) # repeat iteration grammar rules
4737
prod = axiom
4838
repeat.times { prod = new_production(prod) }
49-
return prod
39+
prod
5040
end
5141

52-
53-
def new_production prod # single iteration grammar rules
42+
def new_production(prod) # single iteration grammar rules
5443
@idx = -1
5544
prod.gsub!(/./) do |ch|
5645
get_rule(prod, ch)
5746
end
5847
end
5948

60-
def get_rule prod, ch
49+
def get_rule(prod, ch)
6150
rule = ch # default is return original character as rule (no change)
6251
@idx += 1 # increment the index of axiom/production as a side effect
6352
if context.key?(ch)
6453
if context[ch][1] == '<'
6554
cs_char = context[ch][0]
66-
rule = no_context[context[ch]] if cs_char == get_context(prod, idx, -1) # use context sensitive rule
55+
rule = no_context[context[ch]] if cs_char == get_context(prod, idx, -1)
6756
elsif context[ch][1] == '>'
6857
cs_char = context[ch][2]
69-
rule = no_context[context[ch]] if cs_char == get_context(prod, idx, 1) # use context sensitive rule
58+
rule = no_context[context[ch]] if cs_char == get_context(prod, idx, 1)
7059
end
7160
else
72-
rule = no_context[ch] if no_context.key?(ch) # context free rule if it exists
61+
rule = no_context[ch] if no_context.key?(ch)
7362
end
74-
return rule
63+
rule
7564
end
7665

7766
def get_context(prod, idx, inc)
7867
index = idx + inc
7968
index += inc while ignore.include?(prod[index])
80-
return prod[index]
69+
prod[index]
8170
end
82-
end
71+
end

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Simple lsystem grammar
33
############################
44
class Grammar
5-
65
attr_reader :axiom, :rules
76
def initialize(axiom, rules)
87
@axiom = axiom
@@ -24,8 +23,8 @@ def each(gen)
2423
end
2524

2625
def generate(gen)
27-
output = []
28-
each(gen) { |token| output << token }
29-
return output
26+
[].tap do |output|
27+
each(gen) { |token| output << token }
28+
end
3029
end
31-
end
30+
end

0 commit comments

Comments
 (0)