Skip to content

Commit fef23a5

Browse files
committed
re-arrange some samples
1 parent 7fbce60 commit fef23a5

File tree

17 files changed

+576
-144
lines changed

17 files changed

+576
-144
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ Like the original `ruby-processing`, `JRubyArt` is like a DSL for vanilla `proce
5353

5454
6. Others
5555
1. [WOVNS patterns][wovns]
56+
2. [Grid Method][grid]
5657

5758
### User contributions are most welcome
5859
[Contributions][] add your [own][]
5960

6061
[liquid]:https://github.com/ruby-processing/JRubyArt-examples/tree/master/external_library/java/LiquidFunProcessing
6162
[flow]:https://github.com/ruby-processing/JRubyArt-examples/tree/master/external_library/java/PixelFlow
6263
[wovns]:https://github.com/ruby-processing/JRubyArt-examples/tree/master/examples/WOVNS
64+
[grid]:https://github.com/ruby-processing/JRubyArt-examples/tree/master/examples/grid_method
6365
[Learning Processing with Ruby]:https://github.com/ruby-processing/learning-processing-with-ruby
6466
[Nature of Code Examples in ruby]:https://github.com/ruby-processing/The-Nature-of-Code-for-JRubyArt
6567
[Contributions]:https://github.com/ruby-processing/JRubyArt-examples/tree/master/contributed

contributed/Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Simple demo Rakefile to autorun samples in current directory
2-
# adjust path to rp5 executable, and or opts as required
2+
# adjust path to k9 executable, and or opts as required
33

44
SAMPLES_DIR = './'
55

examples/grid_method/Rakefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Simple demo Rakefile to autorun samples in current directory
2+
# adjust path to k9 executable, and or opts as required
3+
4+
SAMPLES_DIR = './'
5+
6+
desc 'run demo'
7+
task default: [:demo]
8+
9+
desc 'demo'
10+
task :demo do
11+
samples_list.shuffle.each { |sample| run_sample sample }
12+
end
13+
14+
def samples_list
15+
files = []
16+
Dir.chdir(SAMPLES_DIR)
17+
Dir.glob('*.rb').each do |file|
18+
files << File.join(SAMPLES_DIR, file)
19+
end
20+
return files
21+
end
22+
23+
def run_sample(sample_name)
24+
puts "Running #{sample_name}...quit to run next sample"
25+
open("|k9 -r #{sample_name}", 'r') do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#
2+
# Example of using noise to rotate a grid of arrows
3+
#
4+
load_library :pdf
5+
attr_reader :version, :time_seed, :save_one_frame, :arrow
6+
STEP = 40
7+
POINTS = [
8+
[0, -55], [6, 0], [16, 10], [2, 10], [0, -10], [-2, 10], [-16, 10], [-6, 0]
9+
].freeze
10+
11+
def setup
12+
sketch_title 'Generative Arrow'
13+
# resolution does not really matter since we are working with vectors
14+
frame_rate(35)
15+
@version = 0 # if we grab multiple frames, they will be numbered
16+
@time_seed = 0.1 # seed for the third dimension of the noise
17+
@save_one_frame = false
18+
create_arrow
19+
end
20+
21+
def draw
22+
# turn on recording for this frame if we clicked the mouse
23+
begin_record(PDF, data_path("Line_#{version}.pdf")) if (save_one_frame == true)
24+
background(255)
25+
@time_seed = millis / 5000.0 # change noise over time
26+
grid(width, height, STEP, STEP) do |cx, cy| # iterate down and accross the page
27+
push_matrix
28+
translate(cx + STEP / 2, cy + STEP / 2) # set the position of each arrow
29+
# rotate using noise
30+
rotate(4.0 * PI * noise(cx / 400.0, cy / 400.0, time_seed))
31+
# scale(1)
32+
shape(arrow) # draw our arrow
33+
pop_matrix
34+
end
35+
if (save_one_frame == true)
36+
end_record
37+
@save_one_frame = false
38+
@version += 1
39+
end
40+
end
41+
42+
def mouse_pressed
43+
@save_one_frame = true
44+
end
45+
46+
def create_arrow # custom shape for our arrow
47+
@arrow = create_shape
48+
arrow.begin_shape
49+
arrow.fill(200, 0, 0)
50+
arrow.no_stroke
51+
arrow.stroke_weight(2)
52+
POINTS.each { |pt| arrow.vertex(*pt) }
53+
arrow.end_shape(CLOSE)
54+
end
55+
56+
def settings
57+
size(1000, 1000)
58+
end

external_library/java/au_utilities/ag0094hyc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Andrew Glassner - www.glassner.com & www.imaginary-institute.com
2-
# Translated to JRubyArt by Martin Prout class Ag009Hyc < Propane::App
2+
# Translated to JRubyArt by Martin Prout
33
load_library :AULib
44
include_package 'AULib'
55

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,42 @@
11
load_library :slider
2-
attr_reader :color1, :color2, :color3, :r, :gs, :b, :back
2+
attr_reader :sliders, :r, :gs, :b, :back
33

44
def settings
55
size(600, 400)
66
smooth(4)
77
end
88

99
def setup
10-
sketch_title 'Slider Demo'
10+
sketch_title 'Slider Demo'
1111
@back = true
1212
@r, @gs, @b = 0, 0, 0
13-
@color1 = Slider.slider(
13+
create_gui
14+
end
15+
16+
def draw
17+
background(r + 125, gs, b)
18+
@r = sliders[0].read_value
19+
@gs = sliders[1].read_value
20+
@b = sliders[2].read_value
21+
end
22+
23+
def create_gui
24+
@sliders = (1..3).map do |i|
25+
Slider.slider(
1426
app: self,
1527
vertical: true,
16-
x: 100,
28+
x: 100 + 32 * i,
1729
y: 77,
1830
length: 200,
1931
range: (-125.0..125.0),
20-
name: 'Slider 1',
32+
name: "Slider #{i}",
2133
inital_value: 10
22-
)
23-
@color2 = Slider.slider(
24-
app: self,
25-
vertical: true,
26-
x: 256,
27-
y: 77,
28-
length: 200,
29-
range: (0..255),
30-
name: 'Slider 2',
31-
initial_value: 180
32-
)
33-
@color3 = Slider.slider(
34-
app: self,
35-
vertical: true,
36-
x: 410,
37-
y: 77,
38-
length: 200,
39-
range: (0.0..255.0),
40-
name: 'Slider 3',
41-
initial_value: 134
42-
)
43-
color1.bar_width(100)
44-
color1.widget_colors(color('#930303'), color('#FF0000'))
45-
color2.bar_width(100)
46-
color2.widget_colors(color('#5BCE4D'), color('#1CFF00'))
47-
color3.bar_width(100)
48-
color3.widget_colors(color('#4439C9'), color('#9990FF'))
34+
)
35+
end
36+
sliders[0].bar_width(30)
37+
sliders[0].widget_colors(color('#930303'), color('#FF0000'))
38+
sliders[1].bar_width(30)
39+
sliders[1].widget_colors(color('#5BCE4D'), color('#1CFF00'))
40+
sliders[2].bar_width(30)
41+
sliders[2].widget_colors(color('#4439C9'), color('#9990FF'))
4942
end
50-
51-
def draw
52-
background(r + 125, gs, b)
53-
@r = color1.read_value
54-
@gs = color2.read_value
55-
@b = color3.read_value
56-
end
57-

processing_app/library/vecmath/vec2d/circle_collision.rb

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ def initialize(r = 0.0, velocity = nil, position = Vec2D.new)
1010
@position, @velocity, @r = position, velocity, r
1111
@m = r * 0.1
1212
end
13-
13+
1414
def move
1515
@position += velocity
1616
end
17-
17+
1818
def draw
1919
d = r * 2
2020
ellipse position.x, position.y, d, d
2121
@current = position.copy
2222
end
23-
23+
2424
def erase
2525
d = r * 2
2626
rect current.x, current.y, d, d
@@ -33,10 +33,10 @@ def setup
3333
sketch_title 'Circle Collision'
3434
no_stroke
3535
frame_rate 30
36-
rect_mode RADIUS
36+
rect_mode RADIUS
3737
@balls = []
3838
5.times { balls << Ball.new(10, Vec2D.new(2.15, -1.35), empty_space(15)) }
39-
2.times { balls << Ball.new(40, Vec2D.new(-1.65, 0.42), empty_space(45)) }
39+
2.times { balls << Ball.new(40, Vec2D.new(-1.65, 0.42), empty_space(45)) }
4040
@frame_time = nil
4141
@frame_count = 0
4242
end
@@ -45,15 +45,15 @@ def draw
4545
t = Time.now
4646
fps = 1.0 / (t - @frame_time) if @frame_time
4747
@frame_time = t
48-
@frame_count += 1
48+
@frame_count += 1
4949
# erase previous screen
5050
if @frame_count == 1
5151
background 51
5252
else
5353
fill 51
5454
balls.each { |ball| ball.erase }
55-
end
56-
# move the balls
55+
end
56+
# move the balls
5757
fill 240
5858
balls.each do |ball|
5959
ball.move
@@ -80,79 +80,73 @@ def empty_space?(position, r)
8080
end
8181

8282
def check_object_collisions
83-
83+
8484
(0...(balls.length)).each do |ia|
8585
((ia+1)...(balls.length)).each do |ib|
86-
8786
ba = balls[ia]
8887
bb = balls[ib]
89-
9088
# get distances between the balls components
91-
bVect = bb.position - ba.position
89+
bVect = bb.position - ba.position
9290
# calculate magnitude of the vector separating the balls
9391
bVectMag = bVect.mag
9492
next if bVectMag >= ba.r + bb.r
9593
# get angle of bVect
96-
theta = atan2(bVect.y, bVect.x)
94+
theta = bVect.heading
9795
# precalculate trig values
9896
sine = sin(theta)
9997
cosine = cos(theta)
100-
10198
# bTemp will hold rotated ball positions. You just
10299
# need to worry about bTemp[1] position
103100
bTemp = [Ball.new, Ball.new]
104101
# bb's position is relative to ba's
105-
# so you can use the vector between them (bVect) as the
102+
# so you can use the vector between them (bVect) as the
106103
# reference point in the rotation expressions.
107104
# bTemp[0].x and bTemp[0].y will initialize
108105
# automatically to 0.0, which is what you want
109106
# since bb will rotate around ba
110107
bTemp[1].position.x = cosine * bVect.x + sine * bVect.y
111108
bTemp[1].position.y = cosine * bVect.y - sine * bVect.x
112-
113109
# rotate Temporary velocities
114110
vTemp = [Vec2D.new, Vec2D.new]
115111
vTemp[0].x = cosine * ba.velocity.x + sine * ba.velocity.y
116112
vTemp[0].y = cosine * ba.velocity.y - sine * ba.velocity.x
117113
vTemp[1].x = cosine * bb.velocity.x + sine * bb.velocity.y
118114
vTemp[1].y = cosine * bb.velocity.y - sine * bb.velocity.x
119-
120115
# Now that velocities are rotated, you can use 1D
121-
# conservation of momentum equations to calculate
116+
# conservation of momentum equations to calculate
122117
# the final velocity along the x-axis.
123118
vFinal = [Vec2D.new, Vec2D.new]
124119
# final rotated velocity for ba
125120
vFinal[0].x = ((ba.m - bb.m) * vTemp[0].x + 2 * bb.m * vTemp[1].x) / (ba.m + bb.m)
126121
vFinal[0].y = vTemp[0].y
127122
# final rotated velocity for ba
128123
vFinal[1].x = ((bb.m - ba.m) * vTemp[1].x + 2 * ba.m * vTemp[0].x) / (ba.m + bb.m)
129-
vFinal[1].y = vTemp[1].y
130-
124+
vFinal[1].y = vTemp[1].y
131125
# Rotate ball positions and velocities back
132-
# Reverse signs in trig expressions to rotate
126+
# Reverse signs in trig expressions to rotate
133127
# in the opposite direction
134128
# rotate balls
135129
bFinal = [Ball.new, Ball.new]
136130
bFinal[0].position.x = cosine * bTemp[0].position.x - sine * bTemp[0].position.y
137131
bFinal[0].position.y = cosine * bTemp[0].position.y + sine * bTemp[0].position.x
138132
bFinal[1].position.x = cosine * bTemp[1].position.x - sine * bTemp[1].position.y
139-
bFinal[1].position.y = cosine * bTemp[1].position.y + sine * bTemp[1].position.x
133+
bFinal[1].position.y = cosine * bTemp[1].position.y + sine * bTemp[1].position.x
140134
# update balls to screen position
141-
bb.position = ba.position + bFinal[1].position
142-
ba.position = ba.position + bFinal[0].position
135+
bb.position = ba.position + bFinal[1].position
136+
ba.position = ba.position + bFinal[0].position
143137
# update velocities
144138
ba.velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y
145139
ba.velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x
146140
bb.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y
147141
bb.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x
148142
end
149-
end
143+
end
150144
end
151145

152146
# reverse ball velocity if at sketch boundary
153-
def check_boundary_collision(ball)
154-
ball.velocity.x *= -1 unless (ball.r..width - ball.r).include? ball.position.x
155-
ball.velocity.y *= -1 unless (ball.r..height - ball.r).include? ball.position.y
147+
def check_boundary_collision(ball)
148+
ball.velocity.x *= -1 unless (ball.r..width - ball.r).include? ball.position.x
149+
ball.velocity.y *= -1 unless (ball.r..height - ball.r).include? ball.position.y
156150
end
157151

158152
def settings

processing_app/library/vecmath/vec2d/library/verlet_chain/lib/verlet_ball.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize(pos, push, radius)
88
@pos = pos
99
@push = push
1010
@radius = radius
11-
@pos_old = Vec2D.new(pos.x, pos.y)
11+
@pos_old = pos.dup
1212
# start motion
1313
@pos += push
1414
@x_bound = Boundary.new(radius, width - radius)

0 commit comments

Comments
 (0)