Skip to content

Commit 52b0d60

Browse files
committed
various
1 parent 409059b commit 52b0d60

File tree

14 files changed

+342
-354
lines changed

14 files changed

+342
-354
lines changed

contributed/25_squares.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ def draw
2626
# calculate the size of each square for the given number of squares and gap between them
2727
cellsize = (width - (grid_size + 1) * gap) / grid_size
2828
position = -> (count) { gap * (count + 1) + cellsize * count + rand(-5..5) }
29-
grid_size.times do |i|
30-
grid_size.times do |j|
31-
# Note how to create transparent fill with web color JRubyArt
32-
rand(0..5) > 4 ? fill(color('#a11220'), 180.0) : fill(color('#884444'), 180.0)
33-
rect(position.call(i), position.call(j), cellsize, cellsize)
34-
end
29+
grid(grid_size, grid_size) do |i, j|
30+
# Note how to create transparent fill with web color JRubyArt
31+
rand(0..5) > 4 ? fill(color('#a11220'), 180.0) : fill(color('#884444'), 180.0)
32+
rect(position.call(i), position.call(j), cellsize, cellsize)
3533
end
3634
# save your drawings when you press keyboard 's' continually
3735
save_frame('######.jpg') if key_pressed? && key == 's'

contributed/circles.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def settings
1616
size(800, 600, P3D)
1717
end
1818

19-
## To be overriden by the Presentation Code.
2019
def setup
2120
sketch_title 'Circles'
2221
color_mode(HSB, 360, 100, 100, 100)

contributed/plain.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# frozen_string_literal: true
33
require 'propane'
44

5+
# Demonstrates use of the 4 parameter grid convenience method, where parameters
6+
# 3 and 4 represent the size of the grid cell
57
class Plain < Propane::App
68

79
attr_reader :dim, :rnd
@@ -15,12 +17,10 @@ def draw
1517
background 0
1618
fill 120, 160, 220
1719
stroke 0
18-
rect_mode(CENTER)
19-
(dim..width - dim).step(dim) do |x|
20-
(dim..height - dim).step(dim) do |y|
21-
rect x, y, dim, dim, rnd, rnd, rnd, rnd
22-
end
20+
grid(width / dim, height / dim, dim, dim) do |i, j|
21+
rect i, j, dim, dim, rnd, rnd, rnd, rnd
2322
end
23+
no_loop
2424
end
2525

2626
def settings
Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
13
require 'toxiclibs'
24
require_relative 'attractor'
35
require_relative 'particle'
6+
# Verlet physics example
7+
class AttractRepel < Propane::App
48

5-
attr_reader :particles, :attractor, :physics
9+
attr_reader :particles, :attractor, :physics
610

7-
def settings
8-
size 640, 360
9-
end
11+
def settings
12+
size 640, 360
13+
end
1014

11-
def setup
12-
sketch_title "Attract Repel"
13-
@physics = Physics::VerletPhysics2D.new
14-
physics.setDrag(0.01)
15-
@particles = (0..50).map { Particle.new(TVec2D.new(rand(width), rand(height))) }
16-
@attractor = Attractor.new(TVec2D.new(width / 2, height / 2))
17-
end
15+
def setup
16+
sketch_title 'Attract Repel'
17+
@physics = Physics::VerletPhysics2D.new
18+
physics.setDrag(0.01)
19+
@particles = (0..50).map { Particle.new(TVec2D.new(rand(width), rand(height))) }
20+
@attractor = Attractor.new(TVec2D.new(width / 2, height / 2))
21+
end
1822

19-
def draw
20-
background(255)
21-
physics.update
22-
attractor.display
23-
particles.each(&:display)
24-
if mouse_pressed?
25-
attractor.lock
26-
attractor.set(mouse_x, mouse_y)
27-
else
28-
attractor.unlock
23+
def draw
24+
background(255)
25+
physics.update
26+
attractor.display
27+
particles.each(&:display)
28+
if mouse_pressed?
29+
attractor.lock
30+
attractor.set(mouse_x, mouse_y)
31+
else
32+
attractor.unlock
33+
end
2934
end
3035
end
36+
37+
AttractRepel.new

external_library/gem/toxiclibs/simulation/gray_scott_image.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env jruby
2-
# coding: utf-8
32
require 'propane'
43
require 'toxiclibs'
54
# <p>GrayScottImage uses the seedImage() method to use a bitmap as simulation seed.
@@ -62,7 +61,7 @@ def draw
6261
update_pixels
6362
end
6463

65-
def key_pressed
64+
def key_pressed
6665
case key
6766
when *KEYS
6867
@gs.setF(0.02 + key.to_i * 0.001)

external_library/gem/toxiclibs/spherical_harmonics_mesh.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class SphericalHarmonicsSketch < Propane::App
1616
def setup
1717
sketch_title 'Spherical Harmonics Mesh Builder'
1818
Processing::ArcBall.init(self)
19-
@param = [8, 4, 1, 5, 1, 4, 0, 0] # default function parameters (m0..m7)
19+
# default function parameters (m0..m7)
20+
@param = [8, 4, 1, 5, 1, 4, 0, 0].freeze
2021
@mesh = spherical_mesh(param)
2122
@gfx = Gfx::MeshToVBO.new(self) # Mesh to vertex buffer object converter
2223
no_stroke

processing_app/basics/arrays/array.rb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
#!/usr/bin/env jruby
22
require 'propane'
3-
4-
class Array < Propane::App
53
# An array is a list of data. Each piece of data in an array
64
# is identified by an index number representing its position in
75
# the array. Arrays are zero based, which means that the first
86
# element in the array is [0], the second element is [1], and so on.
97
# In this example, an array named "coswave" is created using ruby
108
# map with the cosine values. This data is displayed three
119
# separate ways on the screen.
12-
attr_reader :coswave
10+
class Array < Propane::App
1311

14-
def setup
15-
sketch_title 'Array'
16-
@coswave = (0..width).map do |i|
17-
Math.cos(map1d(i, (0..width), (0..PI))).abs
18-
end
19-
end
12+
attr_reader :coswave
2013

21-
def draw
22-
coswave.each_with_index do |val, i|
23-
stroke(val * 255)
24-
line(i, 0, i, height / 3)
25-
stroke(val * 255 / 4)
26-
line(i, height / 3, i, height / 3 * 2)
27-
stroke(255 - val * 255)
28-
line(i, height / 3 * 2, i, height)
29-
end
30-
end
14+
def setup
15+
sketch_title 'Array'
16+
@coswave = (0..width).map do |i|
17+
Math.cos(map1d(i, (0..width), (0..PI))).abs
18+
end
19+
end
3120

32-
def settings
33-
size 640, 360
34-
end
21+
def draw
22+
coswave.each_with_index do |val, i|
23+
stroke(val * 255)
24+
line(i, 0, i, height / 3)
25+
stroke(val * 255 / 4)
26+
line(i, height / 3, i, height / 3 * 2)
27+
stroke(255 - val * 255)
28+
line(i, height / 3 * 2, i, height)
29+
end
30+
end
31+
32+
def settings
33+
size 640, 360
34+
end
3535
end
3636

37-
Array.new
37+
Array.new

processing_app/basics/arrays/array_2d.rb

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,41 @@
22
require 'propane'
33

44
class Array2d < Propane::App
5-
# Demonstrates the syntax for creating a two-dimensional (2D) array in ruby
6-
# Values in a 2D array are accessed through two index values.
7-
# 2D arrays are useful for storing images. In this example, each dot
8-
# is colored in relation to its distance from the center of the image.
5+
# Demonstrates the use of :grid to create a two-dimensional (2D) array in ruby
6+
# Values in a 2D array are accessed through two index values.
7+
# 2D arrays are useful for storing images. In this example, each dot
8+
# is colored in relation to its distance from the center of the image.
99

10-
attr_reader :distances
10+
attr_reader :distances
1111

12-
def setup
13-
sketch_title 'Array 2D'
14-
@distances = Array.new(width) { Array.new(height) }
15-
stroke_weight 2
16-
max_distance = dist(width / 2, height / 2, width, height)
17-
width.times do |x|
18-
height.times do |y|
19-
distance = dist(width / 2, height / 2, x, y)
20-
distances[x][y] = distance / max_distance * 255
21-
end
22-
end
23-
end
12+
def setup
13+
sketch_title 'Array 2D'
14+
@distances = Array.new(width) { Array.new(height) }
15+
stroke_weight 2
16+
max_distance = dist(width / 2, height / 2, width, height)
17+
grid(width, height) do |x, y|
18+
distance = dist(width / 2, height / 2, x, y)
19+
distances[x][y] = distance / max_distance * 255
20+
end
21+
end
2422

25-
def draw
26-
background 0
27-
x = 0
28-
while x < distances.length
29-
y = 0
30-
while y < distances[x].length
31-
stroke distances[x][y]
32-
point x, y
33-
y += 2
34-
end
35-
x += 2
36-
end
37-
end
23+
def draw
24+
background 0
25+
x = 0
26+
while x < distances.length
27+
y = 0
28+
while y < distances[x].length
29+
stroke distances[x][y]
30+
point x, y
31+
y += 2
32+
end
33+
x += 2
34+
end
35+
end
3836

39-
def settings
40-
size 640, 360
41-
end
37+
def settings
38+
size 640, 360
39+
end
4240
end
4341

44-
Array2d.new
42+
Array2d.new

processing_app/basics/arrays/array_objects.rb

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,67 @@
22
require 'propane'
33

44
class ArrayObjects < Propane::App
5-
# Demonstrates the syntax for creating an array of custom objects.
5+
# Demonstrates the syntax for creating an array of custom objects.
66

7-
UNIT = 40
8-
attr_reader :mods
7+
UNIT = 40
8+
attr_reader :mods
99

10-
def setup
11-
sketch_title 'Array of Objects'
12-
wide_count = width / UNIT
13-
height_count = height / UNIT
14-
@mods = []
15-
wide_count.times do |i|
16-
height_count.times do |j|
17-
mods << CustomObject.new(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
18-
end
19-
end
20-
no_stroke
21-
end
10+
def setup
11+
sketch_title 'Array of Objects'
12+
wide_count = width / UNIT
13+
height_count = height / UNIT
14+
@mods = []
15+
grid(wide_count, height_count) do |i, j|
16+
mods << CustomObject.new(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
17+
end
18+
no_stroke
19+
end
2220

23-
def draw
24-
background 0
25-
mods.each(&:run)
26-
end
21+
def draw
22+
background 0
23+
mods.each(&:run)
24+
end
2725

28-
def settings
29-
size 640, 360, FX2D
30-
end
26+
def settings
27+
size 640, 360, FX2D
28+
end
3129

32-
module Runnable
33-
def run
34-
update
35-
draw
36-
end
37-
end
30+
module Runnable
31+
def run
32+
update
33+
draw
34+
end
35+
end
3836

39-
# the custom object
40-
class CustomObject
41-
include Processing::Proxy, Runnable
42-
attr_reader :x, :y, :mx, :my, :size
43-
def initialize(mx, my, x, y, speed)
44-
@mx, @my = my, mx # This is backwards to match original example.
45-
@x, @y = x.to_i, y.to_i
46-
@xdir, @ydir = 1, 1
47-
@speed = speed
48-
@size = UNIT
49-
end
37+
# the custom object
38+
class CustomObject
39+
include Propane::Proxy, Runnable
40+
attr_reader :x, :y, :mx, :my, :size
41+
def initialize(mx, my, x, y, speed)
42+
@mx, @my = my, mx # This is backwards to match original example.
43+
@x, @y = x.to_i, y.to_i
44+
@xdir, @ydir = 1, 1
45+
@speed = speed
46+
@size = UNIT
47+
end
5048

51-
def update
52-
@x += @speed * @xdir
53-
unless (0..size).cover? x
54-
@xdir *= -1
55-
@x += @xdir
56-
@y += @ydir
57-
end
58-
return unless @y >= @size || x <= 0
59-
@ydir *= -1
60-
@y += @ydir
61-
end
49+
def update
50+
@x += @speed * @xdir
51+
unless (0..size).cover? x
52+
@xdir *= -1
53+
@x += @xdir
54+
@y += @ydir
55+
end
56+
return unless @y >= @size || x <= 0
57+
@ydir *= -1
58+
@y += @ydir
59+
end
6260

63-
def draw
64-
fill(255)
65-
ellipse(mx + x, my + y, 6, 6)
66-
end
67-
end
61+
def draw
62+
fill(255)
63+
ellipse(mx + x, my + y, 6, 6)
64+
end
65+
end
6866
end
6967

70-
ArrayObjects.new
68+
ArrayObjects.new

0 commit comments

Comments
 (0)