ruby-processing 2.4.3 → 2.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/CHANGELOG +5 -0
  4. data/README.md +4 -2
  5. data/lib/ruby-processing/exporters/application_exporter.rb +1 -0
  6. data/lib/ruby-processing/version.rb +1 -1
  7. data/library/boids/boids.rb +14 -13
  8. data/library/vecmath/lib/vec.rb +20 -4
  9. data/samples/contributed/circle_collision.rb +92 -149
  10. data/samples/contributed/drawolver.rb +13 -27
  11. data/samples/contributed/elegant_ball.rb +158 -0
  12. data/samples/contributed/fern.rb +16 -3
  13. data/samples/contributed/flight_patterns.rb +16 -4
  14. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/bumpy_surface_noise.rb +4 -9
  15. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/data/java_args.txt +0 -0
  16. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/custom_shape/custom_shape.rb +1 -1
  17. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/particle_system/particle_system.rb +7 -10
  18. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/surface/surface.rb +2 -2
  19. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/liquidy.rb +7 -7
  20. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/polygons.rb +4 -9
  21. data/samples/external_library/java_processing/custom/README.md +15 -0
  22. data/samples/external_library/java_processing/custom/Rakefile +28 -0
  23. data/samples/external_library/java_processing/custom/landscape.rb +58 -0
  24. data/samples/external_library/java_processing/custom/src/nn/Connection.java +47 -0
  25. data/samples/external_library/java_processing/custom/src/nn/HiddenNeuron.java +20 -0
  26. data/samples/external_library/java_processing/custom/src/nn/InputNeuron.java +23 -0
  27. data/samples/external_library/java_processing/custom/src/nn/Network.java +136 -0
  28. data/samples/external_library/java_processing/custom/src/nn/Neuron.java +79 -0
  29. data/samples/external_library/java_processing/custom/src/nn/OutputNeuron.java +7 -0
  30. data/samples/external_library/java_processing/custom/xor.rb +88 -0
  31. data/samples/external_library/ruby_gem/README +1 -1
  32. data/samples/external_library/ruby_gem/data/data.json +8 -0
  33. data/samples/external_library/ruby_gem/draw_test.rb +171 -0
  34. data/samples/processing_app/basics/form/icosahedra.rb +39 -0
  35. data/samples/processing_app/basics/form/library/icosahedron/icosahedron.rb +60 -0
  36. data/samples/processing_app/basics/form/toroid.rb +78 -92
  37. data/samples/processing_app/basics/transform/birds.rb +6 -12
  38. data/samples/processing_app/basics/transform/cubes_in_cube.rb +25 -22
  39. data/samples/processing_app/basics/transform/library/cube/cube.rb +16 -16
  40. data/samples/processing_app/basics/transform/rotate_push_pop.rb +1 -1
  41. data/samples/processing_app/demos/graphics/bezier_patch.rb +18 -31
  42. data/samples/processing_app/demos/graphics/trefoil.rb +15 -15
  43. data/samples/processing_app/library/vecmath/acceleration_with_vectors.rb +3 -3
  44. data/samples/processing_app/library/vecmath/hilbert_fractal.rb +2 -2
  45. data/samples/processing_app/library/vecmath/library/flock/flock.rb +18 -21
  46. data/samples/processing_app/library/vecmath/library/hilbert/hilbert.rb +11 -8
  47. data/samples/processing_app/library/vecmath/library/wiggler/wiggler.rb +7 -15
  48. data/samples/processing_app/library/vecmath/seeking_neural.rb +172 -0
  49. data/samples/processing_app/topics/animation/animated_sprite.rb +5 -8
  50. data/samples/processing_app/topics/animation/sequential.rb +2 -3
  51. data/samples/processing_app/topics/create_shapes/library/particle/particle_system.rb +7 -7
  52. data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +2 -2
  53. data/samples/processing_app/topics/create_shapes/wiggle_pshape.rb +2 -1
  54. data/samples/processing_app/topics/lsystems/koch.rb +1 -1
  55. data/samples/processing_app/topics/lsystems/library/koch/koch_fractal.rb +24 -23
  56. data/samples/processing_app/topics/motion/circle_collision.rb +117 -160
  57. data/samples/processing_app/topics/motion/library/cube/cube.rb +1 -1
  58. data/samples/processing_app/topics/motion/morph.rb +1 -1
  59. data/samples/processing_app/topics/motion/reflection1.rb +17 -16
  60. data/samples/processing_app/topics/shaders/conway.rb +2 -2
  61. data/samples/processing_app/topics/shaders/data/conway.glsl +10 -10
  62. data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +9 -8
  63. data/samples/processing_app/topics/shaders/landscape.rb +1 -1
  64. data/samples/processing_app/topics/simulate/flocking.rb +1 -1
  65. data/samples/processing_app/topics/simulate/library/flock/flock.rb +62 -57
  66. data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +8 -28
  67. data/samples/processing_app/topics/simulate/simple_particle_system.rb +9 -7
  68. data/samples/processing_app/topics/simulate/smoke_particle_system.rb +12 -11
  69. data/vendors/Rakefile +2 -2
  70. metadata +26 -21
  71. data/samples/contributed/pong.rb +0 -177
  72. data/samples/contributed/simple_buffer.rb +0 -44
  73. data/samples/external_library/java_processing/pbox2d/contact_test.rb +0 -23
  74. data/samples/processing_app/basics/form/icosahedra/icosahedra.rb +0 -72
  75. data/samples/processing_app/basics/form/icosahedra/icosahedron.rb +0 -116
  76. data/samples/processing_app/basics/form/icosahedra/shape_3D.rb +0 -25
@@ -7,6 +7,6 @@ The absolute belt and brace way to use external ruby libraries ie gems:
7
7
  4. Run ruby-processing starting from jruby
8
8
  jruby -S rp5 run sketch.rb
9
9
 
10
- See worked perlin_force and mdarray_test in this folder
10
+ See worked ai4r and mdarray_test in this folder
11
11
  NB: library will need to work with jruby this excludes most native libraries
12
12
  but pure ruby libraries (+java) should be fine
@@ -0,0 +1,8 @@
1
+ {"TRIANGLE":[[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,25,229,229,25,0,0,0,0,0,0],[0,0,0,0,0,0,127,127,127,127,0,0,0,0,0,0],[0,0,0,0,0,25,229,25,25,229,25,0,0,0,0,0],[0,0,0,0,0,127,127,0,0,127,127,0,0,0,0,0],[0,0,0,0,25,229,25,0,0,25,229,25,0,0,0,0],[0,0,0,0,127,127,0,0,0,0,127,127,0,0,0,0],[0,0,0,25,229,25,0,0,0,0,25,229,25,0,0,0],[0,0,0,127,127,0,0,0,0,0,0,127,127,0,0,0],[0,0,25,229,25,0,0,0,0,0,0,25,229,25,0,0],[0,0,127,127,0,0,0,0,0,0,0,0,127,127,0,0],[0,25,229,25,0,0,0,0,0,0,0,0,25,229,25,0],[0,127,127,0,0,0,0,0,0,0,0,0,0,127,127,0],[25,229,25,0,0,0,0,0,0,0,0,0,0,25,229,25],[127,127,0,0,0,0,0,0,0,0,0,0,0,0,127,127],[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]]
2
+ ,"SQUARE":[[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]]
3
+ ,"CROSS":[[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127],[127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0]]
4
+ ,"CIRCLE":[[0,0,0,0,32,64,64,80,80,64,64,32,0,0,0,0],[0,0,32,64,96,103,64,64,64,64,96,96,64,32,0,0],[0,32,96,128,96,32,0,0,0,0,32,89,128,96,32,0],[0,64,128,96,18,0,0,0,0,0,0,0,64,128,64,0],[32,96,96,18,0,0,0,0,0,0,0,0,0,82,101,32],[64,103,32,0,0,0,0,0,0,0,0,0,0,32,96,64],[64,64,0,0,0,0,0,0,0,0,0,0,0,0,64,68],[80,64,0,0,0,0,0,0,0,0,0,0,0,0,64,104],[80,64,0,0,0,0,0,0,0,0,0,0,0,0,64,106],[64,64,0,0,0,0,0,0,0,0,0,0,0,0,64,70],[64,96,32,0,0,0,0,0,0,0,0,0,0,32,96,64],[32,96,88,0,0,0,0,0,0,0,0,0,0,64,119,32],[0,64,128,64,0,0,0,0,0,0,0,0,32,113,70,0],[0,32,96,128,81,32,0,0,0,0,32,64,113,96,32,0],[0,0,32,64,102,96,64,64,64,64,96,119,70,32,0,0],[0,0,0,0,32,64,69,105,106,70,64,32,0,0,0,0]]
5
+ ,"TRIANGLE_WITH_NOISE":[[25,0,0,0,0,0,0,25,127,0,0,25,0,0,0,0],[0,0,0,0,76,0,25,229,229,25,0,0,0,0,76,0],[0,76,0,0,0,0,127,25,127,76,0,0,0,0,0,178],[0,0,0,178,0,25,229,25,25,229,25,0,0,0,76,0],[0,0,0,0,0,76,127,0,76,127,127,0,0,0,0,0],[0,25,0,0,25,229,25,0,25,25,229,25,0,0,0,0],[25,0,0,0,127,127,0,0,0,0,127,127,178,0,0,76],[0,0,76,76,229,25,0,0,25,0,25,229,25,0,0,0],[0,0,0,127,127,0,76,178,0,0,0,127,127,0,0,0],[0,0,25,229,25,0,0,0,0,0,0,25,229,25,0,0],[0,0,127,127,0,0,0,0,76,0,0,0,127,127,0,0],[0,25,229,25,0,0,0,0,0,0,0,0,25,229,25,0],[0,127,127,0,76,0,0,76,0,0,0,0,0,127,127,0],[25,229,25,0,0,76,0,0,0,25,0,0,0,25,229,25],[127,127,0,0,0,0,0,0,0,0,0,0,0,0,127,127],[255,255,255,255,25,255,255,255,255,255,25,255,255,255,255,255]]
6
+ ,"SQUARE_WITH_NOISE":[[255,76,255,255,255,153,255,255,255,255,255,102,255,255,255,255],[255,0,0,0,0,178,0,0,0,0,0,0,0,0,0,255],[255,0,76,0,0,0,0,178,0,153,25,0,0,0,0,0],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,102,0,102,0,0,0,25,0,76,0,0,102,0,255],[255,0,0,0,0,0,0,25,0,0,0,0,0,0,0,0],[255,0,0,0,76,153,0,0,25,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,102,0,0,0,0,178,0,0,255],[255,102,102,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,0,178,0,0,76,0,0,0,0,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,76,0,0,0,0,0,178,255],[255,0,76,0,102,0,0,0,0,153,0,0,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,25,0,0,255],[255,0,0,153,0,0,0,178,0,0,0,178,0,0,0,255],[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255],[255,255,255,255,76,255,255,255,255,0,255,255,25,255,25,255]]
7
+ ,"CROSS_WITH_NOISE":[[0,0,0,0,0,0,76,76,127,0,76,0,0,0,25,0],[0,25,0,0,0,25,0,127,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,76,0,0,0],[0,0,25,204,0,0,0,127,127,0,102,0,0,0,25,0],[0,0,0,0,0,76,0,127,0,0,0,0,25,0,0,0],[0,0,0,204,0,0,0,127,127,0,0,0,0,0,0,25],[0,0,0,0,0,0,0,127,127,0,76,0,0,0,0,0],[127,127,127,204,127,76,127,127,127,127,127,127,127,127,0,5],[127,127,127,127,127,127,127,127,25,127,127,127,127,25,0,0],[0,0,0,204,0,0,0,102,127,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,102,0,0,0,0,0,0],[0,0,0,0,0,102,0,127,127,0,0,0,0,0,0,0],[102,0,0,102,0,0,0,127,127,0,0,0,25,0,0,0],[0,0,0,0,0,25,0,127,102,102,76,0,0,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,255,0,0,0],[0,0,0,0,0,0,0,127,127,0,0,0,0,0,0,0]]
8
+ ,"CIRCLE_WITH_NOISE":[[0,0,0,0,21,0,21,128,128,21,0,21,0,0,0,0],[0,0,8,17,128,128,110,0,0,21,128,128,98,21,0,0],[0,8,21,128,21,21,0,0,0,0,21,21,128,21,21,0],[0,0,128,21,0,0,0,0,0,0,0,0,21,128,2,0],[21,128,21,0,0,0,0,0,0,0,0,0,0,21,128,21],[0,128,42,0,0,0,0,0,0,0,0,0,0,31,128,0],[0,25,0,0,0,0,0,0,0,0,0,0,0,0,21,21],[128,0,0,0,0,0,0,0,0,0,0,0,0,0,89,128],[23,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128],[21,24,12,0,0,0,0,0,0,0,0,0,0,0,128,21],[0,128,21,0,0,0,0,0,0,0,0,0,0,32,128,0],[21,42,21,21,0,0,0,0,0,0,0,0,0,21,128,27],[0,13,128,21,21,0,0,0,0,0,0,0,21,128,86,0],[0,3,21,128,21,31,0,0,0,0,21,21,128,21,0,0],[0,0,0,88,128,128,128,3,89,128,128,128,21,0,0,0],[0,0,0,0,21,0,21,128,128,21,0,21,0,0,0,0]]}
@@ -0,0 +1,171 @@
1
+ #####################################################################
2
+ # Using the ai4r gem in ruby-processing.
3
+ # A simple example that demonstrates using
4
+ # a backpropagation neural network. Use the drop box menu to
5
+ # select a prebuilt shape. To draw a test shape tick drawing checkbox,
6
+ # release the mouse when drawing a discontinous shape eg cross.
7
+ # Clear the sketch with clear button.
8
+ # Press evaluate and result is printed to the console....
9
+ ####################################################################
10
+
11
+ require 'ai4r'
12
+ require 'json'
13
+
14
+ load_library :vecmath, :control_panel
15
+
16
+ attr_reader :img, :img_pixels, :ci_input, :cr_input, :tr_input, :sq_input, :net, :points, :panel, :hide, :drawing, :source_string
17
+
18
+ def setup
19
+ size(320, 320)
20
+ control_panel do |c|
21
+ c.title = "control"
22
+ c.look_feel "Nimbus"
23
+ c.checkbox :drawing
24
+ c.button :clear
25
+ c.button :evaluate
26
+ c.menu :shape, ['CIRCLE', 'CROSS', 'CROSS_WITH_NOISE', 'SQUARE', 'SQUARE_WITH_NOISE', 'TRIANGLE', 'DEFAULT']
27
+ @panel = c
28
+ end
29
+ @hide = false
30
+ @source_string = open("data/data.json", "r"){ |file| file.read }
31
+ triangle = JSON.parse(source_string)["TRIANGLE"]
32
+ square = JSON.parse(source_string)["SQUARE"]
33
+ cross = JSON.parse(source_string)["CROSS"]
34
+ circle = JSON.parse(source_string)["CIRCLE"]
35
+ @points = []
36
+ srand 1
37
+ @net = Ai4r::NeuralNetwork::Backpropagation.new([256, 3])
38
+ @tr_input = triangle.flatten.collect { |input| input.to_f / 127.0}
39
+ @sq_input = square.flatten.collect { |input| input.to_f / 127.0}
40
+ @cr_input = cross.flatten.collect { |input| input.to_f / 127.0}
41
+ @ci_input = circle.flatten.collect { |input| input.to_f / 127.0}
42
+ train
43
+ background 255
44
+ end
45
+
46
+
47
+ def draw
48
+ # only make control_panel visible once, or again when hide is false
49
+ unless hide
50
+ @hide = true
51
+ panel.set_visible(hide)
52
+ end
53
+ if drawing
54
+ stroke_weight 32
55
+ stroke 127
56
+ points.each_cons(2) { |ps, pe| line ps.x, ps.y, pe.x, pe.y}
57
+ else
58
+ no_fill
59
+ stroke_weight(32)
60
+ stroke(127)
61
+ case @shape
62
+ when 'CIRCLE'
63
+ background(255)
64
+ draw_circle
65
+ @shape = 'DEFAULT'
66
+ when 'CROSS'
67
+ background(255)
68
+ draw_cross
69
+ @shape = 'DEFAULT'
70
+ when 'CROSS_WITH_NOISE','SQUARE_WITH_NOISE'
71
+ background(255)
72
+ draw_shape @shape
73
+ @shape = 'DEFAULT'
74
+ when 'SQUARE'
75
+ background(255)
76
+ draw_square
77
+ @shape = 'DEFAULT'
78
+ when 'TRIANGLE'
79
+ background(255)
80
+ draw_triangle
81
+ @shape = 'DEFAULT'
82
+ end
83
+ end
84
+ end
85
+
86
+ def draw_shape shp
87
+ shape = JSON.parse(source_string)[shp]
88
+ background(255)
89
+ no_stroke
90
+ (0 ... width / 20).each do |i|
91
+ (0 ... height / 20).each do |j|
92
+ col = 255 - shape[i][j]
93
+ fill(col)
94
+ rect(i * 20, j * 20, 20, 20)
95
+ end
96
+ end
97
+ end
98
+
99
+ def train
100
+ puts "Training Network Please Wait"
101
+ 101.times do |i|
102
+ error = net.train(tr_input, [1.0, 0, 0])
103
+ error = net.train(sq_input, [0, 1.0, 0])
104
+ error = net.train(cr_input, [0, 0, 1.0])
105
+ error = net.train(ci_input, [0, 1.0, 1.0])
106
+ puts "Error after iteration #{i}:\t#{format("%.5f", error)}" if i%20 == 0
107
+ end
108
+ end
109
+
110
+ def result_label(result)
111
+ if result.inject(0, :+).between?(1.9, 2.1)
112
+ if result[0] < 0.01 && result[1].between?(0.99, 1.0) && result[2].between?(0.99, 1.0)
113
+ return "CIRCLE"
114
+ else
115
+ return "UNKNOWN"
116
+ end
117
+ elsif result.inject(0, :+).between?(0.95, 1.1)
118
+ if result[0].between?(0.95, 1.0) && (result[1] + result[2]) < 0.01
119
+ return "TRIANGLE"
120
+ elsif result[1].between?(0.95, 1.0) && (result[0] + result[2]) < 0.01
121
+ return "SQUARE"
122
+ elsif result[2].between?(0.95, 1.0) && (result[1] + result[0]) < 0.01
123
+ return "CROSS"
124
+ else
125
+ return "UNKNOWN"
126
+ end
127
+ end
128
+ return "UNKNOWN"
129
+ end
130
+
131
+ def mouse_dragged
132
+ points << Vec2D.new(mouse_x, mouse_y)
133
+ end
134
+
135
+ def mouse_released
136
+ points.clear
137
+ end
138
+
139
+ def draw_circle
140
+ ellipse(width / 2, height / 2, 320 - 32, 320 - 32)
141
+ end
142
+
143
+ def draw_square
144
+ rect(16, 16, 320 - 32, 320 - 32)
145
+ end
146
+
147
+ def draw_cross
148
+ line(width / 2, 0, width / 2, 320)
149
+ line(0, height / 2, 320 , height / 2)
150
+ end
151
+
152
+ def draw_triangle
153
+ triangle(width / 2, 32, 24, height - 16, width - 24, height - 16)
154
+ end
155
+
156
+ def clear
157
+ background 255
158
+ end
159
+
160
+ def evaluate
161
+ load_pixels
162
+ img_pixels = []
163
+ (0...height).step(20) do |y|
164
+ row = []
165
+ (0...width).step(20) do |x|
166
+ row << 255 - brightness(pixels[(y + 10) * width + x + 10])
167
+ end
168
+ img_pixels << row
169
+ end
170
+ puts "#{net.eval(img_pixels.flatten).inspect} => #{result_label(net.eval(img_pixels.flatten))}"
171
+ end
@@ -0,0 +1,39 @@
1
+ load_libraries :icosahedron, :vecmath
2
+
3
+ def setup
4
+ size 640, 360, P3D
5
+ @ico1 = Icosahedron.new 75.0
6
+ @ico2 = Icosahedron.new 75.0
7
+ @ico3 = Icosahedron.new 75.0
8
+ end
9
+
10
+ def draw
11
+ background 0
12
+ lights
13
+ translate width/2, height/2
14
+ push_matrix
15
+ translate -width/3.5, 0
16
+ rotate_x frame_count * PI / 185
17
+ rotate_y frame_count * PI / -200
18
+ stroke 170, 0, 0
19
+ no_fill
20
+ @ico1.draw
21
+ pop_matrix
22
+ push_matrix
23
+ rotate_x frame_count * PI / 200
24
+ rotate_y frame_count * PI / 300
25
+ stroke 170, 0, 180
26
+ fill 170, 170, 0
27
+ @ico2.draw
28
+ pop_matrix
29
+ push_matrix
30
+ translate width/3.5, 0
31
+ rotate_x frame_count * PI / -200
32
+ rotate_y frame_count * PI / 200
33
+ no_stroke
34
+ fill 0, 0, 185
35
+ @ico3.draw
36
+ pop_matrix
37
+ end
38
+
39
+
@@ -0,0 +1,60 @@
1
+ class Icosahedron
2
+ include Processing::Proxy # mixin Processing::Proxy
3
+ attr_reader :r
4
+
5
+ def initialize(radius)
6
+ @r = radius
7
+ end
8
+
9
+ ##
10
+ # Draw an icosahedron defined by a radius r.
11
+ #
12
+ def draw
13
+ # Calculate the vertex data for an icosahedron inscribed by a sphere radius 'r'.
14
+ # Use 4 Golden Ratio rectangles as the basis.
15
+ phi = (1.0 + Math.sqrt(5.0)) / 2.0
16
+ h = r / Math.sqrt(1.0 + phi * phi)
17
+ v =
18
+ [
19
+ Vec3D.new(0, -h, h * phi), Vec3D.new(0, -h, -h * phi), Vec3D.new(0, h, -h * phi), Vec3D.new(0, h, h * phi),
20
+ Vec3D.new(h, -h * phi, 0), Vec3D.new(h, h * phi, 0), Vec3D.new(-h, h * phi, 0), Vec3D.new(-h, -h * phi, 0),
21
+ Vec3D.new(-h * phi, 0, h), Vec3D.new(-h * phi, 0, -h), Vec3D.new(h * phi, 0, -h), Vec3D.new(h * phi, 0, h)
22
+ ]
23
+
24
+ begin_shape(TRIANGLES)
25
+
26
+ draw_triangle(v[0], v[7],v[4])
27
+ draw_triangle(v[0], v[4], v[11])
28
+ draw_triangle(v[0], v[11], v[3])
29
+ draw_triangle(v[0], v[3], v[8])
30
+ draw_triangle(v[0], v[8], v[7])
31
+
32
+ draw_triangle(v[1], v[4], v[7])
33
+ draw_triangle(v[1], v[10], v[4])
34
+ draw_triangle(v[10], v[11], v[4])
35
+ draw_triangle(v[11], v[5], v[10])
36
+ draw_triangle(v[5], v[3], v[11])
37
+ draw_triangle(v[3], v[6], v[5])
38
+ draw_triangle(v[6], v[8], v[3])
39
+ draw_triangle(v[8], v[9], v[6])
40
+ draw_triangle(v[9], v[7], v[8])
41
+ draw_triangle(v[7], v[1], v[9])
42
+
43
+ draw_triangle(v[2], v[1], v[9])
44
+ draw_triangle(v[2], v[10], v[1])
45
+ draw_triangle(v[2], v[5], v[10])
46
+ draw_triangle(v[2], v[6], v[5])
47
+ draw_triangle(v[2], v[9], v[6])
48
+
49
+ end_shape
50
+ end
51
+
52
+ def draw_triangle(p1, p2, p3)
53
+
54
+ vertex(p1.x, p1.y, p1.z)
55
+ vertex(p2.x, p2.y, p2.z)
56
+ vertex(p3.x, p3.y, p3.z)
57
+
58
+ end
59
+
60
+ end
@@ -16,112 +16,98 @@
16
16
  # 'w' key _________ toggle wireframe/solid shading
17
17
  # 'h' key _________ toggle sphere/helix
18
18
 
19
+ load_library :vecmath
19
20
 
20
-
21
-
22
- def setup
23
-
24
- size 640, 360, P3D
25
-
26
- @pts = 40
27
- @angle = 0.0
28
- @radius = 60.0
29
- @segments = 60.0
30
- @lathe_angle = 0.0
31
- @lathe_radius = 100.0
32
- @is_wireframe = false
33
- @is_helix = false
34
- @helix_offset = 5.0
35
-
21
+ def setup
22
+ size 640, 360, P3D
23
+ @pts = 40
24
+ @angle = 0.0
25
+ @radius = 60.0
26
+ @segments = 60.0
27
+ @lathe_angle = 0.0
28
+ @lathe_radius = 100.0
29
+ @is_wireframe = false
30
+ @is_helix = false
31
+ @helix_offset = 5.0
36
32
  end
37
33
 
38
- def draw
34
+ def draw
35
+ background 50, 64, 42
36
+ lights
37
+ if @is_wireframe
38
+ stroke 255, 255, 150
39
+ no_fill
40
+ else
41
+ no_stroke
42
+ fill 150, 195, 125
43
+ end
44
+
45
+ translate width/2, height/2, -100
46
+
47
+ rotate_x frame_count * PI / 150
48
+ rotate_y frame_count * PI / 170
49
+ rotate_z frame_count * PI / 90
50
+
51
+ vertices = []
52
+ vertices2 = []
53
+
54
+ 0.upto(@pts) do |i|
39
55
 
40
- background 50, 64, 42
56
+ vertices2[i] = Vec3D.new
57
+ vertices[i] = Vec3D.new
41
58
 
42
- lights
59
+ vertices[i].x = @lathe_radius + sin( radians( @angle ) ) * @radius
43
60
 
44
- if @is_wireframe
45
- stroke 255, 255, 150
46
- no_fill
61
+ if @is_helix
62
+ vertices[i].z = cos( radians( @angle ) ) * @radius - (@helix_offset * @segments) / 2
47
63
  else
48
- no_stroke
49
- fill 150, 195, 125
64
+ vertices[i].z = cos( radians( @angle ) ) * @radius
50
65
  end
51
66
 
52
- translate width/2, height/2, -100
53
-
54
- rotate_x frame_count * PI / 150
55
- rotate_y frame_count * PI / 170
56
- rotate_z frame_count * PI / 90
57
-
58
- vertices = []
59
- vertices2 = []
60
-
61
- 0.upto(@pts) { |i|
62
-
63
- vertices2[i] = PVector.new
64
- vertices[i] = PVector.new
67
+ @angle += 360.0 / @pts
68
+ end
69
+
70
+ @lathe_angle = 0
71
+
72
+ 0.upto(@segments) do |i|
73
+ begin_shape QUAD_STRIP
74
+ (0..@pts).each do |j|
75
+ vertex_for_vector vertices2[j] if i > 0
65
76
 
66
- vertices[i].x = @lathe_radius + sin( radians( @angle ) ) * @radius
77
+ vertices2[j].x = cos( radians( @lathe_angle ) ) * vertices[j].x
78
+ vertices2[j].y = sin( radians( @lathe_angle )
79
+ ) * vertices[j].x
80
+ vertices2[j].z = vertices[j].z
67
81
 
68
- if @is_helix
69
- vertices[i].z = cos( radians( @angle ) ) * @radius - (@helix_offset * @segments) / 2
70
- else
71
- vertices[i].z = cos( radians( @angle ) ) * @radius
72
- end
82
+ vertices[j].z += @helix_offset if @is_helix
73
83
 
74
- @angle += 360.0 / @pts
75
- }
76
-
77
- @lathe_angle = 0
78
-
79
- 0.upto(@segments) { |i|
80
- begin_shape QUAD_STRIP
81
- (0..@pts).each { |j|
82
- vertex_for_pvector vertices2[j] if i > 0
83
-
84
- vertices2[j].x = cos( radians( @lathe_angle ) ) * vertices[j].x
85
- vertices2[j].y = sin( radians( @lathe_angle )
86
- ) * vertices[j].x
87
- vertices2[j].z = vertices[j].z
88
-
89
- vertices[j].z += @helix_offset if @is_helix
90
-
91
- vertex_for_pvector vertices2[j]
92
- }
93
-
94
- @lathe_angle += (@is_helix ? 720 : 360) / @segments
95
- end_shape
96
- }
97
-
84
+ vertex_for_vector vertices2[j]
85
+ end
86
+
87
+ @lathe_angle += (@is_helix ? 720 : 360) / @segments
88
+ end_shape
89
+ end
90
+
98
91
  end
99
92
 
100
- def vertex_for_pvector ( pvec )
101
- vertex( pvec.x, pvec.y, pvec.z )
93
+ def vertex_for_vector ( pvec )
94
+ vertex( pvec.x, pvec.y, pvec.z )
102
95
  end
103
96
 
104
- def key_pressed
105
-
106
- if key == CODED
107
-
108
- @pts += 1 if keyCode == UP && @pts < 40
109
- @pts -= 1 if keyCode == DOWN && @pts > 3
110
-
111
- @segments += 1 if keyCode == RIGHT && @segments < 80
112
- @segments -= 1 if keyCode == LEFT && @segments > 3
113
-
114
- end
115
-
116
- @lathe_radius += 1 if key.eql? "s"
117
- @lathe_radius -= 1 if key.eql? "a" #&& @lathe_radius > 0
118
-
119
- @radius += 1 if key.eql? "x"
120
- @radius -= 1 if key.eql? "z" && @radius > 10
121
-
122
- @is_wireframe = !@is_wireframe if key.eql? "w"
123
-
124
- @is_helix = !@is_helix if key.eql? "h"
125
-
126
- end
97
+ def key_pressed
98
+
99
+ if key == CODED
100
+ @pts += 1 if keyCode == UP && @pts < 40
101
+ @pts -= 1 if keyCode == DOWN && @pts > 3
102
+ @segments += 1 if keyCode == RIGHT && @segments < 80
103
+ @segments -= 1 if keyCode == LEFT && @segments > 3
104
+ end
127
105
 
106
+ @lathe_radius += 1 if key == 's'
107
+ @lathe_radius -= 1 if key == 'a' #&& @lathe_radius > 0
108
+ @radius += 1 if key == 'x'
109
+ @radius -= 1 if key == 'z' && @radius > 10
110
+ @is_wireframe = !@is_wireframe if key == 'w'
111
+ @is_helix = !@is_helix if key == 'h'
112
+ end
113
+