ruby-processing 1.0.2 → 1.0.3

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.
Files changed (83) hide show
  1. data/CHANGELOG +12 -0
  2. data/lib/ruby-processing.rb +2 -1
  3. data/lib/ruby-processing/app.rb +7 -5
  4. data/lib/ruby-processing/exporters/creator.rb +7 -4
  5. data/lib/ruby-processing/helpers/numeric.rb +11 -0
  6. data/lib/ruby-processing/helpers/string.rb +8 -0
  7. data/lib/ruby-processing/runner.rb +11 -10
  8. data/lib/ruby-processing/runners/watch.rb +33 -0
  9. data/samples/bezier_playground.rb +243 -0
  10. data/samples/flight_patterns.rb +1 -1
  11. data/samples/learning_processing/chapter_16/01_display_video.rb +1 -1
  12. data/samples/learning_processing/chapter_16/02_manipulate_video_image.rb +1 -1
  13. data/samples/processing_app/basics/arrays/array.rb +41 -0
  14. data/samples/processing_app/basics/arrays/array_2d.rb +38 -0
  15. data/samples/processing_app/basics/arrays/array_objects.rb +67 -0
  16. data/samples/processing_app/basics/color/brightness.rb +30 -0
  17. data/samples/processing_app/basics/color/color_wheel.rb +97 -0
  18. data/samples/processing_app/basics/color/creating.rb +37 -0
  19. data/samples/processing_app/basics/color/hue.rb +29 -0
  20. data/samples/processing_app/basics/color/linear_gradient.rb +47 -0
  21. data/samples/processing_app/basics/color/radial_gradient.rb +52 -0
  22. data/samples/processing_app/basics/color/reading/data/cait.jpg +0 -0
  23. data/samples/processing_app/basics/color/reading/reading.rb +43 -0
  24. data/samples/processing_app/basics/color/relativity.rb +38 -0
  25. data/samples/processing_app/basics/color/saturation.rb +35 -0
  26. data/samples/processing_app/basics/color/wave_gradient.rb +45 -0
  27. data/samples/processing_app/basics/control/conditionals1.rb +51 -0
  28. data/samples/processing_app/basics/control/conditionals2.rb +44 -0
  29. data/samples/processing_app/basics/control/embedded_iteration.rb +42 -0
  30. data/samples/processing_app/basics/control/iteration.rb +61 -0
  31. data/samples/processing_app/basics/control/logical_operators.rb +59 -0
  32. data/samples/processing_app/basics/data/characters_strings/characters_strings.rb +87 -0
  33. data/samples/processing_app/basics/data/characters_strings/data/Eureka-90.vlw +0 -0
  34. data/samples/processing_app/basics/data/characters_strings/data/rathausFrog.jpg +0 -0
  35. data/samples/processing_app/basics/data/datatype_conversion.rb +45 -0
  36. data/samples/processing_app/basics/data/integers_floats.rb +34 -0
  37. data/samples/processing_app/basics/data/true_false.rb +37 -0
  38. data/samples/processing_app/basics/data/variable_scope.rb +80 -0
  39. data/samples/processing_app/basics/data/variables.rb +40 -0
  40. data/samples/processing_app/basics/form/bezier.rb +23 -0
  41. data/samples/processing_app/basics/form/bezier_ellipse.rb +110 -0
  42. data/samples/processing_app/basics/form/pie_chart.rb +31 -0
  43. data/samples/processing_app/basics/form/points_lines.rb +37 -0
  44. data/samples/processing_app/basics/form/shape_primitives.rb +25 -0
  45. data/samples/processing_app/basics/form/triangle_strip.rb +43 -0
  46. data/samples/processing_app/basics/form/vertices.rb +51 -0
  47. data/samples/processing_app/basics/image/alphamask.rb +23 -0
  48. data/samples/processing_app/basics/image/background_image.rb +30 -0
  49. data/samples/processing_app/basics/image/create_image.rb +23 -0
  50. data/samples/processing_app/basics/image/data/construct.jpg +0 -0
  51. data/samples/processing_app/basics/image/data/eames.jpg +0 -0
  52. data/samples/processing_app/basics/image/data/jelly.jpg +0 -0
  53. data/samples/processing_app/basics/image/data/mask.jpg +0 -0
  54. data/samples/processing_app/basics/image/data/milan_rubbish.jpg +0 -0
  55. data/samples/processing_app/basics/image/data/teddy.gif +0 -0
  56. data/samples/processing_app/basics/image/data/test.jpg +0 -0
  57. data/samples/processing_app/basics/image/data/wash.jpg +0 -0
  58. data/samples/processing_app/basics/image/load_display_image.rb +25 -0
  59. data/samples/processing_app/basics/image/pointillism.rb +30 -0
  60. data/samples/processing_app/basics/image/request_image.rb +71 -0
  61. data/samples/processing_app/basics/image/sprite.rb +38 -0
  62. data/samples/processing_app/basics/image/transparency.rb +27 -0
  63. data/samples/processing_app/basics/input/clock.rb +46 -0
  64. data/samples/processing_app/basics/input/constrain.rb +39 -0
  65. data/samples/processing_app/basics/input/easing.rb +33 -0
  66. data/samples/processing_app/basics/input/keyboard.rb +36 -0
  67. data/samples/processing_app/basics/input/keyboard_2.rb +45 -0
  68. data/samples/processing_app/basics/input/keyboard_functions.rb +87 -0
  69. data/samples/processing_app/basics/input/milliseconds.rb +25 -0
  70. data/samples/processing_app/basics/input/mouse_1d.rb +34 -0
  71. data/samples/processing_app/basics/input/mouse_2d.rb +25 -0
  72. data/samples/processing_app/basics/input/mouse_functions.rb +73 -0
  73. data/samples/processing_app/basics/input/mouse_press.rb +21 -0
  74. data/samples/processing_app/basics/input/mouse_signals.rb +45 -0
  75. data/samples/processing_app/basics/input/storing_input.rb +39 -0
  76. data/samples/processing_app/basics/math/additive_wave.rb +66 -0
  77. data/samples/processing_app/basics/math/arctangent.rb +54 -0
  78. data/samples/processing_app/topics/simulate/chain.rb +82 -0
  79. data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +166 -0
  80. data/samples/processing_app/topics/simulate/simple_particle_system.rb +113 -0
  81. data/samples/processing_app/topics/simulate/spring.rb +85 -0
  82. data/samples/processing_app/topics/simulate/springs.rb +125 -0
  83. metadata +93 -2
@@ -0,0 +1,25 @@
1
+ require 'ruby-processing'
2
+
3
+ # The basic shape primitive functions are triangle, rect,
4
+ # quad, and ellipse. Squares are made with rect and circles
5
+ # are made with ellipse. Each of these functions requires a number
6
+ # of parameters to determine the shape's position and size.
7
+
8
+
9
+ class ShapePrimitives < Processing::App
10
+
11
+ def setup
12
+ smooth
13
+ background 0
14
+ no_stroke
15
+ fill 226
16
+ triangle 10, 10, 10, 200, 45, 200
17
+ rect 45, 45, 35, 35
18
+ quad 105, 10, 120, 10, 120, 200, 80, 200
19
+ ellipse 140, 80, 40, 40
20
+ triangle 160, 10, 195, 200, 160, 200
21
+ end
22
+
23
+ end
24
+
25
+ ShapePrimitives.new :title => "Shape Primitives", :width => 200, :height => 200
@@ -0,0 +1,43 @@
1
+ require 'ruby-processing'
2
+
3
+ # TRIANGLE_STRIP Mode
4
+ # by Ira Greenberg.
5
+ #
6
+ # Generate a closed ring using vertex()
7
+ # function and beginShape(TRIANGLE_STRIP)
8
+ # mode. outerRad and innerRad variables
9
+ # control ring's outer/inner radii respectively.
10
+ # Trig functions generate ring.
11
+
12
+ class TriangleStrip < Processing::App
13
+
14
+ def setup
15
+ background 204
16
+ smooth
17
+
18
+ x = width/2
19
+ y = height/2
20
+ outer_radius = 80
21
+ inner_radius = 50
22
+ px, py, angle = 0.0, 0.0, 0.0
23
+ number_of_points = 36
24
+ rotation = 360.0/number_of_points
25
+
26
+ begin_shape TRIANGLE_STRIP
27
+ number_of_points.times do |i|
28
+ px = x + cos(angle.radians)*outer_radius
29
+ py = y + sin(angle.radians)*outer_radius
30
+ angle += rotation
31
+ vertex px, py
32
+
33
+ px = x + cos(angle.radians)*inner_radius
34
+ py = y + sin(angle.radians)*inner_radius
35
+ angle += rotation
36
+ vertex px, py
37
+ end
38
+ end_shape
39
+ end
40
+
41
+ end
42
+
43
+ TriangleStrip.new :title => "Triangle Strip", :width => 200, :height => 200
@@ -0,0 +1,51 @@
1
+ require 'ruby-processing'
2
+
3
+ # The beginShape() function begins recording vertices
4
+ # for a shape and endShape() stops recording.
5
+ # A vertex is a location in space specified by X, Y,
6
+ # and sometimes Z coordinates. After calling the beginShape() function,
7
+ # a series of vertex() functions must follow.
8
+ # To stop drawing the shape, call the endShape() functions.
9
+
10
+ class Vertices < Processing::App
11
+
12
+ def setup
13
+ background 0
14
+ no_fill
15
+
16
+ stroke 102
17
+ begin_shape
18
+ curve_vertex 168, 182
19
+ curve_vertex 168, 182
20
+ curve_vertex 136, 38
21
+ curve_vertex 42, 34
22
+ curve_vertex 64, 200
23
+ curve_vertex 64, 200
24
+ end_shape
25
+
26
+ stroke 51
27
+ begin_shape LINES
28
+ vertex 60, 40
29
+ vertex 160, 10
30
+ vertex 170, 150
31
+ vertex 60, 150
32
+ end_shape
33
+
34
+ stroke 126
35
+ begin_shape
36
+ vertex 60, 40
37
+ bezier_vertex 160, 10, 170, 150, 60, 150
38
+ end_shape
39
+
40
+ stroke 255
41
+ begin_shape POINTS
42
+ vertex 60, 40
43
+ vertex 160, 10
44
+ vertex 170, 150
45
+ vertex 60, 150
46
+ end_shape
47
+ end
48
+
49
+ end
50
+
51
+ Vertices.new :title => "Vertices", :width => 200, :height => 200
@@ -0,0 +1,23 @@
1
+ require 'ruby-processing'
2
+
3
+ # Loads a "mask" for an image to specify the transparency
4
+ # in different parts of the image. The two images are blended
5
+ # together using the mask() method of PImage.
6
+
7
+ class Alphamask < Processing::App
8
+
9
+ def setup
10
+ @image = load_image "test.jpg"
11
+ @image_mask = load_image "mask.jpg"
12
+ @image.mask @image_mask
13
+ end
14
+
15
+ def draw
16
+ background (mouse_x + mouse_y) / 1.5
17
+ image @image, 50, 50
18
+ image @image, mouse_x-50, mouse_y-50
19
+ end
20
+
21
+ end
22
+
23
+ Alphamask.new :title => "Alphamask", :width => 200, :height => 200
@@ -0,0 +1,30 @@
1
+ require 'ruby-processing'
2
+
3
+ # This example presents the fastest way to load a background image
4
+ # into Processing. To load an image as the background, it must be
5
+ # the same width and height as the program.
6
+
7
+ class BackgroundImage < Processing::App
8
+
9
+ def setup
10
+ frame_rate 30
11
+ @a = 0
12
+
13
+ # The background image must be the same size as the parameters
14
+ # into the size method. In this program, the size of "milan_rubbish.jpg"
15
+ # is 200 x 200 pixels.
16
+ @background_image = load_image "milan_rubbish.jpg"
17
+ end
18
+
19
+ def draw
20
+ background @background_image
21
+
22
+ @a = (@a + 1) % (width+32)
23
+ stroke 266, 204, 0
24
+ line 0, @a, width, @a-26
25
+ line 0, @a-6, width, @a-32
26
+ end
27
+
28
+ end
29
+
30
+ BackgroundImage.new :title => "Background Image", :width => 200, :height => 200
@@ -0,0 +1,23 @@
1
+ require 'ruby-processing'
2
+
3
+ # The createImage() function provides a fresh buffer of pixels to play with.
4
+ # This example creates an image gradient.
5
+
6
+ class CreateImage < Processing::App
7
+
8
+ def setup
9
+ @image = create_image 120, 120, ARGB
10
+ @image.pixels.length.times do |i|
11
+ @image.pixels[i] = color 0, 90, 102, (i % @image.width * 2) # red, green, blue, alpha
12
+ end
13
+ end
14
+
15
+ def draw
16
+ background 204
17
+ image @image, 33, 33
18
+ image @image, mouse_x-60, mouse_y-60
19
+ end
20
+
21
+ end
22
+
23
+ CreateImage.new :title => "Create Image", :width => 200, :height => 200
@@ -0,0 +1,25 @@
1
+ require 'ruby-processing'
2
+
3
+ # Images can be loaded and displayed to the screen at their actual size
4
+ # or any other size.
5
+
6
+ class LoadDisplayImage < Processing::App
7
+
8
+ def setup
9
+ # The file "jelly.jpg" must be in the data folder
10
+ # of the current sketch to load successfully
11
+ @a = load_image "jelly.jpg"
12
+
13
+ no_loop # Makes draw only run once
14
+ end
15
+
16
+ def draw
17
+ # Displays the image at its actual size at point (0,0)
18
+ image @a, 0, 0
19
+ # Displays the image at point (100, 0) at half of its size
20
+ image @a, 100, 0, @a.width/2, @a.height/2
21
+ end
22
+
23
+ end
24
+
25
+ LoadDisplayImage.new :title => "Load Display Image", :width => 200, :height => 200
@@ -0,0 +1,30 @@
1
+ require 'ruby-processing'
2
+
3
+ # Pointillism
4
+ # by Daniel Shiffman.
5
+ #
6
+ # Mouse horizontal location controls size of dots.
7
+ # Creates a simple pointillist effect using ellipses colored
8
+ # according to pixels in an image.
9
+
10
+ class Pointillism < Processing::App
11
+
12
+ def setup
13
+ @a = load_image "eames.jpg"
14
+
15
+ no_stroke
16
+ background 255
17
+ smooth
18
+ end
19
+
20
+ def draw
21
+ pointillize = map mouse_x, 0, width, 2, 18
22
+ x, y = rand(@a.width), rand(@a.height)
23
+ pixel = @a.get(x, y)
24
+ fill pixel
25
+ ellipse x, y, pointillize, pointillize
26
+ end
27
+
28
+ end
29
+
30
+ Pointillism.new :title => "Pointillism", :width => 200, :height => 200
@@ -0,0 +1,71 @@
1
+ require 'ruby-processing'
2
+
3
+ # Request Image
4
+ # by Ira Greenberg.
5
+ # From Processing for Flash Developers, Friends of ED, 2009.
6
+ #
7
+ # Shows how to use the request_image() function with preloader animation.
8
+ # The request_image() function loads images on a separate thread so that
9
+ # the sketch does not freeze while they load. It's very useful when you are
10
+ # loading large images, as this example demonstrates.
11
+ #
12
+ # To work, this example requires 10 images named dublin0.jpg ... dublin9.jpg
13
+ # in the sketch data directory. To save space, these images are not included
14
+ # with the example.
15
+
16
+ class RequestImage < Processing::App
17
+
18
+ def setup
19
+ smooth
20
+
21
+ @imgs = Array.new 10
22
+ @load_states = Array.new( @imgs.length, false )
23
+ @loader_x, @loader_y, @theta = 0.0, 0.0, 0.0
24
+
25
+ # Load images asynchronously
26
+ @imgs.length.times do |i|
27
+ @imgs[i] = request_image "dublin" + i.to_s + ".jpg"
28
+ end
29
+ end
30
+
31
+ def draw
32
+ background 0
33
+
34
+ run_loader_animation
35
+
36
+ # Check if individual images are fully loaded
37
+ @imgs.each_with_index do |img, i|
38
+ # As images are loaded set true in boolean array
39
+ @load_states[i] = (img.width != 0) && (img.width != -1)
40
+ end
41
+
42
+ # When all images are loaded draw them to the screen
43
+ if all_loaded?
44
+ @img.each_with_index do |img, i|
45
+ image( img, width/@imgs.length*i, 0, width/@imgs.length, height )
46
+ end
47
+ end
48
+ end
49
+
50
+ # Loading animation
51
+ def run_loader_animation
52
+ # Only run when images are loading
53
+ if all_loaded?
54
+ ellipse loader_x, loader_y, 10, 10
55
+ loader_x += 2
56
+ loader_y = height/2 + sin(theta) * (height/2.5)
57
+ theta += PI/22
58
+
59
+ # Reposition ellipse if it goes off the screen
60
+ loader_x = -5 if loader_x > (width + 5)
61
+ end
62
+ end
63
+
64
+ # Return true when all images are loaded - no false values left in array
65
+ def all_loaded?
66
+ @load_states.each { |state| return false unless state }
67
+ true
68
+ end
69
+ end
70
+
71
+ RequestImage.new :title => "Request Image", :width => 800, :height => 60
@@ -0,0 +1,38 @@
1
+ require 'ruby-processing'
2
+
3
+ # Sprite (Teddy)
4
+ # by James Patterson.
5
+ #
6
+ # Demonstrates loading and displaying a transparent GIF image.
7
+
8
+ class Sprite < Processing::App
9
+
10
+ def setup
11
+ @teddy = load_image "teddy.gif"
12
+ @xpos, @ypos = width/2, height/2
13
+ @drag = 30.0
14
+
15
+ frame_rate 60
16
+ end
17
+
18
+ def draw
19
+ background 102
20
+
21
+ difx = mouse_x - @xpos - @teddy.width/2
22
+ if difx.abs > 1.0
23
+ @xpos += difx/@drag
24
+ @xpos = constrain( @xpos, 0, width-@teddy.width/2 )
25
+ end
26
+
27
+ dify = mouse_y - @ypos - @teddy.height/2
28
+ if dify.abs > 1.0
29
+ @ypos += dify/@drag
30
+ @ypos = constrain( @ypos, 0, height-@teddy.height/2 )
31
+ end
32
+
33
+ image @teddy, @xpos, @ypos
34
+ end
35
+
36
+ end
37
+
38
+ Sprite.new :title => "Sprite", :width => 200, :height => 200
@@ -0,0 +1,27 @@
1
+ require 'ruby-processing'
2
+
3
+ # Move the pointer left and right across the image to change
4
+ # its position. This program overlays one image over another
5
+ # by modifying the alpha value of the image with the tint() function.
6
+
7
+ class Transparency < Processing::App
8
+
9
+ def setup
10
+ @a = load_image "construct.jpg"
11
+ @b = load_image "wash.jpg"
12
+ @offset = 0.0
13
+
14
+ frame_rate 60
15
+ end
16
+
17
+ def draw
18
+ image @a, 0, 0
19
+ offset_target = map( mouse_x, 0, width, -@b.width/2 - width/2, 0 )
20
+ @offset += (offset_target - @offset) * 0.05
21
+ tint 255, 153
22
+ image @b, @offset, 20
23
+ end
24
+
25
+ end
26
+
27
+ Transparency.new :title => "Transparency", :width => 200, :height => 200
@@ -0,0 +1,46 @@
1
+ require 'ruby-processing'
2
+
3
+ # The current time can be read with the second(), minute(),
4
+ # and hour() functions. In this example, sin() and cos() values
5
+ # are used to set the position of the hands.
6
+
7
+ class Clock < Processing::App
8
+
9
+ def setup
10
+ stroke 255
11
+ smooth
12
+ end
13
+
14
+ def draw
15
+ background 0
16
+ fill 80
17
+ no_stroke
18
+
19
+ # Angles for sin() and cos() start at 3 o'clock;
20
+ # subtract HALF_PI to make them start at the top
21
+ ellipse 100, 100, 160, 160
22
+
23
+ s = map( second, 0, 60, 0, TWO_PI) - HALF_PI
24
+ m = map( minute + norm( second, 0, 60 ), 0, 60, 0, TWO_PI ) - HALF_PI
25
+ h = map( hour + norm( minute, 0, 60 ), 0, 24, 0, TWO_PI * 2 ) - HALF_PI
26
+
27
+ stroke 255
28
+ stroke_weight 1
29
+ line( 100, 100, cos(s)*72 + 100, sin(s)*72 + 100 )
30
+ stroke_weight 2
31
+ line( 100, 100, cos(m)*60 + 100, sin(m)*60 + 100 )
32
+ stroke_weight 4
33
+ line( 100, 100, cos(h)*50 + 100, sin(h)*50 + 100 )
34
+
35
+ # Draw the minute ticks
36
+ stroke_weight 2
37
+ (0..360).step(6) do |a|
38
+ x = 100 + cos( a.radians ) * 72
39
+ y = 100 + sin( a.radians ) * 72
40
+ point x, y
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ Clock.new :title => "Clock", :width => 200, :height => 200