Skip to content

Commit caa3b90

Browse files
author
monkstone
committed
modified: samples/processing_app/basics/data/variable_scope.rb
modified: samples/processing_app/basics/data/variables.rb modified: samples/processing_app/basics/math/additive_wave.rb modified: samples/processing_app/basics/math/graphing_2_d_equation.rb modified: samples/processing_app/basics/math/interpolate.rb modified: samples/processing_app/basics/math/modulo.rb modified: samples/processing_app/basics/math/random_gaussian.rb modified: samples/processing_app/basics/math/sine.rb modified: samples/processing_app/basics/math/sine_cosine.rb
1 parent bd20b97 commit caa3b90

File tree

9 files changed

+100
-152
lines changed

9 files changed

+100
-152
lines changed
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,54 @@
1-
# Variables may either have a global or local "scope".
1+
# Variables may either have a global or local "scope".
22
# For example, variables declared within either the
33
# setup or draw functions may be only used in these
44
# functions. Global variables, variables declared outside
55
# of setup and draw, may be used anywhere within the program.
66
# If a local variable is declared with the same name as a
7-
# global variable, the program will use the local variable to make
7+
# global variable, the program will use the local variable to make
88
# its calculations within the current scope. Variables may be localized
99
# within classes, functions, and iterative statements.
1010

11-
# Please note that there are some changes on variable scope inside blocks
12-
# between Ruby versions 1.8 and 1.9.
13-
14-
1511
def setup
1612
background 51
1713
stroke 255
1814
no_loop
19-
20-
@a = 20 # Use "@" before the name to create an instance variable ("@a"),
15+
@a = 20 # Use "@" before the name to create an instance variable ("@a"),
2116
# which will be available anywhere inside this instance of "VariableScope".
2217
end
2318

2419
def draw
25-
# Draw a line using the instance variable "a",
20+
# Draw a line using the instance variable "a",
2621
# as returned by its getter function below
2722
line a, 0, a, height
28-
2923
# Create a new variable "a" local to the block (do-end)
3024
(50..80).step(2) do |a|
3125
line a, 0, a, height
3226
end
33-
3427
# Create a new variable "a" local to the draw method
3528
a = 100
3629
line a, 0, a, height
37-
3830
# Make a call to the custom function draw_another_line
3931
draw_another_line
40-
4132
# Make a call to the custom function draw_yet_another_line
4233
draw_yet_another_line
4334
end
4435

4536
def draw_another_line
4637
# Create a new variable "a" local to this method
4738
a = 185
48-
4939
# Draw a line using the local variable "a"
5040
line a, 0, a, height
5141
end
5242

5343
def draw_yet_another_line
54-
# Because no new local variable "a" is set, this line draws using the
55-
# instance variable "a" (returned by its getter function) which was
44+
# Because no new local variable "a" is set, this line draws using the
45+
# instance variable "a" (returned by its getter function) which was
5646
# set to the value 20 in setup.
57-
line a+2, 0, a+2, height
47+
line a + 2, 0, a + 2, height
5848
end
5949

60-
# This is a "getter" function, it returns the value of the instance variable "a"
50+
# This is a "getter" function, it returns the value of the instance variable "a" which is
51+
# completely pointless in ruby, use instead attr_reader see below
6152
def a
6253
@a
6354
end
@@ -71,4 +62,3 @@ def a
7162

7263
# Or you could just have Ruby add both at once with:
7364
# attr_accessor :a
74-
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
# Variables are used for storing values. In this example, changing
2-
# the values of variables @one and @two significantly changes the composition.
1+
# Variables are used for storing values. In this example, changing
2+
# the values of variables @one and @two significantly changes the composition.
33

44
load_library :control_panel
55
attr_reader :panel
66
def setup
7-
size 200, 200
8-
stroke 153
7+
size 200, 200
8+
stroke 153
99
@one = 20 # Change these with the sliders
10-
@two = 50
10+
@two = 50
1111
control_panel do |c|
1212
c.slider :one, -20..100
1313
c.slider :two, -20..100
1414
@panel = c
15-
end
15+
end
1616
end
1717

18-
1918
def draw
2019
panel.set_visible(true)
21-
background 0
20+
background 0
2221
c = @one * 8
2322
d = @one * 9
2423
e = @two - @one
2524
f = @two * 2
2625
g = f + e
27-
2826
line @one, f, @two, g
2927
line @two, e, @two, g
3028
line @two, e, d, c
31-
line @one, e, d-e, c
29+
line @one, e, d - e, c
3230
end
33-
34-
35-
Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
2-
31
# Additive Wave
4-
# by Daniel Shiffman.
5-
#
6-
# Create a more complex wave by adding two waves together.
2+
# by Daniel Shiffman.
3+
#
4+
# Create a more complex wave by adding two waves together.
75
attr_reader :dx, :amplitude, :max_waves, :y_values
86

97
def setup
108
size 640, 360
11-
@max_waves = 4 # Total number of waves to add together
12-
@wave_width = width + 16 # Width of entire wave
13-
@x_spacing = 8 # How far apart should each horizontal location be spaced
9+
@max_waves = 4 # Total number of waves to add together
10+
@wave_width = width + 16 # Width of entire wave
11+
@x_spacing = 8 # How far apart each horizontal location is spaced
1412
@theta = 0.0
15-
@amplitude = [] # Height of wave
16-
@dx = [] # Value for incrementing X, to be calculated as a function of period and x_spacing
17-
18-
@max_waves.times do |i|
13+
@amplitude = [] # Height of wave
14+
# Value for incrementing X, to be calculated as a function of period and x_spacing
15+
@dx = []
16+
17+
@max_waves.times do
1918
amplitude << rand(10..30)
2019
period = rand(100..300) # How many pixels before the wave repeats
2120
dx << (TAU / period) * @x_spacing
2221
end
23-
22+
2423
frame_rate 30
2524
color_mode RGB, 255, 255, 255, 100
2625
smooth
@@ -35,16 +34,14 @@ def draw
3534
def calculate_wave
3635
# Increment theta (try different values for 'angular velocity' here
3736
@theta += 0.02
38-
3937
# Set all height values to zero
40-
@y_values = Array.new @wave_width/@x_spacing, 0
41-
38+
@y_values = Array.new @wave_width / @x_spacing, 0
4239
# Accumulate wave height values
4340
max_waves.times do |j|
4441
x = @theta
4542
y_values.length.times do |i|
4643
# Every other wave is cosine instead of sine
47-
value = (j % 2) == 0 ? sin(x) : cos(x)
44+
value = j.even? ? sin(x) : cos(x)
4845
y_values[i] += value * amplitude[j]
4946
x += dx[j]
5047
end
@@ -57,8 +54,6 @@ def render_wave
5754
fill 255, 50
5855
ellipse_mode CENTER
5956
y_values.each_with_index do |y, i|
60-
ellipse i*@x_spacing, width/2+y, 16, 16
57+
ellipse i * @x_spacing, width / 2 + y, 16, 16
6158
end
6259
end
63-
64-
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#
22
# Graphing 2D Equations
3-
# by Daniel Shiffman.
4-
#
5-
# Graphics the following equation:
6-
# sin(n*cos(r) + 5*theta)
7-
# where n is a function of horizontal mouse location.
3+
# by Daniel Shiffman.
84
#
9-
5+
# Graphics the following equation:
6+
# sin(n*cos(r) + 5*theta)
7+
# where n is a function of horizontal mouse location.
8+
#
9+
1010
def setup
1111
size(640, 360)
1212
end
@@ -22,18 +22,17 @@ def draw
2222
width.times do |i|
2323
y = -h / 2 # Start y at -1 * height / 2
2424
height.times do |j|
25-
r = sqrt((x * x) + (y * y)) # Convert cartesian to polar
26-
theta = atan2(y, x) # Convert cartesian to polar
25+
r = sqrt((x * x) + (y * y)) # Convert cartesian to polar
26+
theta = atan2(y, x) # Convert cartesian to polar
2727
# Compute 2D polar coordinate function
28-
val = sin(n * cos(r) + 5 * theta) # Results in a value between -1 and 1
29-
#val = cos(r) # Another simple function
30-
#val = sin(theta) # Another simple function
31-
# Map the resulting value to a grayscale value
32-
pixels[i + j * width] = color((val + 1.0) * 255.0/2.0) # Scale to between 0 and 255
33-
y += dy # Increment y
28+
val = sin(n * cos(r) + 5 * theta) # Results in a value between -1 and 1
29+
# val = cos(r) # Another simple function
30+
# val = sin(theta) # Another simple function
31+
# Map the resulting value to a grayscale value. Scale to between 0 and 255
32+
pixels[i + j * width] = color((val + 1.0) * 255.0 / 2.0)
33+
y += dy # Increment y
3434
end
35-
x += dx # Increment x
35+
x += dx # Increment x
3636
end
3737
update_pixels
3838
end
39-

samples/processing_app/basics/math/interpolate.rb

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
#
2-
# Linear Interpolation.
3-
#
4-
# Move the mouse across the screen and the symbol will follow.
5-
# Between drawing each frame of the animation, the ellipse moves
6-
# part of the distance (0.05) from its current position toward
2+
# Linear Interpolation.
3+
#
4+
# Move the mouse across the screen and the symbol will follow.
5+
# Between drawing each frame of the animation, the ellipse moves
6+
# part of the distance (0.05) from its current position toward
77
# the cursor using the lerp() function
88
#
9-
# This is the same as the Easing under input only with lerp() instead.
9+
# This is the same as the Easing under input only with lerp() instead.
1010
#
11-
11+
1212
attr_reader :x, :y
1313

1414
def setup
15-
size(640, 360)
16-
no_stroke
15+
size(640, 360)
16+
no_stroke
17+
@x, @y = width / 2, height / 2
1718
end
1819

19-
def draw
20+
def draw
2021
background(51)
21-
22-
# lerp() calculates a number between two numbers at a specific increment.
23-
# The amt parameter is the amount to interpolate between the two values
24-
# where 0.0 equal to the first point, 0.1 is very near the first point, 0.5
25-
# is half-way in between, etc.
26-
22+
# lerp() calculates a number between two numbers at a specific increment.
23+
# The amt parameter is the amount to interpolate between the two values
24+
# where 0.0 equal to the first point, 0.1 is very near the first point, 0.5
25+
# is half-way in between, etc.
2726
# Here we are moving 5% of the way to the mouse location each frame
2827
@x = lerp(x, mouse_x, 0.05)
2928
@y = lerp(y, mouse_y, 0.05)
30-
3129
fill(255)
3230
stroke(255)
3331
ellipse(x, y, 66, 66)
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
1-
# Modulo.
2-
#
3-
# The modulo operator (%) returns the remainder of a number
4-
# divided by another. As in this example, it is often used
5-
# to keep numerical values within a set range.
6-
1+
# Modulo.
2+
#
3+
# The modulo operator (%) returns the remainder of a number
4+
# divided by another. As in this example, it is often used
5+
# to keep numerical values within a set range.
76

87
def setup
9-
108
size 200, 200
11-
129
@c = 0.0
1310
@num = 20
14-
1511
fill 255
1612
frame_rate 30
17-
1813
end
1914

2015
def draw
21-
2216
background 0
23-
2417
@c += 0.1
25-
26-
(1...(height/@num)).each { |i|
27-
28-
x = (@c %i ) * i * i
18+
(1...height / @num).each do |i|
19+
x = (@c % i) * i * i
2920
stroke 102
30-
31-
line 0, i*@num, x, i*@num
32-
21+
line 0, i * @num, x, i * @num
3322
no_stroke
34-
35-
rect x, i*@num-@num/2, 8, @num
36-
}
37-
23+
rect x, i * @num - @num / 2, 8, @num
24+
end
3825
end
39-
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#
21
# Random Gaussian.
32
#
4-
# This sketch draws ellipses with x and y locations tied to a gaussian distribution of random numbers.
5-
#
3+
# This sketch draws ellipses with x and y locations tied to a gaussian distribution of
4+
# random numbers.
65

76
def setup
87
size(640, 360)
@@ -12,12 +11,10 @@ def setup
1211
def draw
1312
# Get a gaussian random number w/ mean of 0 and standard deviation of 1.0
1413
val = random_gaussian
15-
sd = 60 # Define a standard deviation
16-
mean = width/2 # Define a mean value (middle of the screen along the x-axis)
17-
x = (val * sd) + mean # Scale the gaussian random number by standard deviation and mean
14+
sd = 60 # Define a standard deviation
15+
mean = width / 2 # Define a mean value (middle of the screen along the x-axis)
16+
x = (val * sd) + mean # Scale the gaussian random number by standard deviation and mean
1817
fill(200, 20)
1918
no_stroke
20-
ellipse(x, height/2, 32, 32) # Draw an ellipse at our "normal" random location
19+
ellipse(x, height / 2, 32, 32) # Draw an ellipse at our "normal" random location
2120
end
22-
23-

0 commit comments

Comments
 (0)