Skip to content

Commit 97799c7

Browse files
committed
Add voronoi examples
1 parent e4b8266 commit 97799c7

File tree

9 files changed

+147
-6
lines changed

9 files changed

+147
-6
lines changed

contributed/circle_collision.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def draw
2424
end
2525

2626
def settings
27-
size 640, 360, FX2D
27+
size 640, 360
2828
end
2929
end
3030

@@ -60,7 +60,7 @@ def check_collision(other_ball)
6060
# calculate magnitude of the vector separating the balls
6161
return unless difference.mag < (r + other_ball.r)
6262
# get angle of difference
63-
theta = difference.heading
63+
theta = difference.fast_heading
6464
# precalculate trig values
6565
sine = sin(theta)
6666
cosine = cos(theta)

external_library/gem/geomerative/hello_svg_to_pdf.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
require 'geomerative'
44

55
class HelloSvgToPDF < Propane::App
6-
load_library 'pdf'
6+
load_library :pdf
7+
include_package 'processing.pdf'
78
attr_reader :grp, :pdf
89

910
def settings
@@ -14,7 +15,7 @@ def setup
1415
sketch_title 'SVG to PDF sketch'
1516
RG.init(self)
1617
@grp = RG.load_shape(data_path('bot1.svg'))
17-
@pdf = create_graphics(width, height, PDF, 'bot1.pdf')
18+
@pdf = create_graphics(width, height, PDF, data_path('bot1.pdf'))
1819
end
1920

2021
def draw
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 rp5 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("|jruby #{sample_name}", 'r') do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env jruby -w
2+
require 'propane'
3+
require 'toxiclibs'
4+
# Sketch class
5+
class MonalisaVoronoi < Propane::App
6+
attr_reader :gfx, :voronoi, :img, :pixels
7+
def setup
8+
sketch_title 'Monalisa Voronoi'
9+
@img = load_image(data_path('monalisa.jpg'))
10+
img.load_pixels
11+
@pixels = img.pixels
12+
@gfx = Gfx::ToxiclibsSupport.new(self)
13+
@voronoi = Toxi::Voronoi.new
14+
3_000.times do
15+
voronoi.add_point(TVec2D.new(rand(width), rand(height)))
16+
end
17+
no_loop
18+
end
19+
20+
def draw
21+
voronoi.get_regions.each do |polygon|
22+
voronoi.get_sites.each do |v|
23+
if polygon.contains_point(v)
24+
fill pixels[v.y * img.width + v.x]
25+
gfx.polygon2D(polygon)
26+
end
27+
end
28+
end
29+
save_frame(data_path('mona-lisa-voronoi.png'))
30+
end
31+
32+
def settings
33+
size(600, 894)
34+
end
35+
end
36+
37+
MonalisaVoronoi.new
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env jruby -w
2+
require 'propane'
3+
require 'toxiclibs'
4+
# Sketch class
5+
class Voronoi1 < Propane::App
6+
attr_reader :gfx, :voronoi
7+
8+
def setup
9+
sketch_title 'Basic Voronoi'
10+
@gfx = Gfx::ToxiclibsSupport.new(self)
11+
@voronoi = Toxi::Voronoi.new
12+
50.times do
13+
voronoi.add_point(TVec2D.new(rand(width), rand(height)))
14+
end
15+
no_loop
16+
end
17+
18+
def draw
19+
background 0
20+
fill 0
21+
smooth
22+
stroke_weight 1
23+
stroke 255
24+
voronoi.get_regions.each { |polygon| gfx.polygon2D(polygon) }
25+
save_frame(data_path('voronoi-001.png'))
26+
end
27+
28+
def settings
29+
size 450, 360
30+
end
31+
end
32+
33+
Voronoi1.new
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env jruby -w
2+
require 'propane'
3+
require 'toxiclibs'
4+
# Sketch class
5+
class Voronoi2 < Propane::App
6+
attr_reader :colors, :gfx, :voronoi
7+
8+
def setup
9+
sketch_title 'Colored Voronoi'
10+
@gfx = Gfx::ToxiclibsSupport.new(self)
11+
complement = Toxi::ComplementaryStrategy.new.create_list_from_color(TColor::BLUE)
12+
@colors = complement.to_a # a ruby Array of TColors
13+
@voronoi = Toxi::Voronoi.new
14+
50.times do
15+
voronoi.add_point(TVec2D.new(rand(width), rand(height)))
16+
end
17+
end
18+
19+
def draw
20+
background 0
21+
stroke_weight 1
22+
stroke 255
23+
voronoi.get_regions.each do |polygon|
24+
fill colors.sample.toARGB
25+
gfx.polygon2D(polygon)
26+
end
27+
save_frame(data_path('voronoi-002.png'))
28+
no_loop
29+
end
30+
31+
def settings
32+
size 450, 360
33+
end
34+
35+
def mouse_pressed
36+
loop
37+
end
38+
end
39+
40+
Voronoi2.new

processing_app/library/vecmath/vec2d/library/circles/lib/triangle_point.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def direction(acc)
1818
@accel = acc
1919
# magnitude of the acceleration is proportional to the angle between
2020
# acceleration and velocity
21-
dif = acc.angle_between(vel)
21+
dif = acc.fast_angle_between(vel)
2222
dif = map1d(dif, 0..PI, 0.1..0.001)
2323
@accel = acc * dif
2424
end

processing_app/library/vecmath/vec2d/library/flock/flock.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def seek(target)
8888

8989
def render
9090
# Draw a triangle rotated in the direction of velocity
91-
theta = velocity.heading + Math::PI/2
91+
theta = velocity.fast_heading + Math::PI/2
9292
fill(200, 100)
9393
stroke(255)
9494
push_matrix

0 commit comments

Comments
 (0)