ruby-processing 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. data/CHANGELOG +6 -0
  2. data/lib/core/core.jar +0 -0
  3. data/lib/core/jruby-complete.jar +0 -0
  4. data/lib/ruby-processing.rb +5 -10
  5. data/lib/ruby-processing/app.rb +139 -98
  6. data/lib/ruby-processing/exporters/applet_exporter.rb +4 -1
  7. data/lib/ruby-processing/exporters/application_exporter.rb +5 -1
  8. data/lib/ruby-processing/exporters/base_exporter.rb +3 -2
  9. data/lib/ruby-processing/runner.rb +54 -46
  10. data/lib/ruby-processing/runners/base.rb +6 -7
  11. data/lib/ruby-processing/runners/live.rb +4 -3
  12. data/lib/ruby-processing/runners/watch.rb +33 -27
  13. data/library/control_panel/control_panel.rb +24 -23
  14. data/samples/anar/data/java_args.txt +1 -0
  15. data/samples/anar/extrusion.rb +49 -0
  16. data/samples/anar/l_system.rb +86 -0
  17. data/samples/anar/library/anar/anar.jar +0 -0
  18. data/samples/anar/many_shapes.rb +98 -0
  19. data/samples/empathy.rb +73 -0
  20. data/samples/gravity.rb +113 -0
  21. data/samples/peasy_cam/data/java_args.txt +1 -0
  22. data/samples/peasy_cam/hello_peasy.rb +29 -0
  23. data/samples/peasy_cam/hilbert_fractal.rb +40 -0
  24. data/samples/peasy_cam/library/PeasyCam/PeasyCam.jar +0 -0
  25. data/samples/peasy_cam/library/hilbert/hilbert.rb +99 -0
  26. data/samples/processing_app/3D/form/brick_tower.rb +3 -3
  27. data/samples/processing_app/basics/data/characters_strings/characters_strings.rb +5 -7
  28. data/samples/processing_app/basics/form/triangle_strip.rb +11 -11
  29. data/samples/processing_app/basics/input/keyboard.rb +2 -8
  30. data/samples/processing_app/basics/input/keyboard_2.rb +4 -4
  31. data/samples/processing_app/basics/input/keyboard_functions.rb +12 -21
  32. metadata +41 -64
@@ -0,0 +1,113 @@
1
+ # After Gravity by Christian Hahn
2
+
3
+ attr_reader :particles, :grabbed
4
+
5
+ def setup
6
+ @particles = []
7
+ @grabbed = nil
8
+
9
+ size 600, 500
10
+ background 0
11
+ smooth
12
+ stroke_weight 4
13
+ ellipse_mode CENTER_DIAMETER
14
+ color_mode RGB, 255
15
+ end
16
+
17
+ def draw
18
+ no_stroke
19
+ fill 0, 60
20
+ rect 0, 0, width, height
21
+ @particles.each {|p| p.collect_force; p.move; p.render }
22
+ end
23
+
24
+ def mouse_pressed
25
+ return if mouse_x == 0 || mouse_y == 0
26
+ return if particle_grab
27
+ @particles << Particle.new(mouse_x, mouse_y, rand * 8 + 0.1)
28
+ end
29
+
30
+ def mouse_released
31
+ @grabbed = nil
32
+ end
33
+
34
+ def particle_grab
35
+ @grabbed = @particles.detect {|p| dist(mouse_x, mouse_y, p.x1, p.y1) < p.radius }
36
+ end
37
+
38
+
39
+ class Particle
40
+
41
+ GRAVITY = 1.0
42
+
43
+ attr_reader :x0, :y0, :x1, :y1, :radius, :mass_amount
44
+
45
+ def initialize(x, y, mass)
46
+ @x0, @y0, @x1, @y1 = x, y, x, y
47
+ @x_speed, @y_speed = 0, 0
48
+ @x_accel, @y_accel = 0, 0
49
+ @mass_amount = mass
50
+ @diameter = Math.sqrt(@mass_amount) * 20
51
+ @radius = @diameter / 2.0
52
+ end
53
+
54
+ def collect_force
55
+ @x_accel, @y_accel = 0, 0
56
+ @min_dist = 1000
57
+ $app.particles.each do |p|
58
+ next if p == self
59
+ g_dist = dist(@x0, @y0, p.x0, p.y0)
60
+ g_theta = -(angleOf(@x0, @y0, p.x0, p.y0)).radians
61
+ @min_dist = g_dist if g_dist < @min_dist
62
+ force = (GRAVITY * @mass_amount * p.mass_amount) / g_dist
63
+ if g_dist.abs > @diameter
64
+ @x_accel += force / @mass_amount * Math.cos(g_theta)
65
+ @y_accel += force / @mass_amount * Math.sin(g_theta)
66
+ end
67
+ end
68
+ end
69
+
70
+ def move
71
+ @x_speed, @y_speed = 0, 0 if grabbed?
72
+ @x_speed += @x_accel
73
+ @y_speed += @y_accel
74
+ @x1, @y1 = @x0 + @x_speed, @y0 + @y_speed
75
+ end
76
+
77
+ def grabbed?
78
+ $app.grabbed == self
79
+ end
80
+
81
+ def render
82
+ no_stroke
83
+ grabbed? ? render_grabbed : render_free
84
+ end
85
+
86
+ def render_free
87
+ charge_col = 1000.0 / @min_dist / 50.0
88
+ tot_col_1 = 100 + charge_col * 6
89
+ tot_col_2 = 150 + charge_col*charge_col
90
+ tot_col_3 = @diameter + 8 + charge_col
91
+ fill(tot_col_1, tot_col_1, 255, charge_col * 150 + 3)
92
+ ellipse(@x1, @y1, tot_col_3, tot_col_3)
93
+ fill 0, 255
94
+ stroke tot_col_2, tot_col_2, 255, charge_col * 255 + 3
95
+ ellipse @x1, @y1, @diameter, @diameter
96
+ @x0, @y0 = @x1, @y1
97
+ end
98
+
99
+ def render_grabbed
100
+ fill 150, 150, 255, 100
101
+ ellipse mouse_x, mouse_y, @diameter + 8, @diameter + 8
102
+ fill 0, 255
103
+ stroke 150, 150, 255, 255
104
+ ellipse mouse_x, mouse_y, @diameter, @diameter
105
+ @x0, @y0 = mouse_x, mouse_y
106
+ end
107
+
108
+ def angleOf(x1, y1, x2, y2)
109
+ xd, yd = x1 - x2, y1 - y2
110
+ 180 + (-(180 * Math.atan2(yd, xd)) / PI)
111
+ end
112
+
113
+ end
@@ -0,0 +1 @@
1
+ -Xms256m -Xmx256m
@@ -0,0 +1,29 @@
1
+ load_library 'PeasyCam'
2
+ import 'peasy'
3
+
4
+ attr_reader :cam
5
+
6
+ def setup
7
+ size 200, 200, P3D
8
+ configure_camera
9
+ end
10
+
11
+ def configure_camera
12
+ @cam = PeasyCam.new(self, 100)
13
+ cam.set_minimum_distance 50
14
+ cam.set_maximum_distance 500
15
+ end
16
+
17
+ def draw
18
+ rotate_x -0.5
19
+ rotate_y -0.5
20
+ background 0
21
+ fill 255, 0, 0
22
+ box 30
23
+ push_matrix
24
+ translate 0, 0, 20
25
+ fill 0, 0, 255
26
+ box 5
27
+ pop_matrix
28
+ end
29
+
@@ -0,0 +1,40 @@
1
+ ########################################################
2
+ # A 3D Hilbert fractal implemented using a
3
+ # Lindenmayer System in ruby-processing by Martin Prout
4
+ # Best if you've got opengl
5
+ ########################################################
6
+
7
+ class Hilbert_Test < Processing::App
8
+ full_screen # NB: All distances are relative to screen height
9
+ load_libraries 'hilbert', 'PeasyCam', 'opengl'
10
+ import 'peasy'
11
+ import "processing.opengl" if library_loaded? "opengl"
12
+ attr_reader :hilbert, :cam
13
+
14
+ def setup
15
+ library_loaded?(:opengl) ? configure_opengl : render_mode(P3D)
16
+ configure_peasycam
17
+ @hilbert = Hilbert.new(height)
18
+ hilbert.create_grammar 3
19
+ no_stroke
20
+ end
21
+
22
+ def configure_peasycam
23
+ cam = PeasyCam.new self, height / 6.5
24
+ cam.set_minimum_distance height / 10
25
+ cam.set_maximum_distance height
26
+ end
27
+
28
+ def configure_opengl
29
+ render_mode OPENGL
30
+ hint ENABLE_OPENGL_4X_SMOOTH # optional
31
+ hint DISABLE_OPENGL_ERROR_REPORT # optional
32
+ end
33
+
34
+ def draw
35
+ background 0
36
+ lights
37
+ hilbert.render
38
+ end
39
+ end
40
+
@@ -0,0 +1,99 @@
1
+ ############################
2
+ # Non-stochastic grammar
3
+ # with unique premise/rules
4
+ ############################
5
+ class Grammar
6
+ attr_accessor :axiom, :rules
7
+
8
+ def initialize axiom
9
+ @axiom = axiom
10
+ @rules = Hash.new
11
+ end
12
+
13
+ def add_rule premise, rule
14
+ rules.store(premise, rule)
15
+ end
16
+
17
+ ##########################################
18
+ # replace each pre char with a unique rule
19
+ ##########################################
20
+ def new_production production
21
+ production.gsub!(/./) { |c| (r = @rules[c]) ? r : c }
22
+ end
23
+
24
+ ##########################################
25
+ # control the number of iterations
26
+ # default 0, returns the axiom
27
+ ##########################################
28
+ def generate repeat = 0
29
+ prod = axiom
30
+ repeat.times do
31
+ prod = new_production prod
32
+ end
33
+ return prod
34
+ end
35
+ end
36
+
37
+ ############################
38
+ # Hilbert Curves
39
+ ###########################
40
+ class Hilbert
41
+ include Processing::Proxy
42
+
43
+ attr_reader :grammar, :axiom, :production, :premis, :rule,
44
+ :theta, :scale_factor, :distance, :phi, :len
45
+
46
+ def initialize(len)
47
+ @axiom = "X"
48
+ @grammar = Grammar.new(axiom)
49
+ @production = axiom
50
+ @premis = "X"
51
+ @rule = "^<XF^<XFX-F^>>XFX&F+>>XFX-F>X->"
52
+ @len = len
53
+ @distance = len/12 # distance value relative to screen height
54
+ @theta = Math::PI/180 * 90
55
+ @phi = Math::PI/180 * 90
56
+ grammar.add_rule(premis, rule)
57
+ no_stroke()
58
+ end
59
+
60
+ def render()
61
+ translate(-len/42, len/42, -len/42) # use the "answer?" to center the Hilbert
62
+ fill(0, 75, 152)
63
+ light_specular(204, 204, 204)
64
+ specular(255, 255, 255)
65
+ shininess(1.0)
66
+ production.scan(/./) do |ch|
67
+ case(ch)
68
+ when "F"
69
+ translate(0, distance/-2, 0)
70
+ box(distance/9 , distance, distance/9)
71
+ translate(0, distance/-2, 0)
72
+ when "+"
73
+ rotateX(-theta)
74
+ when "-"
75
+ rotateX(theta)
76
+ when ">"
77
+ rotateY(theta)
78
+ when "<"
79
+ rotateY(-theta)
80
+ when "&"
81
+ rotateZ(-phi)
82
+ when "^"
83
+ rotateZ(phi)
84
+ when "X"
85
+ else
86
+ puts("character '#{ch}' not in grammar")
87
+ end
88
+ end
89
+ end
90
+ ##############################
91
+ # create grammar from axiom and
92
+ # rules (adjust scale)
93
+ ##############################
94
+
95
+ def create_grammar(gen)
96
+ @distance *= 0.5**gen
97
+ @production = @grammar.generate gen
98
+ end
99
+ end
@@ -77,9 +77,9 @@ class Cubeish
77
77
 
78
78
  def create
79
79
  @vertices.each do |name, vectors|
80
- begin_shape App::QUADS
81
- vectors.each {|v| vertex(v.x, v.y, v.z) }
82
- end_shape
80
+ shape(App::QUADS) do
81
+ vectors.each {|v| vertex(v.x, v.y, v.z) }
82
+ end
83
83
  end
84
84
  end
85
85
 
@@ -64,18 +64,16 @@ class CharactersStrings < Processing::App
64
64
  def key_pressed
65
65
  # The variable "key" always contains the value of the most recent key pressed.
66
66
  # If the key is an upper or lowercase letter between 'A' and 'z'
67
- # the image is shifted to the corresponding value of that key
68
-
69
- if (key >= "A"[0]) and (key <= "z"[0]) # should become "A".ord and "z".ord in Ruby 1.9
70
-
67
+ # the image is shifted to the corresponding value of that key
68
+ if ('A'..'z').include? key
69
+
71
70
  # Map the index of the key pressed from the range between 'A' and 'z',
72
71
  # into a position for the left edge of the image. The maximum xoffset
73
72
  # is the width of the drawing area minus the size of the image.
74
-
75
- @xoffset = map( key, "A"[0], "z"[0], 0, width - @frog.width ).to_i
73
+ @xoffset = map( key[0], "A"[0], "z"[0], 0, width - @frog.width ).to_i
76
74
 
77
75
  # Update the letter shown to the screen
78
- @letter = key.chr
76
+ @letter = key
79
77
 
80
78
  # Write the letter to the console
81
79
  puts key
@@ -23,19 +23,19 @@ class TriangleStrip < Processing::App
23
23
  number_of_points = 36
24
24
  rotation = 360.0/number_of_points
25
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
26
+ shape(TRIANGLE_STRIP) do
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
32
 
33
- px = x + cos(angle.radians)*inner_radius
34
- py = y + sin(angle.radians)*inner_radius
35
- angle += rotation
36
- vertex px, py
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
37
38
  end
38
- end_shape
39
39
  end
40
40
 
41
41
  end
@@ -17,17 +17,11 @@ class Keyboard < Processing::App
17
17
  end
18
18
 
19
19
  def draw
20
- if key_pressed?
21
- if key >= ("A"[0]) && key <= ("z"[0])
22
- if key <= ("Z"[0])
23
- @key_index = key - ("A"[0])
24
- else
25
- @key_index = key - ("a"[0])
26
- end
20
+ if key_pressed? && ('A'..'z').include?(key)
21
+ @key_index = key.ord - (key <= 'Z' ? 'A'.ord : 'a'.ord)
27
22
  fill millis % 255
28
23
  begin_rect = @rect_width/2 + @key_index * @key_scale - @rect_width/2
29
24
  rect begin_rect, 0, @rect_width, height
30
- end
31
25
  end
32
26
  end
33
27
 
@@ -17,11 +17,11 @@ class Keyboard2 < Processing::App
17
17
 
18
18
  def draw
19
19
  if key_pressed?
20
- if key.chr && key.chr >= "A" && key.chr <= "z" # added testing 'gainst Nil
21
- if key.chr <= "Z"
22
- @key_index = key - "A".ord
20
+ if ('A'..'z').include? key
21
+ if key <= "Z"
22
+ @key_index = key.ord - "A".ord
23
23
  else
24
- @key_index = key - "a".ord
24
+ @key_index = key.ord - "a".ord
25
25
  end
26
26
  fill millis % 255
27
27
  begin_rect = @rect_width/2 + @key_index * @key_scale - @rect_width/2
@@ -28,14 +28,14 @@ class KeyboardFunctions < Processing::App
28
28
  def draw
29
29
  if new_letter?
30
30
  if upcase?
31
- fill (key - "A".ord).abs % 255
31
+ fill (key.ord - "A".ord).abs % 255
32
32
  rect @x, @y, @letter_width, @letter_height*2
33
33
  else
34
34
  # clear with letter space with background color
35
35
  fill @num_chars/2
36
36
  rect @x, @y, @letter_width, @letter_height
37
37
 
38
- fill (key - "a".ord).abs % 255
38
+ fill (key.ord - "a".ord).abs % 255
39
39
  rect @x, @y+@letter_height, @letter_width, @letter_height
40
40
  end
41
41
  @new_letter = false
@@ -51,26 +51,17 @@ class KeyboardFunctions < Processing::App
51
51
  end
52
52
 
53
53
  def key_pressed
54
- # Try to get a String for int value in "key"
55
- key_char = nil
56
- key_char = key.chr rescue nil
57
-
58
- # if the key is between 'A'(65) and 'z'(122)
59
- if key_char && key_char >= "A" && key_char <= "z"
60
- if key_char <= "Z"
61
- @upcase = true
62
- else
63
- @upcase = false
64
- end
54
+ if ('A'..'z').include? key
55
+ @upcase = key <= "Z"
56
+ @new_letter = true
57
+
58
+ # Update the "letter" position and
59
+ # wrap horizontally and vertically
60
+ @y += (@letter_height*2) if @x + @letter_width >= width
61
+ @y = @y % height
62
+ @x += @letter_width
63
+ @x = @x % width
65
64
  end
66
- @new_letter = true
67
-
68
- # Update the "letter" position and
69
- # wrap horizontally and vertically
70
- @y += (@letter_height*2) if @x + @letter_width >= width
71
- @y = @y % height
72
- @x += @letter_width
73
- @x = @x % width
74
65
  end
75
66
 
76
67
  end