Skip to content

Commit a4341c8

Browse files
author
monkstone
committed
modified: samples/processing_app/basics/math/double_random.rb
modified: samples/processing_app/basics/math/increment_decrement.rb modified: samples/processing_app/basics/math/polar_to_cartesian.rb
1 parent caa3b90 commit a4341c8

File tree

3 files changed

+37
-63
lines changed

3 files changed

+37
-63
lines changed
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
# Double Random
2-
# by Ira Greenberg.
3-
#
4-
# Using two rand(..) calls and the point() function to
5-
# create an irregular sawtooth line. The noise line guards
6-
# against an illegal input to rand range when min > max,
7-
# and to return min as vanilla processing does
1+
# Double Random
2+
# by Ira Greenberg.
3+
#
4+
# Using two rand(..) calls and the point() function to
5+
# create an irregular sawtooth line. The noise line guards
6+
# against an illegal input to rand range when min > max,
7+
# and to return min as vanilla processing does
88
# (NB: the sawtooth relies on this)
99

1010
attr_reader :steps
1111

12-
def setup
13-
size 640, 360
14-
frame_rate(1)
15-
total_pts = 300
12+
def setup
13+
size 640, 360
14+
frame_rate(1)
15+
total_pts = 300
1616
@steps = total_pts + 1.0
1717
stroke_weight 2
18-
stroke 255
18+
stroke 255
1919
end
2020

2121
def draw
2222
background 0
23-
rand_y = 0.0
24-
(1 ... steps).each do |i|
25-
noise_y = (rand_y > 0)? rand(-rand_y..rand_y) : rand(rand_y..-rand_y)
26-
point( (width/steps) * i, (height/2) + noise_y )
27-
rand_y += rand(-5..5)
23+
rand_y = 0.0
24+
(1...steps).each do |i|
25+
noise_y = (rand_y > 0) ? rand(-rand_y..rand_y) : rand(rand_y..-rand_y)
26+
point((width / steps) * i, (height / 2) + noise_y)
27+
rand_y += rand(-5..5.0)
2828
end
2929
end
30-
31-
Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,31 @@
1-
# Increment Decrement.
2-
#
1+
# Increment Decrement.
2+
#
33
# In Java and C ...
4-
# Writing "a++" is equivalent to "a = a + 1".
5-
# Writing "a--" is equivalent to "a = a - 1".
4+
# Writing "a++" is equivalent to "a = a + 1".
5+
# Writing "a--" is equivalent to "a = a - 1".
66

77
# In Ruby there is += 1 and -= 1:
88
# a += 1 is equal to a = a + 1
99

10-
1110
def setup
12-
1311
size 640, 360
14-
1512
color_mode RGB, width
16-
1713
@a = 0
1814
@b = width
19-
@direction = false
20-
15+
@direction = true
2116
frame_rate 30
22-
2317
end
2418

2519
def draw
26-
2720
@a += 1
28-
2921
if @a > width
3022
@a = 0
3123
@direction = !@direction
3224
end
33-
34-
if @direction
35-
stroke @a
36-
else
37-
stroke width-@a
38-
end
39-
40-
line @a, 0, @a, height/2
41-
25+
stroke(@direction ? width - @a : @a)
26+
line @a, 0, @a, height / 2
4227
@b -= 1
43-
4428
@b = width if @b < 0
45-
46-
if @direction
47-
stroke width-@b
48-
else
49-
stroke @b
50-
end
51-
52-
line @b, height/2+1, @b, height
53-
29+
stroke(@direction ? width - @b : @b)
30+
line @b, height / 2 + 1, @b, height
5431
end
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
# PolarToCartesian
2-
# by Daniel Shiffman.
3-
#
4-
# Convert a polar coordinate (r,theta) to cartesian (x,y):
2+
# by Daniel Shiffman.
3+
#
4+
# Convert a polar coordinate (r,theta) to cartesian (x,y):
55
# x = r * cos(theta)
66
# y = r * sin(theta)
77
attr_reader :r, :theta
88

9-
def setup
10-
size 640, 360
9+
def setup
10+
size 640, 360
1111
frame_rate 30
1212
@r = height * 0.45
1313
@theta = 0.0
1414
@theta_vel = 0.0
1515
@theta_acc = 0.1e-3
1616
end
1717

18-
def draw
19-
background 0
20-
translate width/2, height/2
18+
def draw
19+
background 0
20+
translate width / 2, height / 2
2121
# Convert polar to cartesian
2222
x = r * cos(theta)
23-
y = r * sin(theta)
23+
y = r * sin(theta)
2424
ellipse_mode CENTER
2525
no_stroke
2626
fill 200
27-
ellipse x, y, 32, 32
27+
ellipse x, y, 32, 32
2828
@theta_vel += @theta_acc
29-
@theta += @theta_vel
29+
@theta += @theta_vel
3030
end
31-

0 commit comments

Comments
 (0)