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.
- data/CHANGELOG +12 -0
- data/lib/ruby-processing.rb +2 -1
- data/lib/ruby-processing/app.rb +7 -5
- data/lib/ruby-processing/exporters/creator.rb +7 -4
- data/lib/ruby-processing/helpers/numeric.rb +11 -0
- data/lib/ruby-processing/helpers/string.rb +8 -0
- data/lib/ruby-processing/runner.rb +11 -10
- data/lib/ruby-processing/runners/watch.rb +33 -0
- data/samples/bezier_playground.rb +243 -0
- data/samples/flight_patterns.rb +1 -1
- data/samples/learning_processing/chapter_16/01_display_video.rb +1 -1
- data/samples/learning_processing/chapter_16/02_manipulate_video_image.rb +1 -1
- data/samples/processing_app/basics/arrays/array.rb +41 -0
- data/samples/processing_app/basics/arrays/array_2d.rb +38 -0
- data/samples/processing_app/basics/arrays/array_objects.rb +67 -0
- data/samples/processing_app/basics/color/brightness.rb +30 -0
- data/samples/processing_app/basics/color/color_wheel.rb +97 -0
- data/samples/processing_app/basics/color/creating.rb +37 -0
- data/samples/processing_app/basics/color/hue.rb +29 -0
- data/samples/processing_app/basics/color/linear_gradient.rb +47 -0
- data/samples/processing_app/basics/color/radial_gradient.rb +52 -0
- data/samples/processing_app/basics/color/reading/data/cait.jpg +0 -0
- data/samples/processing_app/basics/color/reading/reading.rb +43 -0
- data/samples/processing_app/basics/color/relativity.rb +38 -0
- data/samples/processing_app/basics/color/saturation.rb +35 -0
- data/samples/processing_app/basics/color/wave_gradient.rb +45 -0
- data/samples/processing_app/basics/control/conditionals1.rb +51 -0
- data/samples/processing_app/basics/control/conditionals2.rb +44 -0
- data/samples/processing_app/basics/control/embedded_iteration.rb +42 -0
- data/samples/processing_app/basics/control/iteration.rb +61 -0
- data/samples/processing_app/basics/control/logical_operators.rb +59 -0
- data/samples/processing_app/basics/data/characters_strings/characters_strings.rb +87 -0
- data/samples/processing_app/basics/data/characters_strings/data/Eureka-90.vlw +0 -0
- data/samples/processing_app/basics/data/characters_strings/data/rathausFrog.jpg +0 -0
- data/samples/processing_app/basics/data/datatype_conversion.rb +45 -0
- data/samples/processing_app/basics/data/integers_floats.rb +34 -0
- data/samples/processing_app/basics/data/true_false.rb +37 -0
- data/samples/processing_app/basics/data/variable_scope.rb +80 -0
- data/samples/processing_app/basics/data/variables.rb +40 -0
- data/samples/processing_app/basics/form/bezier.rb +23 -0
- data/samples/processing_app/basics/form/bezier_ellipse.rb +110 -0
- data/samples/processing_app/basics/form/pie_chart.rb +31 -0
- data/samples/processing_app/basics/form/points_lines.rb +37 -0
- data/samples/processing_app/basics/form/shape_primitives.rb +25 -0
- data/samples/processing_app/basics/form/triangle_strip.rb +43 -0
- data/samples/processing_app/basics/form/vertices.rb +51 -0
- data/samples/processing_app/basics/image/alphamask.rb +23 -0
- data/samples/processing_app/basics/image/background_image.rb +30 -0
- data/samples/processing_app/basics/image/create_image.rb +23 -0
- data/samples/processing_app/basics/image/data/construct.jpg +0 -0
- data/samples/processing_app/basics/image/data/eames.jpg +0 -0
- data/samples/processing_app/basics/image/data/jelly.jpg +0 -0
- data/samples/processing_app/basics/image/data/mask.jpg +0 -0
- data/samples/processing_app/basics/image/data/milan_rubbish.jpg +0 -0
- data/samples/processing_app/basics/image/data/teddy.gif +0 -0
- data/samples/processing_app/basics/image/data/test.jpg +0 -0
- data/samples/processing_app/basics/image/data/wash.jpg +0 -0
- data/samples/processing_app/basics/image/load_display_image.rb +25 -0
- data/samples/processing_app/basics/image/pointillism.rb +30 -0
- data/samples/processing_app/basics/image/request_image.rb +71 -0
- data/samples/processing_app/basics/image/sprite.rb +38 -0
- data/samples/processing_app/basics/image/transparency.rb +27 -0
- data/samples/processing_app/basics/input/clock.rb +46 -0
- data/samples/processing_app/basics/input/constrain.rb +39 -0
- data/samples/processing_app/basics/input/easing.rb +33 -0
- data/samples/processing_app/basics/input/keyboard.rb +36 -0
- data/samples/processing_app/basics/input/keyboard_2.rb +45 -0
- data/samples/processing_app/basics/input/keyboard_functions.rb +87 -0
- data/samples/processing_app/basics/input/milliseconds.rb +25 -0
- data/samples/processing_app/basics/input/mouse_1d.rb +34 -0
- data/samples/processing_app/basics/input/mouse_2d.rb +25 -0
- data/samples/processing_app/basics/input/mouse_functions.rb +73 -0
- data/samples/processing_app/basics/input/mouse_press.rb +21 -0
- data/samples/processing_app/basics/input/mouse_signals.rb +45 -0
- data/samples/processing_app/basics/input/storing_input.rb +39 -0
- data/samples/processing_app/basics/math/additive_wave.rb +66 -0
- data/samples/processing_app/basics/math/arctangent.rb +54 -0
- data/samples/processing_app/topics/simulate/chain.rb +82 -0
- data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +166 -0
- data/samples/processing_app/topics/simulate/simple_particle_system.rb +113 -0
- data/samples/processing_app/topics/simulate/spring.rb +85 -0
- data/samples/processing_app/topics/simulate/springs.rb +125 -0
- metadata +93 -2
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Processings datatype conversion functions make no sense for Ruby as it does
|
4
|
+
# not have primitive datatypes. Try these instead:
|
5
|
+
# "to_s" (to String)
|
6
|
+
# "to_i" (to Integer, thats a Fixnum or Bignum)
|
7
|
+
# "to_f" (to Float, which would be "double" in Processing, not "float")
|
8
|
+
# "to_a" (to Array, i.e. from a Range or Hash)
|
9
|
+
|
10
|
+
class DatatypeConversion < Processing::App
|
11
|
+
|
12
|
+
def setup
|
13
|
+
|
14
|
+
# Ruby has no primitive datatypes, everything is an object!
|
15
|
+
# See:
|
16
|
+
[1, 2.0, 'a', "B", nil, false].each do |element|
|
17
|
+
puts
|
18
|
+
puts "#{element.inspect} ... is a #{element.class.name} object"
|
19
|
+
end
|
20
|
+
|
21
|
+
c = 'A' # String (!) as there is no char datatype in Ruby.
|
22
|
+
# Single quotes are parsed without substitutions (i.e. "It is #{Time.now}.")
|
23
|
+
|
24
|
+
f = c[0].to_f # Sets f = 65.0
|
25
|
+
# will become "c.ord.to_f" in Ruby 1.9+
|
26
|
+
|
27
|
+
i = (f * 1.4).to_i # Sets i to 91
|
28
|
+
|
29
|
+
b = (c[0] / 2) # Integer or FixNum as there is no byte in Ruby
|
30
|
+
|
31
|
+
background 51
|
32
|
+
no_stroke
|
33
|
+
|
34
|
+
rect f, 0, 40, 66
|
35
|
+
|
36
|
+
fill 204
|
37
|
+
rect i, 67, 40, 66
|
38
|
+
|
39
|
+
fill 255
|
40
|
+
rect b, 134, 40, 66
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
DatatypeConversion.new :title => "Datatype Conversion", :width => 200, :height => 200
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Integers and floats are two different kinds of numerical data.
|
4
|
+
# An integer (more commonly called an int) is a number without
|
5
|
+
# a decimal point. A float is a floating-point number, which means
|
6
|
+
# it is a number that has a decimal place. Floats are used when
|
7
|
+
# more precision is needed.
|
8
|
+
|
9
|
+
class IntegersFloats < Processing::App
|
10
|
+
|
11
|
+
def setup
|
12
|
+
stroke 255
|
13
|
+
frame_rate 30
|
14
|
+
|
15
|
+
@a = 0 # Create an instance variable "a" of class Integer
|
16
|
+
@b = 0.0 # Create an instance variable "b" of class Float (because of "0.0")
|
17
|
+
end
|
18
|
+
|
19
|
+
def draw
|
20
|
+
background 51
|
21
|
+
|
22
|
+
@a += 1
|
23
|
+
@b += 0.2
|
24
|
+
|
25
|
+
line @a, 0, @a, height/2
|
26
|
+
line @b, height/2, @b, height
|
27
|
+
|
28
|
+
@a = 0 if @a > width
|
29
|
+
@b = 0 if @b > width
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
IntegersFloats.new :title => "Integers Floats", :width => 200, :height => 200
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Boolean data is one bit of information. True or false.
|
4
|
+
# It is common to use Booleans with control statements to
|
5
|
+
# determine the flow of a program. In this example, when the
|
6
|
+
# boolean value "x" is true, vertical black lines are drawn and when
|
7
|
+
# the boolean value "x" is false, horizontal gray lines are drawn.
|
8
|
+
|
9
|
+
# In Ruby, false and nil are "falsy" ... they are the only things
|
10
|
+
# that will fail an "if" test. Absolutely everything else passes "if".
|
11
|
+
|
12
|
+
class TrueFalse < Processing::App
|
13
|
+
|
14
|
+
def setup
|
15
|
+
background 0
|
16
|
+
stroke 0
|
17
|
+
|
18
|
+
(1..width).step(2) do |i|
|
19
|
+
|
20
|
+
x = i < (width/2) # Evaluates to true or false, depending on i
|
21
|
+
|
22
|
+
if x
|
23
|
+
stroke 255
|
24
|
+
line i, 1, i, height-1
|
25
|
+
end
|
26
|
+
|
27
|
+
if !x
|
28
|
+
stroke 126
|
29
|
+
line width/2, i, width-2, i
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
TrueFalse.new :title => "True False", :width => 200, :height => 200
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Variables may either have a global or local "scope".
|
4
|
+
# For example, variables declared within either the
|
5
|
+
# setup or draw functions may be only used in these
|
6
|
+
# functions. Global variables, variables declared outside
|
7
|
+
# of setup and draw, may be used anywhere within the program.
|
8
|
+
# If a local variable is declared with the same name as a
|
9
|
+
# global variable, the program will use the local variable to make
|
10
|
+
# its calculations within the current scope. Variables may be localized
|
11
|
+
# within classes, functions, and iterative statements.
|
12
|
+
|
13
|
+
# Please note that there are some changes on variable scope inside blocks
|
14
|
+
# between Ruby versions 1.8 and 1.9.
|
15
|
+
|
16
|
+
class VariableScope < Processing::App
|
17
|
+
|
18
|
+
def setup
|
19
|
+
background 51
|
20
|
+
stroke 255
|
21
|
+
no_loop
|
22
|
+
|
23
|
+
@a = 20 # Use "@" before the name to create an instance variable ("@a"),
|
24
|
+
# which will be available anywhere inside this instance of "VariableScope".
|
25
|
+
end
|
26
|
+
|
27
|
+
def draw
|
28
|
+
# Draw a line using the instance variable "a",
|
29
|
+
# as returned by its getter function below
|
30
|
+
line a, 0, a, height
|
31
|
+
|
32
|
+
# Create a new variable "a" local to the block (do-end)
|
33
|
+
(50..80).step(2) do |a|
|
34
|
+
line a, 0, a, height
|
35
|
+
end
|
36
|
+
|
37
|
+
# Create a new variable "a" local to the draw method
|
38
|
+
a = 100
|
39
|
+
line a, 0, a, height
|
40
|
+
|
41
|
+
# Make a call to the custom function draw_another_line
|
42
|
+
draw_another_line
|
43
|
+
|
44
|
+
# Make a call to the custom function draw_yet_another_line
|
45
|
+
draw_yet_another_line
|
46
|
+
end
|
47
|
+
|
48
|
+
def draw_another_line
|
49
|
+
# Create a new variable "a" local to this method
|
50
|
+
a = 185
|
51
|
+
|
52
|
+
# Draw a line using the local variable "a"
|
53
|
+
line a, 0, a, height
|
54
|
+
end
|
55
|
+
|
56
|
+
def draw_yet_another_line
|
57
|
+
# Because no new local variable "a" is set, this line draws using the
|
58
|
+
# instance variable "a" (returned by its getter function) which was
|
59
|
+
# set to the value 20 in setup.
|
60
|
+
line a+2, 0, a+2, height
|
61
|
+
end
|
62
|
+
|
63
|
+
# This is a "getter" function, it returns the value of the instance variable "a"
|
64
|
+
def a
|
65
|
+
@a
|
66
|
+
end
|
67
|
+
|
68
|
+
# Ruby can add that getter function for you automatically:
|
69
|
+
# attr_reader :a
|
70
|
+
|
71
|
+
# And this would create a setter which will allow you to set "a" to a value ( "a = 123" )
|
72
|
+
# without using that "@" sign all the time:
|
73
|
+
# attr_writer :a
|
74
|
+
|
75
|
+
# Or you could just have Ruby add both at once with:
|
76
|
+
# attr_accessor :a
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
VariableScope.new :title => "Variable Scope", :width => 200, :height => 200
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Variables are used for storing values. In this example, changing
|
4
|
+
# the values of variables @one and @two significantly changes the composition.
|
5
|
+
|
6
|
+
class Variables < Processing::App
|
7
|
+
|
8
|
+
load_library :control_panel
|
9
|
+
|
10
|
+
def setup
|
11
|
+
stroke 153
|
12
|
+
|
13
|
+
@one = 20 # Change these with the sliders
|
14
|
+
@two = 50
|
15
|
+
|
16
|
+
control_panel do |c|
|
17
|
+
c.slider :one, -20..100
|
18
|
+
c.slider :two, -20..100
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def draw
|
24
|
+
background 0
|
25
|
+
|
26
|
+
c = @one * 8
|
27
|
+
d = @one * 9
|
28
|
+
e = @two - @one
|
29
|
+
f = @two * 2
|
30
|
+
g = f + e
|
31
|
+
|
32
|
+
line @one, f, @two, g
|
33
|
+
line @two, e, @two, g
|
34
|
+
line @two, e, d, c
|
35
|
+
line @one, e, d-e, c
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
Variables.new :title => "Variables", :width => 200, :height => 200
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# The first two parameters for the bezier function specify the
|
4
|
+
# first point in the curve and the last two parameters specify
|
5
|
+
# the last point. The middle parameters set the control points
|
6
|
+
# that define the shape of the curve.
|
7
|
+
|
8
|
+
class Bezier < Processing::App
|
9
|
+
|
10
|
+
def setup
|
11
|
+
background 0
|
12
|
+
stroke 255
|
13
|
+
no_fill
|
14
|
+
smooth
|
15
|
+
|
16
|
+
(0..100).step(20) do |i|
|
17
|
+
bezier 90-(i/2.0), 20+i, 210, 10, 220, 150, 120-(i/8.0), 150+(i/4.0)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
Bezier.new :title => "Bezier", :width => 200, :height => 200
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Bezier Ellipse
|
4
|
+
# By Ira Greenberg
|
5
|
+
#
|
6
|
+
# Generates an ellipse using bezier and
|
7
|
+
# trig functions. Approximately every 1/2
|
8
|
+
# second a new ellipse is plotted using
|
9
|
+
# random values for control/anchor points.
|
10
|
+
|
11
|
+
class BezierEllipse < Processing::App
|
12
|
+
|
13
|
+
# have Ruby add getter/setter functions for these,
|
14
|
+
# so we don't have to write "@var" all the time
|
15
|
+
attr_accessor :px, :py, :cx, :cy, :cx2, :cy2
|
16
|
+
|
17
|
+
def setup
|
18
|
+
@color_controls = 0xFF222222
|
19
|
+
@color_anchors = 0xFFBBBBBB
|
20
|
+
|
21
|
+
smooth
|
22
|
+
rect_mode CENTER
|
23
|
+
set_ellipse 4, 65, 65
|
24
|
+
frame_rate 1
|
25
|
+
end
|
26
|
+
|
27
|
+
def draw
|
28
|
+
background 145
|
29
|
+
draw_ellipse
|
30
|
+
set_ellipse random(3, 12).to_i, random(-100, 150), random(-100, 150)
|
31
|
+
end
|
32
|
+
|
33
|
+
def draw_ellipse
|
34
|
+
|
35
|
+
number_of_points = px.length # all our arrays have same length
|
36
|
+
|
37
|
+
# draw shape
|
38
|
+
|
39
|
+
stroke_weight 1.125
|
40
|
+
stroke 255
|
41
|
+
no_fill
|
42
|
+
|
43
|
+
number_of_points.times do |i|
|
44
|
+
i2 = (i+1) % number_of_points # wrap around to make a loop
|
45
|
+
bezier px[i], py[i],
|
46
|
+
cx[i], cy[i],
|
47
|
+
cx2[i], cy2[i],
|
48
|
+
px[i2], py[i2]
|
49
|
+
end
|
50
|
+
|
51
|
+
# draw lines and handles
|
52
|
+
|
53
|
+
stroke_weight 0.75
|
54
|
+
stroke 0
|
55
|
+
|
56
|
+
number_of_points.times do |i|
|
57
|
+
i2 = (i+1) % number_of_points
|
58
|
+
line px[i], py[i], cx[i], cy[i] # anchor to control 1
|
59
|
+
line cx2[i], cy2[i], px[i2], py[i2] # control 2 to next anchor
|
60
|
+
end
|
61
|
+
|
62
|
+
number_of_points.times do |i|
|
63
|
+
fill @color_controls
|
64
|
+
no_stroke
|
65
|
+
|
66
|
+
ellipse cx[i], cy[i], 4, 4
|
67
|
+
ellipse cx2[i], cy2[i], 4, 4
|
68
|
+
|
69
|
+
fill @color_anchors
|
70
|
+
stroke 0
|
71
|
+
|
72
|
+
rect px[i], py[i], 5, 5
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def set_ellipse( points, radius, control_radius )
|
77
|
+
|
78
|
+
# first time we come here the instance variables are created
|
79
|
+
# therefore we need to use "@" or "self.". the former will access the variable
|
80
|
+
# directly the later will use the setter function created by "attr_accessor".
|
81
|
+
@px , self.py , @cx , @cy , @cx2 , @cy2 = [], [], [], [], [], []
|
82
|
+
|
83
|
+
angle = 360.0/points
|
84
|
+
control_angle_1 = angle/3.0
|
85
|
+
control_angle_2 = control_angle_1*2.0
|
86
|
+
points.times do |i|
|
87
|
+
px[i] = width/2+cos(angle.radians)*radius
|
88
|
+
|
89
|
+
py[i] = height/2+sin(angle.radians)*radius
|
90
|
+
|
91
|
+
cx[i] = width/2+cos((angle+control_angle_1).radians)*
|
92
|
+
control_radius/cos((control_angle_1).radians)
|
93
|
+
|
94
|
+
cy[i] = height/2+sin((angle+control_angle_1).radians)*
|
95
|
+
control_radius/cos((control_angle_1).radians)
|
96
|
+
|
97
|
+
cx2[i] = width/2+cos((angle+control_angle_2).radians)*
|
98
|
+
control_radius/cos((control_angle_1).radians)
|
99
|
+
|
100
|
+
cy2[i] = height/2+sin((angle+control_angle_2).radians)*
|
101
|
+
control_radius/cos((control_angle_1).radians)
|
102
|
+
|
103
|
+
#increment angle so trig functions keep chugging along
|
104
|
+
angle += 360.0/points
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
BezierEllipse.new :title => "Bezier Ellipse", :width => 200, :height => 200
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Pie Chart
|
4
|
+
# By Ira Greenberg
|
5
|
+
#
|
6
|
+
# Uses the arc() function to generate a pie chart from the data
|
7
|
+
# stored in an array.
|
8
|
+
|
9
|
+
class PieChart < Processing::App
|
10
|
+
|
11
|
+
def setup
|
12
|
+
background 100
|
13
|
+
smooth
|
14
|
+
no_stroke
|
15
|
+
|
16
|
+
diameter = 150
|
17
|
+
angles = [30, 10, 45, 35 ,60, 38, 75, 67]
|
18
|
+
last_angle = 0.0
|
19
|
+
|
20
|
+
angles.each do |angle|
|
21
|
+
fill angle * 3.0
|
22
|
+
arc width/2, height/2, # center x, y
|
23
|
+
diameter, diameter, # width, height
|
24
|
+
last_angle, last_angle + radians(angle) # angles from, to
|
25
|
+
last_angle += radians(angle)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
PieChart.new :title => "Pie Chart", :width => 200, :height => 200
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'ruby-processing'
|
2
|
+
|
3
|
+
# Constructing a simple dimensional form with lines and rectangles.
|
4
|
+
# Changing the value of the variable 'd' scales the image.
|
5
|
+
# The four variables set the positions based on the value of 'd'.
|
6
|
+
|
7
|
+
class PointsLines < Processing::App
|
8
|
+
|
9
|
+
def setup
|
10
|
+
d = 40
|
11
|
+
p1 = d
|
12
|
+
p2 = p1+d
|
13
|
+
p3 = p2+d
|
14
|
+
p4 = p3+d
|
15
|
+
|
16
|
+
background 0
|
17
|
+
|
18
|
+
# Draw gray box
|
19
|
+
stroke 153
|
20
|
+
line p3, p3, p2, p3
|
21
|
+
line p2, p3, p2, p2
|
22
|
+
line p2, p2, p3, p2
|
23
|
+
line p3, p2, p3, p3
|
24
|
+
|
25
|
+
# Draw white points
|
26
|
+
stroke 255
|
27
|
+
point p1, p1
|
28
|
+
point p1, p3
|
29
|
+
point p2, p4
|
30
|
+
point p3, p1
|
31
|
+
point p4, p2
|
32
|
+
point p4, p4
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
PointsLines.new :title => "Points Lines", :width => 200, :height => 200
|