Skip to content

Commit 01fe889

Browse files
committed
some refactoring
1 parent f67aee8 commit 01fe889

File tree

13 files changed

+38
-48
lines changed

13 files changed

+38
-48
lines changed

library/video/capture/mirror2.rb

+10-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env jruby -w
2+
# frozen_string_literal: true
3+
24
require 'picrate'
35
# Mirror 2
46
# by Daniel Shiffman.
@@ -8,19 +10,15 @@
810
class Mirror2 < Processing::App
911
load_library :video
1012
java_import 'processing.video.Capture'
11-
attr_reader :video, :cols, :rows
13+
attr_reader :video
1214
# Size of each cell in the grid
1315
CELL_SIZE = 15
1416

1517
def setup
1618
sketch_title 'mirror'
17-
frame_rate(30)
18-
@cols = width / CELL_SIZE
19-
@rows = height / CELL_SIZE
2019
colorMode(RGB, 255, 255, 255, 100)
21-
# Try test_capture to find name of your Camera
22-
@video = Capture.new(self, width, height, "UVC Camera (046d:0825)")
23-
# Start capturing the images from the camera
20+
# Try test_capture to find the name of your Camera
21+
@video = Capture.new(self, width, height, 'UVC Camera (046d:0825)')
2422
video.start
2523
end
2624

@@ -30,25 +28,18 @@ def draw
3028
background(0, 0, 255)
3129
video.read
3230
video.load_pixels
33-
grid(cols, rows) do |i, j|
34-
x = i * CELL_SIZE
35-
y = j * CELL_SIZE
36-
loc = (width - x -1 + y * width)# Reversing x to mirror the image
37-
col = video.pixels[loc]
38-
# Code for drawing a single rect
39-
# Using translate in order for rotation to work properly
40-
# rectangle size is based on brightness
41-
sz = g.brightness(col) / 255 * CELL_SIZE
31+
grid(width, height, CELL_SIZE, CELL_SIZE) do |x, y|
32+
loc = (width - x - 1 + y * width) # Reversing x to mirror the image
33+
sz = brightness(video.pixels[loc]) / 255 * CELL_SIZE
4234
rect_mode(CENTER)
4335
fill(255)
4436
no_stroke
45-
# Rects are larger than the cell for some overlap
46-
rect(x + CELL_SIZE / 2, y + CELL_SIZE / 2, sz, sz)
37+
rect(x + CELL_SIZE / 2.0, y + CELL_SIZE / 2.0, sz, sz)
4738
end
4839
end
4940

5041
def settings
51-
size(480, 360)
42+
size(960, 544)
5243
end
5344
end
5445

lsystems/chequer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def initialize(xpos, ypos)
4444
end
4545

4646
def render
47-
production.each do |element|
47+
production.scan(/./) do |element|
4848
case element
4949
when 'F'
5050
x_temp = xpos

lsystems/david_tour.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def create_grammar(gen)
5454
def translate_rules(prod)
5555
swap = false
5656
[].tap do |points| # An array to store lines as a flat array of points
57-
prod.each do |ch|
57+
prod.scan(/./) do |ch|
5858
case ch
5959
when 'F'
6060
new_pos = pos + Vec2D.from_angle(theta) * draw_length

lsystems/library/cs_grammar/cs_grammar.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def generate(repeat = 0) # repeat iteration grammar rules
4141

4242
def new_production(prod) # single iteration grammar rules
4343
@idx = -1
44-
prod.gsub!(/./) do |ch|
44+
prod.gsub(/./) do |ch|
4545
get_rule(prod, ch)
4646
end
4747
end

lsystems/library/grammar/grammar.rb

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
############################
24
# Simple lsystem grammar
35
############################
@@ -8,23 +10,17 @@ def initialize(axiom, rules)
810
@rules = rules
911
end
1012

11-
def expand(production, iterations, &block)
12-
production.each_char do |token|
13-
if rules.key?(token) && iterations > 0
14-
expand(rules[token], iterations - 1, &block)
15-
else
16-
yield token
17-
end
18-
end
19-
end
20-
21-
def each(gen)
22-
expand(axiom, gen) { |token| yield token }
13+
def apply_rules(prod)
14+
prod.gsub(/./) { |token| rules.fetch(token, token) }
2315
end
2416

2517
def generate(gen)
26-
[].tap do |output|
27-
each(gen) { |token| output << token }
18+
return axiom if gen.zero?
19+
20+
prod = axiom
21+
gen.times do
22+
prod = apply_rules(prod)
2823
end
24+
prod
2925
end
3026
end

lsystems/library/penrose/penrose.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def render
4343
repeated = %w[1 2 3 4]
4444
pen = Turtle.new(pos, theta, RED) # simple Struct for pen, symbol
4545
stack = [] # simple array for stack
46-
production.each do |element|
46+
production.scan(/./) do |element|
4747
case element
4848
when 'F'
4949
pen = draw_line(pen, draw_length)

lsystems/library/snowflake/snowflake.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ class PenroseSnowflake
33

44
attr_accessor :axiom, :grammar, :start_length, :theta, :production,
55
:draw_length, :pos
6-
DELTA = PI / 10
6+
DELTA = 36.radians
77

88
def initialize(pos)
9-
@axiom = 'F3-F3-F3-F3-F'
10-
@grammar = Grammar.new(axiom, 'F' => 'F3-F3-F45-F++F3-F')
9+
@axiom = 'F2-F2-F2-F2-F'
10+
@grammar = Grammar.new(axiom, 'F' => 'F2-F2-F5-F+F2-F')
1111
@start_length = 450.0
1212
@theta = 0
1313
@pos = pos
@@ -21,7 +21,7 @@ def initialize(pos)
2121

2222
def render
2323
repeats = 1
24-
production.each do |element|
24+
production.scan(/./) do |element|
2525
case element
2626
when 'F'
2727
new_pos = pos + Vec2D.from_angle(theta) * draw_length
@@ -34,8 +34,8 @@ def render
3434
when '-'
3535
@theta -= DELTA * repeats
3636
repeats = 1
37-
when '3', '4', '5'
38-
repeats += element.to_i
37+
when '2', '5'
38+
repeats = element.to_i
3939
else
4040
puts "Character '#{element}' is not in grammar"
4141
end

lsystems/mandelbrot.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'picrate'
24
# The Mandelbrot Set
35
# by Daniel Shiffman.
@@ -46,6 +48,7 @@ def setup
4648
b = twoab + y
4749
# Infinity in our finite world is simple, let's just consider it 16
4850
break if dist(aa, bb, 0, 0) > 4.0 # Bail
51+
4952
n += 1
5053
end
5154
# We color each pixel based on how long it takes to get to infinity

lsystems/moore_curve.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def generate(gen)
6565

6666
def translate_rules(prod)
6767
[].tap do |pts| # An array to store line vertices as Vec2D
68-
prod.each do |ch|
68+
prod.scan(/./) do |ch|
6969
case ch
7070
when 'F'
7171
pts << vec

lsystems/mpeano.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def generate(gen)
6262

6363
def translate_rules(prod)
6464
[].tap do |points| # An empty array to store line vertices
65-
prod.each do |ch|
65+
prod.scan(/./) do |ch|
6666
case ch
6767
when 'F'
6868
points << xpos << ypos << (@xpos -= draw_length * cos(theta)) << (@ypos -= draw_length * sin(theta))

lsystems/peano.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def forward(pos)
6969

7070
def translate_rules(prod)
7171
[].tap do |pts| # An array to store line vertices as Vec2D
72-
prod.each do |ch|
72+
prod.scan(/./) do |ch|
7373
case ch
7474
when 'F'
7575
new_pos = forward(pos)

lsystems/pentagonal.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def make_shape
8585
shape.stroke 200
8686
shape.stroke_weight 3
8787
shape.vertex(pos.x, pos.y)
88-
production.each do |element|
88+
production.scan(/./) do |element|
8989
case element
9090
when 'F'
9191
@pos = forward(pos)

lsystems/snake_kolam.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def setup_grammar
5555

5656
def render # NB not using affine transforms here
5757
turtle = [xpos, ypos, 0]
58-
production.each do |element|
58+
production.scan(/./) do |element|
5959
case element
6060
when 'F'
6161
turtle = draw_line(turtle, draw_length)

0 commit comments

Comments
 (0)