Skip to content

Commit 5076121

Browse files
committed
promote grid convenience method
1 parent a769c74 commit 5076121

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

contributed/25_squares.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# # # # #
55
# Interpretation by Martin Vögeli
66
# Converted to propane Martin Prout
7+
# features the :grid convenience method
78
# # # # #
89
# Based on code by Indae Hwang and Jon McCormack
910
def settings
@@ -24,12 +25,10 @@ def draw
2425
# calculate the size of each square for the given number of squares and gap between them
2526
cellsize = (width - (grid_size + 1) * gap) / grid_size
2627
position = -> (count) { gap * (count + 1) + cellsize * count + rand(-5..5) }
27-
grid_size.times do |i|
28-
grid_size.times do |j|
29-
# Note how to create transparent fill with web color JRubyArt
30-
rand(0..5) > 4 ? fill(color('#a11220'), 180.0) : fill(color('#884444'), 180.0)
31-
rect(position.call(i), position.call(j), cellsize, cellsize)
32-
end
28+
grid(grid_size, grid_size) do |i, j| # the grid convenience method takes a block
29+
# Note how to create transparent fill with web color JRubyArt
30+
rand(0..5) > 4 ? fill(color('#a11220'), 180.0) : fill(color('#884444'), 180.0)
31+
rect(position.call(i), position.call(j), cellsize, cellsize)
3332
end
3433
# save your drawings when you press keyboard 's' continually
3534
save_frame('######.jpg') if key_pressed? && key == 's'

processing_app/basics/arrays/array_2d.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ def setup
1010
@distances = Array.new(width) { Array.new(height) }
1111
stroke_weight 2
1212
max_distance = dist(width / 2, height / 2, width, height)
13-
width.times do |x|
14-
height.times do |y|
15-
distance = dist(width / 2, height / 2, x, y)
16-
distances[x][y] = distance / max_distance * 255
17-
end
13+
grid(width, height) do |x, y|
14+
distance = dist(width / 2, height / 2, x, y)
15+
distances[x][y] = distance / max_distance * 255
1816
end
1917
end
2018

processing_app/basics/arrays/array_objects.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ def setup
88
wide_count = width / UNIT
99
height_count = height / UNIT
1010
@mods = []
11-
wide_count.times do |i|
12-
height_count.times do |j|
13-
mods << CustomObject.new(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
14-
end
11+
grid(wide_count, height_count) do |i, j|
12+
mods << CustomObject.new(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
1513
end
1614
no_stroke
1715
end

processing_app/basics/arrays/custom_array.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ def setup
88
wide_count = width / UNIT
99
height_count = height / UNIT
1010
@custom_array = CustomArray.new
11-
height_count.times do |i|
12-
wide_count.times do |j|
13-
custom_array.add_object(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
14-
end
11+
grid(wide_count, height_count) do |i, j|
12+
custom_array.add_object(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
1513
end
1614
no_stroke
1715
end
@@ -33,19 +31,19 @@ def settings
3331

3432
# The custom Array created using Forwardable
3533
# Processing::Proxy gives access to PApplet methods
36-
class CustomArray
34+
class CustomArray
3735
extend Forwardable
3836
def_delegators(:@objs, :each, :<<)
3937
include Enumerable, Processing::Proxy
40-
38+
4139
def initialize
4240
@objs = []
4341
end
44-
42+
4543
def add_object(mx, my, x, y, speed)
4644
self << Particle.new(x.to_i, y.to_i, mx, my, UNIT, speed, 1, 1)
4745
end
48-
46+
4947
def update
5048
each do |obj|
5149
update_x obj
@@ -54,15 +52,15 @@ def update
5452
obj.y += obj.ydir
5553
end
5654
end
57-
55+
5856
def update_x(obj)
5957
obj.x += obj.speed * obj.xdir
6058
return if (0..UNIT).cover? obj.x
6159
obj.xdir *= -1
6260
obj.x += obj.xdir
6361
obj.y += obj.ydir
6462
end
65-
63+
6664
def draw
6765
background(0)
6866
fill(255)
@@ -71,4 +69,3 @@ def draw
7169
end
7270
end
7371
end
74-

processing_app/topics/cellular_automata/game_of_life.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ def setup
3434
def draw
3535
background(0)
3636
# Draw live cells
37-
row.times do |x|
38-
column.times do |y|
39-
if cells[x][y]
40-
fill(alive)
41-
rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
42-
end
37+
grid(row, column) do |x, y|
38+
if cells[x][y]
39+
fill(alive)
40+
rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
4341
end
4442
end
4543
# Iterate if timer ticks
@@ -88,9 +86,9 @@ def tick! # When the clock ticks
8886
(x - 1..x + 1).each do |xx|
8987
(y - 1..y + 1).each do |yy|
9088
# Make sure you are not out of bounds
91-
next unless [(xx >= 0), (xx < row), (yy >= 0), (yy < column)].all?
89+
next unless [(xx >= 0), (xx < row), (yy >= 0), (yy < column)].all?
9290
# Make sure to check against self
93-
next if [(xx == x), (yy == y)].all?
91+
next if [(xx == x), (yy == y)].all?
9492
# Check alive neighbours and count them
9593
neighbours += 1 if cells_buffer[xx][yy]
9694
end # End of yy loop

0 commit comments

Comments
 (0)