Skip to content

Commit f248a7b

Browse files
author
monkstone
committed
modified: samples/processing_app/basics/math/arctangent.rb
modified: samples/processing_app/basics/math/distance1.rb modified: samples/processing_app/basics/math/distance2.rb modified: samples/processing_app/basics/math/noise_1_d.rb modified: samples/processing_app/basics/math/noise_2_d.rb modified: samples/processing_app/basics/math/noise_3_d.rb modified: samples/processing_app/basics/math/noise_wave.rb modified: samples/processing_app/basics/math/operator_precedence.rb modified: samples/processing_app/basics/math/polar_to_cartesian.rb modified: samples/processing_app/basics/math/random.rb modified: samples/processing_app/basics/math/sine_wave.rb
1 parent a4341c8 commit f248a7b

File tree

11 files changed

+161
-185
lines changed

11 files changed

+161
-185
lines changed

samples/processing_app/basics/math/arctangent.rb

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
1-
# Move the mouse to change the direction of the eyes.
2-
# The atan2() function computes the angle from each eye
3-
# to the cursor.
1+
# Move the mouse to change the direction of the eyes.
2+
# The atan2() function computes the angle from each eye
3+
# to the cursor.
4+
attr_reader :eyes
45

56
def setup
67
size 640, 360
78
@eyes = [
8-
Eye.new(420, 230, 220),
9-
Eye.new(250, 16, 120),
10-
Eye.new(164, 185, 80)
9+
Eye.new(420, 230, 220),
10+
Eye.new(250, 16, 120),
11+
Eye.new(164, 185, 80)
1112
]
1213
no_stroke
1314
end
1415

1516
def draw
1617
background 102
17-
18-
@eyes.each do |eye|
18+
eyes.each do |eye|
1919
eye.update mouse_x, mouse_y
20-
eye.display self
20+
eye.display
2121
end
2222
end
2323

24+
# Using Processing::Proxy to mimic java inner class access
2425
class Eye
26+
include Processing::Proxy
27+
28+
attr_reader :angle, :x, :y, :size
2529
def initialize(x, y, sz) # contructor, called by Eye.new
2630
@x, @y, @size = x, y, sz
2731
end
28-
32+
2933
def update(mx, my)
30-
@angle = atan2(my - @y, mx - @x)
34+
@angle = atan2(my - y, mx - x)
3135
end
32-
33-
def display( context )
34-
context.push_matrix
35-
context.translate @x, @y
36-
context.fill 255
37-
context.ellipse 0, 0, @size, @size
38-
context.rotate @angle
39-
context.fill 153
40-
context.ellipse @size/4, 0, @size/2, @size/2
41-
context.pop_matrix
36+
37+
def display
38+
push_matrix
39+
translate x, y
40+
fill 255
41+
ellipse 0, 0, size, size
42+
rotate angle
43+
fill 153
44+
ellipse size / 4, 0, size / 2, size / 2
45+
pop_matrix
4246
end
4347
end
44-
Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,35 @@
1-
# Distance 1D.
2-
#
3-
# Move the mouse left and right to control the
4-
# speed and direction of the moving shapes.
1+
# Distance 1D.
2+
#
3+
# Move the mouse left and right to control the
4+
# speed and direction of the moving shapes.
55

6-
7-
def setup
6+
def setup
87
size 640, 360
98
no_stroke
109
frame_rate 60
11-
1210
@thin = 8
1311
@thick = 36
1412
@xpos1 = width / 2
1513
@xpos2 = width / 2
1614
@xpos3 = width / 2
1715
@xpos4 = width / 2
18-
1916
end
2017

2118
def draw
22-
2319
background 0
24-
2520
mx = mouse_x * 0.4 - width / 5.0
26-
2721
fill 102
28-
rect @xpos2, 0, @thick, height/2
29-
22+
rect @xpos2, 0, @thick, height / 2
3023
fill 204
31-
rect @xpos1, 0, @thin, height/2
32-
24+
rect @xpos1, 0, @thin, height / 2
3325
fill 102
34-
rect @xpos4, height/2, @thick, height/2
35-
26+
rect @xpos4, height / 2, @thick, height / 2
3627
fill 204
37-
rect @xpos3, height/2, @thin, height/2
38-
39-
@xpos1 += mx/16
40-
@xpos2 += mx/64
41-
@xpos3 -= mx/16
42-
@xpos4 -= mx/64
43-
28+
rect @xpos3, height / 2, @thin, height / 2
29+
@xpos1 += mx / 16
30+
@xpos2 += mx / 64
31+
@xpos3 -= mx / 16
32+
@xpos4 -= mx / 64
4433
@xpos1 = width if @xpos1 < -@thin
4534
@xpos1 = -@thin if @xpos1 > width
4635
@xpos2 = width if @xpos2 < -@thick
@@ -49,5 +38,4 @@ def draw
4938
@xpos3 = -@thin if @xpos3 > width
5039
@xpos4 = width if @xpos4 < -@thick
5140
@xpos4 = -@thick if @xpos4 > width
52-
5341
end
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
# Distance 2D.
2-
#
3-
# Move the mouse across the image to obscure and reveal the matrix.
1+
# Distance 2D.
2+
#
3+
# Move the mouse across the image to obscure and reveal the matrix.
44
# Measures the distance from the mouse to each square and sets the
5-
# size proportionally.
5+
# size proportionally.
66

7-
def setup
8-
size 640, 360
7+
def setup
8+
size 640, 360
99
no_stroke
10-
@max_distance = dist(0, 0, width, height)
10+
@max_distance = dist(0, 0, width, height)
1111
end
1212

13-
def draw
14-
background 51
15-
(0..width).step(20) do |i|
16-
(0..height).step(20) do |j|
17-
size = dist(mouse_x, mouse_y, i, j)
18-
size = size / @max_distance * 66
13+
def draw
14+
background 51
15+
(0..width).step(20) do |i|
16+
(0..height).step(20) do |j|
17+
size = dist(mouse_x, mouse_y, i, j) / @max_distance * 66
1918
ellipse i, j, size, size
2019
end
21-
end
20+
end
2221
end
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# Noise1D.
2-
#
3-
# Using 1D Perlin Noise to assign location.
1+
# Noise1D.
2+
#
3+
# Using 1D Perlin Noise to assign location.
44

5-
6-
def setup
7-
size 640, 360
5+
def setup
6+
size 640, 360
87
@xoff = 0.0
9-
@x_increment = 0.01
8+
@x_increment = 0.01
109
background 0
11-
no_stroke
10+
no_stroke
1211
end
1312

14-
def draw
13+
def draw
1514
fill 0, 10
16-
rect 0, 0, width, height
17-
n = noise(@xoff) * width
18-
@xoff += @x_increment
15+
rect 0, 0, width, height
16+
n = noise(@xoff) * width
17+
@xoff += @x_increment
1918
fill 200
20-
ellipse n, height/2, 64, 64
19+
ellipse n, height / 2, 64, 64
2120
end
Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
# Noise2D
2-
# by Daniel Shiffman.
3-
#
4-
# Using 2D noise to create simple texture.
1+
# Noise2D
2+
# by Daniel Shiffman.
3+
#
4+
# Using 2D noise to create simple texture.
55

6-
7-
def setup
8-
size 640, 360
9-
@increment = 0.02
6+
def setup
7+
size 640, 360
8+
@increment = 0.02
109
end
1110

12-
def draw
13-
background 0
14-
load_pixels
15-
xoff = 0.0
16-
detail = map(mouse_x, 0, width, 0.1, 0.6)
11+
def draw
12+
background 0
13+
load_pixels
14+
xoff = 0.0
15+
detail = map1d(mouse_x, (0..width), (0.1..0.6))
1716
noise_detail(8, detail)
18-
(0...width).each { |x|
17+
(0...width).each do |x|
1918
xoff += @increment
20-
yoff = 0.0
21-
(0...height).each { |y|
22-
yoff += @increment
23-
bright = noise(xoff, yoff) * 255
24-
pixels[x+y*width] = color bright
25-
}
26-
}
27-
update_pixels
19+
yoff = 0.0
20+
(0...height).each do |y|
21+
yoff += @increment
22+
bright = noise(xoff, yoff) * 255
23+
pixels[x + y * width] = color(bright)
24+
end
25+
end
26+
update_pixels
2827
end
29-
30-
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
# Noise3D.
2-
#
3-
# Using 3D noise to create simple animated texture.
1+
# Noise3D.
2+
#
3+
# Using 3D noise to create simple animated texture.
44
# Here, the third dimension ('z') is treated as time.
55
attr_reader :increment, :z_increment
66

7-
def setup
7+
def setup
88
size 640, 360
9-
frame_rate 30
9+
frame_rate 30
1010
@increment = 0.01
1111
@zoff = 0.0
12-
@z_increment = 0.02
12+
@z_increment = 0.02
1313
end
1414

15-
def draw
16-
background 0
17-
load_pixels
18-
xoff = 0.0
19-
(0...width).each do |x|
15+
def draw
16+
background 0
17+
load_pixels
18+
xoff = 0.0
19+
(0...width).each do |x|
2020
xoff += increment
21-
yoff = 0.0
22-
(0...height).each do |y|
23-
yoff += increment
24-
bright = noise( xoff, yoff, @zoff ) * 255
21+
yoff = 0.0
22+
(0...height).each do |y|
23+
yoff += increment
24+
bright = noise(xoff, yoff, @zoff) * 255
2525
pixels[x + y * width] = color(bright, bright, bright)
2626
end
27-
end
28-
update_pixels
29-
@zoff += z_increment
27+
end
28+
update_pixels
29+
@zoff += z_increment
3030
end

samples/processing_app/basics/math/noise_wave.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
#
22
# Noise Wave
3-
# by Daniel Shiffman.
4-
#
5-
# Using Perlin Noise to generate a wave-like pattern.
3+
# by Daniel Shiffman.
64
#
5+
# Using Perlin Noise to generate a wave-like pattern.
6+
# Note the use of ruby-processing map1d in place of processing "map"
77

88
attr_reader :yoff # 2nd dimension of perlin noise
99

1010
def setup
1111
size(640, 360)
12-
@yoff = 0.0
12+
@yoff = 0.0
1313
end
1414

1515
def draw
1616
background(51)
17-
1817
fill(255)
1918
# We are going to draw a polygon out of the wave points
20-
begin_shape
21-
19+
begin_shape
2220
xoff = 0 # Option #1: 2D Noise
2321
# xoff = yoff # Option #2: 1D Noise
24-
2522
# Iterate over horizontal pixels
2623
(0..width).step(10) do |x|
27-
# Calculate a y value according to noise, map to
28-
y = map(noise(xoff, yoff), 0, 1, 200,300) # Option #1: 2D Noise
29-
# y = map(noise(xoff), 0, 1, 200,300) # Option #2: 1D Noise
30-
24+
# Calculate a y value according to noise, map to
25+
y = map1d(noise(xoff, yoff), (0..1.0), (200..300)) # Option #1: 2D Noise
26+
# y = map1d(noise(xoff), (0..1.0), (200..300)) # Option #2: 1D Noise
3127
# Set the vertex
32-
vertex(x, y)
28+
vertex(x, y)
3329
# Increment x dimension for noise
3430
xoff += 0.05
3531
end

0 commit comments

Comments
 (0)