ruby-processing 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +8 -0
- data/lib/patches/JRubyApplet.diff +0 -0
- data/lib/patches/PATCHES.txt +0 -0
- data/lib/patches/PApplet.diff +0 -0
- data/lib/ruby-processing.rb +1 -1
- data/lib/ruby-processing/app.rb +20 -1
- data/lib/ruby-processing/exporters/base_exporter.rb +1 -1
- data/lib/ruby-processing/exporters/creator.rb +1 -2
- data/lib/ruby-processing/runner.rb +4 -3
- data/lib/ruby-processing/runners/watch.rb +2 -2
- data/lib/templates/applet/index.html.erb +1 -1
- metadata +2 -145
- data/samples/learning_processing/ABOUT +0 -11
- data/samples/learning_processing/chapter_01/1_stroke_and_fill.rb +0 -18
- data/samples/learning_processing/chapter_01/2_nofill.rb +0 -20
- data/samples/learning_processing/chapter_01/3_rgb_color.rb +0 -19
- data/samples/learning_processing/chapter_01/4_alpha_transparency.rb +0 -42
- data/samples/learning_processing/chapter_01/5_zoog.rb +0 -24
- data/samples/learning_processing/chapter_02/1_zoog_again.rb +0 -41
- data/samples/learning_processing/chapter_03/1_zoog_as_dynamic_sketch.rb +0 -44
- data/samples/learning_processing/chapter_03/2_mouse_x_and_mouse_y.rb +0 -26
- data/samples/learning_processing/chapter_03/3_zoog_as_dynamic_sketch_with_variation.rb +0 -41
- data/samples/learning_processing/chapter_03/4_drawing_a_continuous_line.rb +0 -18
- data/samples/learning_processing/chapter_03/5_mouse_pressed_and_key_pressed.rb +0 -29
- data/samples/learning_processing/chapter_03/6_interactive_zoog.rb +0 -42
- data/samples/learning_processing/chapter_04/1_variable_declaration.rb +0 -24
- data/samples/learning_processing/chapter_04/2_using_variables.rb +0 -27
- data/samples/learning_processing/chapter_04/3_varying_variables.rb +0 -27
- data/samples/learning_processing/chapter_04/4_many_variables.rb +0 -38
- data/samples/learning_processing/chapter_04/5_using_system_variables.rb +0 -28
- data/samples/learning_processing/chapter_04/6_ellipse_with_variables.rb +0 -24
- data/samples/learning_processing/chapter_04/7_filling_variables_with_random_values.rb +0 -27
- data/samples/learning_processing/chapter_04/8_variable_zoog.rb +0 -50
- data/samples/learning_processing/chapter_05/01_conditionals.rb +0 -31
- data/samples/learning_processing/chapter_05/02_more_conditionals.rb +0 -38
- data/samples/learning_processing/chapter_05/03_rollovers.rb +0 -34
- data/samples/learning_processing/chapter_05/04_hold_down_the_button.rb +0 -33
- data/samples/learning_processing/chapter_05/05_button_as_switch.rb +0 -39
- data/samples/learning_processing/chapter_05/06_bouncing_ball.rb +0 -33
- data/samples/learning_processing/chapter_05/07_bouncing_color.rb +0 -37
- data/samples/learning_processing/chapter_05/08_square_following_edge.rb +0 -59
- data/samples/learning_processing/chapter_05/09_simple_gravity.rb +0 -43
- data/samples/learning_processing/chapter_05/10_zoog_and_conditionals.rb +0 -53
- data/samples/learning_processing/chapter_06/01_many_lines.rb +0 -29
- data/samples/learning_processing/chapter_06/02_many_lines_with_variables.rb +0 -53
- data/samples/learning_processing/chapter_06/03_while_loop.rb +0 -30
- data/samples/learning_processing/chapter_06/04_infinite_loop.rb +0 -25
- data/samples/learning_processing/chapter_06/05_another_infinite_loop.rb +0 -33
- data/samples/learning_processing/chapter_06/06_legs_with_a_for_loop.rb +0 -41
- data/samples/learning_processing/chapter_06/07_local_variables.rb +0 -39
- data/samples/learning_processing/chapter_06/08_lines_one_at_a_time.rb +0 -30
- data/samples/learning_processing/chapter_06/09_simple_while_loop_with_interactivity.rb +0 -41
- data/samples/learning_processing/chapter_06/10_zoog_with_arms.rb +0 -56
- data/samples/learning_processing/chapter_06/11_multiple_zoogs.rb +0 -46
- data/samples/learning_processing/chapter_07/01_defining_a_function.rb +0 -22
- data/samples/learning_processing/chapter_07/02_calling_a_function.rb +0 -22
- data/samples/learning_processing/chapter_07/03_bouncing_ball_with_functions.rb +0 -49
- data/samples/learning_processing/chapter_07/04_function_returns_distance.rb +0 -48
- data/samples/learning_processing/chapter_07/05_zoog_with_functions.rb +0 -71
- data/samples/learning_processing/chapter_08/01_car_class_and_car_variable.rb +0 -45
- data/samples/learning_processing/chapter_08/02_two_car_objects.rb +0 -60
- data/samples/learning_processing/chapter_08/03_object_oriented_zoog.rb +0 -81
- data/samples/learning_processing/chapter_10/01_catcher.rb +0 -35
- data/samples/learning_processing/chapter_10/02_bouncing_ball.rb +0 -44
- data/samples/learning_processing/chapter_10/03_bouncing_balls_with_intersection.rb +0 -69
- data/samples/learning_processing/chapter_10/04_a_timer.rb +0 -24
- data/samples/learning_processing/chapter_10/05_object_oriented_timer.rb +0 -42
- data/samples/learning_processing/chapter_10/06_simple_raindrop.rb +0 -23
- data/samples/learning_processing/chapter_10/07_drops_one_at_a_time.rb +0 -63
- data/samples/learning_processing/chapter_13/02_random_number_distribution.rb +0 -24
- data/samples/learning_processing/chapter_13/03_probabilities.rb +0 -33
- data/samples/learning_processing/chapter_13/04_perlin_noise.rb +0 -22
- data/samples/learning_processing/chapter_13/06_oscillation.rb +0 -29
- data/samples/learning_processing/chapter_13/07_wave.rb +0 -31
- data/samples/learning_processing/chapter_13/08_recursion.rb +0 -30
- data/samples/learning_processing/chapter_13/09_two_dimensional_array.rb +0 -21
- data/samples/learning_processing/chapter_13/10_two_dimensional_array_with_objects.rb +0 -54
- data/samples/learning_processing/chapter_14/01_growing_rectangle.rb +0 -26
- data/samples/learning_processing/chapter_14/02_multiple_translations.rb +0 -37
- data/samples/learning_processing/chapter_14/03_depth_rectangle.rb +0 -29
- data/samples/learning_processing/chapter_14/04_pyramid_using_beingshape.rb +0 -55
- data/samples/learning_processing/chapter_14/05_rotate_rectangle_around_center.rb +0 -28
- data/samples/learning_processing/chapter_14/06_rotate_z.rb +0 -24
- data/samples/learning_processing/chapter_14/07_rotate_x.rb +0 -24
- data/samples/learning_processing/chapter_14/08_rotate_y.rb +0 -24
- data/samples/learning_processing/chapter_14/09_rotate_all.rb +0 -24
- data/samples/learning_processing/chapter_14/10_draw_pyramid.rb +0 -59
- data/samples/learning_processing/chapter_14/12_rotating_one_square.rb +0 -25
- data/samples/learning_processing/chapter_14/13_rotating_another_square.rb +0 -25
- data/samples/learning_processing/chapter_14/14_rotating_both_squares.rb +0 -43
- data/samples/learning_processing/chapter_14/15_rotating_many_things.rb +0 -56
- data/samples/learning_processing/chapter_14/16_simple_solar_system.rb +0 -53
- data/samples/learning_processing/chapter_14/17_nested_push_and_pop.rb +0 -50
- data/samples/learning_processing/chapter_14/18_object_oriented_solar_system.rb +0 -69
- data/samples/learning_processing/chapter_15/01_hello_world_image.rb +0 -20
- data/samples/learning_processing/chapter_15/02_image_sprite.rb +0 -29
- data/samples/learning_processing/chapter_15/03_swapping_images.rb +0 -25
- data/samples/learning_processing/chapter_15/04_image_sequence.rb +0 -22
- data/samples/learning_processing/chapter_15/05_setting_pixels.rb +0 -19
- data/samples/learning_processing/chapter_15/06_pixels_2d.rb +0 -29
- data/samples/learning_processing/chapter_15/07_image_pixels.rb +0 -40
- data/samples/learning_processing/chapter_15/08_image_brightness.rb +0 -29
- data/samples/learning_processing/chapter_15/09_flashlight_effect.rb +0 -42
- data/samples/learning_processing/chapter_15/10_brightness_threshold.rb +0 -33
- data/samples/learning_processing/chapter_15/11_brightness_threshold_with_filter.rb +0 -20
- data/samples/learning_processing/chapter_15/12_pixel_neighbour_differences.rb +0 -43
- data/samples/learning_processing/chapter_15/14_pointillism.rb +0 -34
- data/samples/learning_processing/chapter_15/15_2d_image_mapped_to_3d.rb +0 -43
- data/samples/learning_processing/chapter_15/data/animal0.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal1.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal2.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal3.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal4.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal5.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/animal6.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/face.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/mysummervacation.jpg +0 -0
- data/samples/learning_processing/chapter_15/data/sunflower.jpg +0 -0
- data/samples/learning_processing/chapter_16/01_display_video.rb +0 -25
- data/samples/learning_processing/chapter_16/02_manipulate_video_image.rb +0 -24
- data/samples/learning_processing/chapter_16/03_adjust_video_brightness.rb +0 -49
- data/samples/learning_processing/chapter_17/01_simple_displaying_text.rb +0 -14
- data/samples/learning_processing/chapter_17/02_text_align.rb +0 -21
- data/samples/learning_processing/chapter_17/03_scrolling_headlines.rb +0 -35
- data/samples/learning_processing/chapter_17/04_text_mirror.rb +0 -68
- data/samples/learning_processing/chapter_17/05_rotating_text.rb +0 -22
- data/samples/learning_processing/chapter_17/06_text_breaking_up.rb +0 -73
- data/samples/learning_processing/chapter_17/07_boxes_along_a_curve.rb +0 -49
- data/samples/learning_processing/chapter_17/08_characters_along_a_curve.rb +0 -51
- data/samples/learning_processing/chapter_17/data/ArialMT-16.vlw +0 -0
- data/samples/learning_processing/chapter_17/data/Courier-Bold-20.vlw +0 -0
- data/samples/learning_processing/chapter_18/01_user_input.rb +0 -39
- data/samples/learning_processing/chapter_18/02_graphing_comma_separated_numbers_from_a_text_file.rb +0 -28
- data/samples/learning_processing/chapter_18/03_creating_object_from_a_text_file.rb +0 -64
- data/samples/learning_processing/chapter_18/data/data-1.txt +0 -1
- data/samples/learning_processing/chapter_18/data/data-2.txt +0 -10
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class BouncingBallsWithIntersection < Processing::App
|
4
|
-
def setup
|
5
|
-
smooth
|
6
|
-
# Create two balls.
|
7
|
-
@ball_1, @ball_2 = Ball.new(64), Ball.new(32)
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw
|
11
|
-
background 255
|
12
|
-
@ball_1.move
|
13
|
-
@ball_2.move
|
14
|
-
|
15
|
-
# New! Methods on objects that you define can take each other as arguments.
|
16
|
-
# This is one way to have objects communicate.
|
17
|
-
# In this case they're checking to see if they intersect.
|
18
|
-
if @ball_1.intersect(@ball_2)
|
19
|
-
@ball_1.highlight
|
20
|
-
@ball_2.highlight
|
21
|
-
end
|
22
|
-
|
23
|
-
@ball_1.display
|
24
|
-
@ball_2.display
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
class Ball
|
30
|
-
# We use attr_reader to make the @x, @y and @r instance variables readable by other objects.
|
31
|
-
attr_reader :x, :y, :r
|
32
|
-
|
33
|
-
def initialize(temp_r)
|
34
|
-
@r = temp_r
|
35
|
-
@x, @y = rand($app.width), rand($app.height)
|
36
|
-
@x_speed, @y_speed = (rand * 10 - 5), (rand * 10 - 5)
|
37
|
-
@color = $app.color(100, 50)
|
38
|
-
end
|
39
|
-
|
40
|
-
def move
|
41
|
-
@x += @x_speed # Move the ball horizontally
|
42
|
-
@y += @y_speed # Move the ball vertically
|
43
|
-
|
44
|
-
@x_speed *= -1 unless (0..$app.width).include?(@x)
|
45
|
-
@y_speed *= -1 unless (0..$app.height).include?(@y)
|
46
|
-
end
|
47
|
-
|
48
|
-
def highlight
|
49
|
-
@color = $app.color(0, 150)
|
50
|
-
end
|
51
|
-
|
52
|
-
def display
|
53
|
-
$app.stroke 0
|
54
|
-
$app.fill @color
|
55
|
-
$app.ellipse @x, @y, @r*2, @r*2
|
56
|
-
# After the ball is displayed, the color is reset back to a dark gray.
|
57
|
-
@color = $app.color(100, 50)
|
58
|
-
end
|
59
|
-
|
60
|
-
# A function that returns true or false based on whether or not two circles intersect.
|
61
|
-
# If distance is less than the sum of the radii, the circles touch.
|
62
|
-
# Objects that you define can be passed as arguments too!
|
63
|
-
def intersect(other_ball)
|
64
|
-
distance = $app.dist(@x, @y, other_ball.x, other_ball.y)
|
65
|
-
distance < @r + other_ball.r
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
BouncingBallsWithIntersection.new :title => "Bouncing Balls with Intersection", :width => 400, :height => 400
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class Timer < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
background 0
|
7
|
-
@total_time = 5000
|
8
|
-
@saved_time = millis
|
9
|
-
end
|
10
|
-
|
11
|
-
def draw
|
12
|
-
passed_time = millis - @saved_time
|
13
|
-
# Has 5 seconds passed?
|
14
|
-
if passed_time > @total_time
|
15
|
-
puts "5 seconds have passed!"
|
16
|
-
# Color a new background, and let's give it some color.
|
17
|
-
background rand(255), rand(255), rand(255)
|
18
|
-
@saved_time = millis # Save the current time to restart the timer!
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
Timer.new :title => "04 A Timer", :width => 200, :height => 200
|
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class ObjectOrientedTimer < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
background 0
|
7
|
-
@timer = Timer.new(5000)
|
8
|
-
@timer.start
|
9
|
-
end
|
10
|
-
|
11
|
-
def draw
|
12
|
-
if @timer.is_finished?
|
13
|
-
background rand(255)
|
14
|
-
@timer.start
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
class Timer
|
22
|
-
def initialize(total_time)
|
23
|
-
@saved_time = nil # When it started
|
24
|
-
@total_time = total_time # How long it should last
|
25
|
-
end
|
26
|
-
|
27
|
-
# When the timer starts it stores the current time in milliseconds
|
28
|
-
def start
|
29
|
-
@saved_time = $app.millis
|
30
|
-
end
|
31
|
-
|
32
|
-
# The method is_finished? returns true if 5 seconds have passed.
|
33
|
-
# Most of the work of the timer is farmed out to this method.
|
34
|
-
def is_finished?
|
35
|
-
passed_time = $app.millis - @saved_time
|
36
|
-
passed_time > @total_time
|
37
|
-
end
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
ObjectOrientedTimer.new :title => "Object Oriented Timer", :width => 200, :height => 200
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class SimpleRaindrop < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
background 0
|
7
|
-
@x, @y = width / 2, 0
|
8
|
-
smooth
|
9
|
-
no_stroke
|
10
|
-
end
|
11
|
-
|
12
|
-
def draw
|
13
|
-
background 255
|
14
|
-
# Show the drop
|
15
|
-
fill 50, 100, 150
|
16
|
-
ellipse @x, @y, 16, 16
|
17
|
-
# Move the drop
|
18
|
-
@y += 1
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
SimpleRaindrop.new :title => "Simple Raindrop", :width => 400, :height => 400
|
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class DropsOneAtATime < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@max_drops = 1000 # The most drops we're going to have at once.
|
7
|
-
@drops = [] # An array to hold the drops
|
8
|
-
no_stroke
|
9
|
-
smooth
|
10
|
-
end
|
11
|
-
|
12
|
-
def draw
|
13
|
-
background 255
|
14
|
-
@drops << Drop.new # Create one drop
|
15
|
-
# Start over when we have too many.
|
16
|
-
@drops = [] if @drops.size > @max_drops
|
17
|
-
|
18
|
-
# Move and display all the drops.
|
19
|
-
# New! We no longer move and display all the drops, but rather,
|
20
|
-
# only the ones that are still present in the @drops array.
|
21
|
-
@drops.each do |drop|
|
22
|
-
drop.move
|
23
|
-
drop.display
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
class Drop
|
31
|
-
def initialize
|
32
|
-
@r = 8 # All raindrops are the same size
|
33
|
-
@x = rand($app.width) # Start with a random x location
|
34
|
-
@y = -@r * 4 # Start a little above the window
|
35
|
-
@speed = rand * 4 + 1 # Pick a random speed
|
36
|
-
@color = $app.color(50, 100, 150)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Move the raindrop down
|
40
|
-
def move
|
41
|
-
@y += @speed # Increment y by speed
|
42
|
-
end
|
43
|
-
|
44
|
-
# Check to see if it hit the bottom
|
45
|
-
def reached_bottom?
|
46
|
-
@y > $app.height + @r*4 # If we go a little beyond the bottom
|
47
|
-
end
|
48
|
-
|
49
|
-
# Display the raindrop
|
50
|
-
def display
|
51
|
-
$app.fill @color
|
52
|
-
# For each number between 2 and the radius, draw an ellipse.
|
53
|
-
(2...@r).each { |i| $app.ellipse(@x, @y+i*4, i*2, i*2) }
|
54
|
-
end
|
55
|
-
|
56
|
-
# If the drop is caught
|
57
|
-
def caught
|
58
|
-
@speed = 0 # Stop it from moving by setting speed to 0
|
59
|
-
@y = -1000 # Set the location way off the screen
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
DropsOneAtATime.new :title => "Drops One At A Time", :width => 400, :height => 400
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class RandomNumberDistributionSketch < Processing::App
|
4
|
-
def setup
|
5
|
-
# An array to keep track of how often numbers are picked.
|
6
|
-
@random_counts = Array.new(20, 0)
|
7
|
-
end
|
8
|
-
|
9
|
-
def draw
|
10
|
-
size = @random_counts.size
|
11
|
-
# Pick a random number and increase the count.
|
12
|
-
@random_counts[rand(size)] += 1
|
13
|
-
background 255
|
14
|
-
stroke 0
|
15
|
-
fill 175
|
16
|
-
# Draw a rectangle to graph results
|
17
|
-
@random_counts.each_with_index do |count, i|
|
18
|
-
rect i * width/size, 0, (width/size)-1, count
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
RandomNumberDistributionSketch.new :title => "Random Number Distribution", :width => 200, :height => 200
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class ProbabilitiesSketch < Processing::App
|
4
|
-
def setup
|
5
|
-
background 255
|
6
|
-
smooth
|
7
|
-
no_stroke
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw
|
11
|
-
# Probabilities for 3 different cases
|
12
|
-
# These need to add up to 100%!
|
13
|
-
red_prob = 0.60 # 60% chance of red color
|
14
|
-
green_prob = 0.70 # 10% chance of green color
|
15
|
-
blue_prob = 1.00 # 30% chance of blue color
|
16
|
-
|
17
|
-
# Pick a random number between 0 and 1
|
18
|
-
num = rand
|
19
|
-
|
20
|
-
case num
|
21
|
-
when (0.0..red_prob): fill(255, 53, 2, 150)
|
22
|
-
when (red_prob..green_prob): fill(156, 255, 28, 150)
|
23
|
-
when (green_prob..blue_prob): fill(10, 52, 178, 150)
|
24
|
-
end
|
25
|
-
|
26
|
-
# Draw ellipse
|
27
|
-
ellipse rand(width), rand(height), 64, 64
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
ProbabilitiesSketch.new :title => "Probabilities", :width => 400, :height => 400
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class PerlinNoiseSketch < Processing::App
|
4
|
-
def setup
|
5
|
-
smooth
|
6
|
-
@time = 0.0
|
7
|
-
@increment = 0.01
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw
|
11
|
-
background 255
|
12
|
-
|
13
|
-
n = noise(@time) * width
|
14
|
-
@time += @increment
|
15
|
-
fill 0
|
16
|
-
ellipse width/2, height/2, n, n
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
PerlinNoiseSketch.new :title => "Perlin Noise", :width => 400, :height => 400
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class OscillationSketch < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@theta = 0.0
|
7
|
-
smooth
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw
|
11
|
-
background 255
|
12
|
-
# The output of the sin() function oscillates smoothly between 1 and 1.
|
13
|
-
# By adding 1 we get values between 0 and 2.
|
14
|
-
# By multiplying by 100, we get values between 0 and 200 which can be used as the ellipse's x location.
|
15
|
-
x = (sin(@theta) + 1) * width/2
|
16
|
-
|
17
|
-
# with each cycle, increment theta
|
18
|
-
@theta += 0.05
|
19
|
-
fill 0
|
20
|
-
stroke 0
|
21
|
-
|
22
|
-
# draw the ellipse at the value produce by the sin
|
23
|
-
line width/2, 0, x, height/2
|
24
|
-
ellipse x, height/2, 16, 16
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
OscillationSketch.new :title => "Oscillation", :width => 200, :height => 200
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class WaveSketch < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@theta = 0
|
7
|
-
smooth
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw
|
11
|
-
background 255
|
12
|
-
# Increment theta (try different values for " angular velocity " here)
|
13
|
-
@theta += 0.02
|
14
|
-
no_stroke
|
15
|
-
fill 0
|
16
|
-
x = @theta
|
17
|
-
|
18
|
-
# times is used to draw all the points along a sine wave (scaled to the pixel dimension of the window).
|
19
|
-
20.times do |i|
|
20
|
-
# Calculate y value based off of sine function
|
21
|
-
y = sin(x) * height/2
|
22
|
-
# Draw an ellipse
|
23
|
-
ellipse i*10, y+height/2, 16, 16
|
24
|
-
# Move along x-axis
|
25
|
-
x += 0.2
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
WaveSketch.new :title => "Oscillation", :width => 200, :height => 200
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class RecursionSketch < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
smooth
|
7
|
-
end
|
8
|
-
|
9
|
-
def draw
|
10
|
-
background 255
|
11
|
-
stroke 0
|
12
|
-
no_fill
|
13
|
-
|
14
|
-
draw_circle width/2, height/2, width/2
|
15
|
-
end
|
16
|
-
|
17
|
-
def draw_circle(x, y, radius)
|
18
|
-
ellipse x, y, radius, radius
|
19
|
-
# It's important to establish an end-point to recursion, or else it
|
20
|
-
# would go forever. Comment out the following line to break the sketch.
|
21
|
-
return if radius < 2
|
22
|
-
# draw_circle calls itself twice, creating a branching effect.
|
23
|
-
# For every circle, a smaller circle is drawn to the left and right.
|
24
|
-
draw_circle x+radius/2, y, radius/2
|
25
|
-
draw_circle x-radius/2, y, radius/2
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
RecursionSketch.new :title => "Recursion", :width => 200, :height => 200
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
class TwoDimensionalArraySketch < Processing::App
|
4
|
-
|
5
|
-
def setup
|
6
|
-
smooth
|
7
|
-
# initialize array with random values
|
8
|
-
@my_array = Array.new(width) { Array.new(height) { rand 255 }}
|
9
|
-
|
10
|
-
# draw the points
|
11
|
-
width.times do |i|
|
12
|
-
height.times do |j|
|
13
|
-
stroke @my_array[i][j]
|
14
|
-
point i, j
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
TwoDimensionalArraySketch.new :title => "Two Dimensional Array", :width => 200, :height => 200
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'ruby-processing'
|
2
|
-
|
3
|
-
# Let's start by defining the cell class.
|
4
|
-
class Cell
|
5
|
-
|
6
|
-
def initialize(x, y, w, h, angle)
|
7
|
-
@x, @y = x, y
|
8
|
-
@w, @h = w, h
|
9
|
-
@angle = angle
|
10
|
-
end
|
11
|
-
|
12
|
-
# we oscillate by increasing the angle, here.
|
13
|
-
def oscillate
|
14
|
-
@angle += 0.02
|
15
|
-
end
|
16
|
-
|
17
|
-
def display
|
18
|
-
$app.stroke 255
|
19
|
-
# calculate color using sine wave
|
20
|
-
$app.fill 127 + 127 * Math.sin(@angle)
|
21
|
-
$app.rect @x, @y, @w, @h
|
22
|
-
end
|
23
|
-
|
24
|
-
def oscillate_and_display
|
25
|
-
oscillate
|
26
|
-
display
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
# And then the sketch that holds many cells.
|
33
|
-
class ArrayWithObjectsSketch < Processing::App
|
34
|
-
|
35
|
-
def setup
|
36
|
-
smooth
|
37
|
-
@cols = @rows = 20
|
38
|
-
|
39
|
-
# initialize array with random values
|
40
|
-
@grid = Array.new(@cols) do |i|
|
41
|
-
Array.new(@rows) do |j|
|
42
|
-
Cell.new(i*20, j*20, 20, 20, i+j)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def draw
|
48
|
-
background 0
|
49
|
-
@grid.each { |array| array.each { |cell| cell.oscillate_and_display } }
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
ArrayWithObjectsSketch.new :title => "Two Dimensional Array with Objects", :width => 200, :height => 200
|