Skip to content

Commit 74b1d75

Browse files
author
monkstone
committed
some tidy up
1 parent 1e67f03 commit 74b1d75

File tree

9 files changed

+95
-114
lines changed

9 files changed

+95
-114
lines changed

samples/processing_app/topics/advanced_data/Rakefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ task default: [:demo]
88

99
desc 'demo'
1010
task :demo do
11-
samples_list.shuffle.each{ |sample| run_sample sample }
11+
samples_list.shuffle.each { |sample| run_sample sample }
1212
end
1313

1414
def samples_list
@@ -17,14 +17,14 @@ def samples_list
1717
Dir.glob('*.rb').each do |file|
1818
files << File.join(SAMPLES_DIR, file)
1919
end
20-
return files
20+
files
2121
end
2222

2323
def run_sample(sample_name)
2424
puts "Running #{sample_name}...quit to run next sample"
2525
open("|rp5 --nojruby run #{sample_name}", 'r') do |io|
2626
while l = io.gets
2727
puts(l.chop)
28-
end
28+
end
2929
end
3030
end

samples/processing_app/topics/advanced_data/counting_words.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
# A helper to keep track of word frequency.
12
class Word
2-
# A helper to keep track of word frequency.
33
attr_accessor :word
44
attr_reader :count
5-
def initialize word
5+
def initialize(word)
66
@word = word
77
@count = 1
88
end
@@ -36,13 +36,12 @@ def setup
3636
def draw
3737
background 51
3838
fill 255
39-
s = (tokens[counter] == 'I')? tokens[counter] : tokens[counter].downcase
39+
s = (tokens[counter] == 'I') ? tokens[counter] : tokens[counter].downcase
4040
@counter = (counter + 1) % tokens.length
41-
if (concordance.has_key?(s))
41+
if concordance.key? s
4242
# Get the word object and increase the count
4343
# We access objects from a Hash via its key, the String
44-
w = concordance[s]
45-
w.increment # increment word count
44+
concordance[s].increment # increment word count
4645
else
4746
# Otherwise make a new Word instance and add it to
4847
# the Hash using the word String as the key
@@ -56,23 +55,22 @@ def draw
5655
# Look at each word
5756
concordance.values.each do |w|
5857
# Only display words that appear 3 times
59-
if (w.count > 3) # access word count
58+
if w.count > 3 # access word count
6059
# The size is the count
6160
fsize = constrain(w.count, 0, 100)
6261
text_size(fsize)
6362
text(w.word, x, y)
6463
# Move along the x-axis
6564
x += text_width(w.word) + 1
6665
end
67-
6866
# If x gets to the end, move y
6967
# If y == 0 we are done
70-
if (y == 0)
68+
if y == 0
7169
no_loop
7270
else
73-
if (x > width)
71+
if x > width
7472
x = 0
75-
y = (y < 0)? 0 : y - 100
73+
y = (y < 0) ? 0 : y - 100
7674
end
7775
end
7876
end

samples/processing_app/topics/advanced_data/library/word/word.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
class Word
88
include Processing::Proxy
99
# Store a count for occurences in two different books
10-
attr_reader :count_dracula, :count_franken, :total_count, :word, :position, :width, :height, :speed
10+
attr_reader :count_dracula, :count_franken, :total_count
11+
attr_reader :word, :position, :width, :height, :speed
1112

1213
def initialize(s)
1314
@width, @height = $app.width, $app.height
1415
@count_dracula, @count_franken, @total_count = 0, 0, 0
15-
@position = [rand(width), rand(-height..height*2)]
16+
@position = [rand(width), rand(-height..height * 2)]
1617
@word = s
1718
end
1819

1920
# We will display a word if it appears at least 5 times
2021
# and only in one of the books
2122
def qualify?
22-
return ((count_dracula == total_count || count_franken == total_count) && total_count > 5)
23+
((count_dracula == total_count || count_franken == total_count) && total_count > 5)
2324
end
2425

2526
# Increment the count for Dracula
@@ -40,22 +41,18 @@ def move
4041
@speed = map(total_count, 5, 25, 0.1, 0.4).to_f
4142
@speed = constrain(speed, 0, 10.0)
4243
@position[Y] += speed
43-
44-
if (position[Y] > height*2)
45-
@position[Y] = -height
46-
end
44+
@position[Y] = -height if position[Y] > height * 2
4745
end
4846

49-
5047
# Depending on which book it gets a color
5148
def display
52-
if (count_dracula > 0)
49+
if count_dracula > 0
5350
fill(255)
54-
elsif (count_franken > 0)
51+
elsif count_franken > 0
5552
fill(0)
5653
end
5754
# Its size is also tied to number of occurences
58-
fs = map(total_count,5,25,2,24.0).to_f
55+
fs = map(total_count, 5, 25, 2, 24.0).to_f
5956
fs = constrain(fs, 2, 48)
6057
text_size(fs)
6158
text_align(CENTER)

samples/processing_app/topics/advanced_data/load_save_XML.rb

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ def draw
3636
# Display all bubbles
3737
bubbles.each do |b|
3838
b.display
39-
b.rollover(mouseX, mouseY)
39+
b.rollover(mouse_x, mouse_y)
4040
end
41-
4241
text_align(LEFT)
4342
fill(0)
4443
text('Click to add bubbles.', 10, height - 10)
@@ -49,65 +48,49 @@ def load_data
4948
@xml = loadXML('data.xml')
5049
# Get all the child nodes named 'bubble'
5150
children = xml.get_children('bubble')
52-
53-
# The size of the array of Bubble objects is determined by the total XML elements named 'bubble'
51+
# The size of the array of Bubble objects is determined by the
52+
# total XML elements named 'bubble'
5453
@bubbles = []
55-
5654
children.each do |element|
5755
# The position element has two attributes: x and y
5856
position_element = element.get_child('position')
5957
# Note how with attributes we can get an integer or directly
6058
x, y = position_element.get_int('x'), position_element.get_int('y')
61-
6259
# The diameter is the content of the child named 'diamater'
6360
diameter_element = element.get_child('diameter')
64-
# Note how with the content of an XML node, we retrieve as a String and then convert
61+
# Note how with the content of an XML node, we retrieve as a
62+
# String and then convert
6563
diameter = (diameter_element.get_content).to_f
66-
6764
# The label is the content of the child named 'label'
6865
label_element = element.get_child('label')
6966
label = label_element.get_content
70-
7167
# Make a Bubble object out of the data read
7268
bubbles << Bubble.new(x, y, diameter, label)
7369
end
74-
7570
end
7671

7772
# Still need to work on adding and deleting
78-
7973
def mouse_pressed
80-
8174
# Create a new XML bubble element
8275
bubble = xml.add_child('bubble')
83-
8476
# Set the poisition element
8577
position = bubble.add_child('position')
8678
# Here we can set attributes as integers directly
8779
position.set_int('x', mouseX)
8880
position.set_int('y', mouseY)
89-
9081
# Set the diameter element
9182
diameter = bubble.add_child('diameter')
9283
# Here for a node's content, we have to convert to a String
9384
diameter.set_content(rand(40.0..80).to_s)
94-
9585
# Set a label
9686
label = bubble.add_child('label')
9787
label.set_content('New label')
98-
99-
10088
# Here we are removing the oldest bubble if there are more than 10
10189
children = xml.get_children('bubble')
102-
# If the XML file has more than 10 bubble elements
103-
if (children.length > 10)
104-
# Delete the first one
105-
xml.remove_child(children[0])
106-
end
107-
90+
# If the XML file has more than 10 bubble elements
91+
xml.remove_child(children[0]) if children.length > 10
10892
# Save a new XML file
10993
saveXML(xml, 'data/data.xml')
110-
11194
# reload the new data
11295
load_data
113-
end
96+
end

samples/processing_app/topics/advanced_data/regex.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
def setup
1313
size(360, 480)
14-
@url = 'http://processing.org'
14+
@url = 'https://processing.org'
1515
# Load the links
1616
@links = load_links(url)
1717
links.uniq! # get rid of the duplicates
@@ -43,4 +43,4 @@ def load_links(s)
4343
[\w=]*
4444
)?
4545
/ix)
46-
end
46+
end

samples/processing_app/topics/advanced_data/threads.rb

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515
# This sketch will load data from all of these URLs in a separate thread
1616

1717
URLS = [
18-
'http://processing.org',
19-
'http://www.processing.org/exhibition/',
20-
'http://www.processing.org/reference/',
21-
'http://www.processing.org/reference/libraries',
22-
'http://www.processing.org/reference/tools',
23-
'http://www.processing.org/reference/environment',
24-
'http://www.processing.org/learning/',
25-
'http://www.processing.org/learning/basics/',
26-
'http://www.processing.org/learning/topics/',
27-
'http://www.processing.org/learning/gettingstarted/',
28-
'http://www.processing.org/download/',
29-
'http://www.processing.org/shop/',
30-
'http://www.processing.org/about/'
18+
'https://processing.org',
19+
'https://www.processing.org/exhibition/',
20+
'https://www.processing.org/reference/',
21+
'https://www.processing.org/reference/libraries',
22+
'https://www.processing.org/reference/tools',
23+
'https://www.processing.org/reference/environment',
24+
'https://www.processing.org/learning/',
25+
'https://www.processing.org/learning/basics/',
26+
'https://www.processing.org/learning/topics/',
27+
'https://www.processing.org/learning/gettingstarted/',
28+
'https://www.processing.org/download/',
29+
'https://www.processing.org/shop/',
30+
'https://www.processing.org/about/'
3131
]
3232

33-
3433
attr_reader :finished, :percent
3534

3635
def setup
@@ -42,34 +41,33 @@ def setup
4241

4342
def draw
4443
background(0)
45-
46-
# If we're not finished draw a 'loading bar'
47-
# This is so that we can see the progress of the thread
48-
# This would not be necessary in a sketch where you wanted to load data in the background
49-
# and hide this from the user, allowing the draw loop to simply continue
50-
if (!finished)
44+
# If we're not finished draw a 'loading bar', so that we can see the progress
45+
# of the thread. This would not be necessary in a sketch where you wanted to
46+
# load data in the background and hide this from the user.
47+
if !finished
5148
stroke(255)
5249
no_fill
53-
rect(width/2-150, height/2, 300, 10)
50+
rect(width / 2 - 150, height / 2, 300, 10)
5451
fill(255)
5552
# The size of the rectangle is mapped to the percentage completed
5653
w = map(percent, 0, 1, 0, 300)
57-
rect(width/2-150, height/2, w, 10)
54+
rect(width / 2 - 150, height / 2, w, 10)
5855
text_size(16)
5956
text_align(CENTER)
6057
fill(255)
61-
text('Loading', width/2, height/2+30)
58+
text('Loading', width / 2, height / 2 + 30)
6259
else
6360
# The thread is complete!
6461
text_align(CENTER)
6562
text_size(24)
6663
fill(255)
67-
text('Finished loading. Click the mouse to load again.', width/2, height/2)
64+
message = 'Finished loading. Click the mouse to load again.'
65+
text(message, width / 2, height / 2)
6866
end
6967
end
7068

7169
def load_data
72-
Thread.new {
70+
Thread.new do
7371
# The thread is not completed
7472
@finished = false
7573
@percent = 0
@@ -88,9 +86,9 @@ def load_data
8886
@percent = i.to_f / URLS.length
8987
end
9088
@finished = true
91-
}
89+
end
9290
end
9391

9492
def mouse_pressed # guard against calling load_data when running
95-
load_data unless !finished
93+
load_data if finished
9694
end

samples/processing_app/topics/advanced_data/threads_two.rb

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,48 @@ def setup
1414
@frames = []
1515
@done_loading = false
1616
@curr = 0
17-
thread do # supply a block in ruby-processing rather than use reflection
17+
load_image_thread
18+
@ang = 0.0
19+
end
20+
21+
def load_image_thread
22+
thread do # supply a block in ruby-processing rather than use reflection
1823
FRAMES.times do |i|
1924
frames << load_image("a#{i.to_s.rjust(3, '0')} copy.jpg") # ruby syntax
2025
@curr = i
21-
delay(75) #just slows down this thread - the main draw cycle is unaffected...
26+
delay(75) # just slows down this thread, the main draw cycle is unaffected
2227
end
2328
@curr = 0
2429
frame_rate(30)
2530
@done_loading = true
2631
end
27-
@ang = 0.0
2832
end
2933

30-
3134
def draw
32-
if (done_loading)
33-
background(0)
34-
image(frames[curr], 0, 0)
35-
@curr += 1
36-
if (curr > 115)
37-
@curr = 0
38-
end
35+
if done_loading
36+
display_animation
3937
else
38+
display_loading ang
4039
@ang += 0.1
41-
x = cos(ang) * 8
42-
y = sin(ang) * 8
43-
fill(0, 8)
44-
rect(50, 150, 100, 100)
45-
fill(32, 32, 255)
46-
ellipse(x + 100, y + 200, 8, 8)
47-
fill(0)
48-
rect(120, 150, 170, 100)
49-
fill(128)
50-
text("loading frames (#{curr} of 115)", 200, 200)
5140
end
5241
end
42+
43+
def display_animation
44+
background(0)
45+
image(frames[curr], 0, 0)
46+
@curr += 1
47+
@curr = 0 if curr >= FRAMES
48+
end
49+
50+
def display_loading(ang)
51+
x = cos(ang) * 8
52+
y = sin(ang) * 8
53+
fill(0, 8)
54+
rect(50, 150, 100, 100)
55+
fill(32, 32, 255)
56+
ellipse(x + 100, y + 200, 8, 8)
57+
fill(0)
58+
rect(120, 150, 170, 100)
59+
fill(128)
60+
text("loading frames (#{curr} of 115)", 200, 200)
61+
end

0 commit comments

Comments
 (0)