Skip to content

Commit 4bbe7a0

Browse files
committed
basic examples
1 parent d9af127 commit 4bbe7a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2971
-0
lines changed

examples/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Contributing picrate examples
2+
3+
Share your enthusiasm for coding by submitting an example for inclusion here, provisional versions are acceptable if they demonstrate something interesting (and work), but we would prefer that you followed the style guide. But naturally we expect that you to aspire to the principal of code as art eventually (not many original examples survive without amendment).
4+
5+
### Style guide
6+
7+
* Prefer `Vec2D` and `Vec3D` to `PVector` (it is confusing and sometimes plain wrong).
8+
* Prefer `Struct` over `Vec2D` and `Vec3D` if you don't need their methods.
9+
* No trailing whitespace.
10+
* Use spaces not tabs.
11+
* Avoid explicit return statements.
12+
* Avoid using semicolons.
13+
* Don't use `self` explicitly anywhere except class methods (`def self.method`)
14+
and assignments (`self.attribute =`).
15+
* Prefer `&:method_name` to `{ |item| item.method_name }` for simple method
16+
calls.
17+
* Use `CamelCase` for classes and modules, `snake_case` for variables and
18+
methods, `SCREAMING_SNAKE_CASE` for constants.
19+
* Use `def self.method`, not `def Class.method` or `class << self`.
20+
* Use `def` with parentheses when there are arguments.
21+
* Don't use spaces after required keyword arguments.
22+
* Use `each`, not `for`, for iteration.
23+
24+
When translating a sketch from vanilla processing (or some other codebase), you should credit the original author, unless the rubified version is unrecognizable from the original. It is often worth running rubocop on sketch code to avoid the most egregious errors.

examples/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 jruby 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 --dev #{sample_name}", 'r') do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end

examples/animator.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
4+
class Animator < Processing::App
5+
FRAME_COUNT = 12
6+
7+
def settings
8+
size 350, 350
9+
# pixel_density(2) # for HiDpi screens
10+
# smooth see https://processing.org/reference/smooth_.html
11+
end
12+
13+
def setup
14+
sketch_title 'Animator'
15+
@frames = []
16+
@last_time = 0
17+
@current_frame = 0
18+
@draw = false
19+
@back_color = 204
20+
stroke_weight 4
21+
background @back_color
22+
FRAME_COUNT.times { @frames << get }
23+
end
24+
25+
def draw
26+
time = millis
27+
if time > @last_time + 100
28+
next_frame
29+
@last_time = time
30+
end
31+
line(pmouse_x, pmouse_y, mouse_x, mouse_y) if @draw
32+
end
33+
34+
def mouse_pressed
35+
@draw = true
36+
end
37+
38+
def mouse_released
39+
@draw = false
40+
end
41+
42+
def key_pressed
43+
background @back_color
44+
@frames.size.times { |i| @frames[i] = get }
45+
end
46+
47+
def next_frame
48+
@frames[@current_frame] = get
49+
@current_frame += 1
50+
@current_frame = 0 if @current_frame >= @frames.size
51+
image(@frames[@current_frame], 0, 0)
52+
end
53+
end
54+
55+
Animator.new

examples/bezier_playground.rb

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/usr/bin/env jruby
2+
# frozen_string_literal: true
3+
require 'picrate'
4+
5+
X1, Y1, X2, Y2 = 50.0, 50.0, 250.0, 250.0
6+
REDDISH = [250, 100, 100].freeze
7+
RADIUS = 7
8+
9+
# A Bezier playground. Click to shape the curve. Drag to move it.
10+
# Arrows toggle between curves, delete removes them.
11+
# You can print out the parametric equations for t = 0..1
12+
class BezierPlayground < Processing::App
13+
load_libraries :control_panel, :curve
14+
include Olap
15+
attr_reader :curves, :c1x, :c1y, :c2x, :c2y
16+
17+
def settings
18+
size 300, 300
19+
# pixel_density(2) # for HiDpi screens
20+
# smooth # see https://processing.org/reference/smooth_.html
21+
end
22+
23+
def setup
24+
sketch_title 'Bezier Playground'
25+
@curves = []
26+
@hide = false
27+
control_panel do |c|
28+
c.look_feel 'Nimbus'
29+
c.button :new_curve
30+
c.button :print_equations
31+
end
32+
generate_curve
33+
end
34+
35+
def draw
36+
background 50
37+
draw_control_tangent_lines
38+
draw_curves
39+
draw_current_control_points
40+
end
41+
42+
def print_equations
43+
curves.each_with_index { |c, i| c.print_equation(i + 1) }
44+
end
45+
46+
def control_points
47+
return c1x, c1y, c2x, c2y
48+
end
49+
50+
def set_control_points(*points)
51+
@c1x, @c1y, @c2x, @c2y = points.any? ? points : [X1, Y1, X2, Y2]
52+
end
53+
54+
def generate_curve
55+
curves << current_curve = Curve.new
56+
@current = curves.length - 1
57+
set_control_points(*current_curve.control_points)
58+
end
59+
60+
def current_curve
61+
curves[@current]
62+
end
63+
64+
def new_curve
65+
current_curve.set_control_points(c1x, c1y, c2x, c2y)
66+
generate_curve
67+
end
68+
69+
def clicked_control_point?
70+
x, y = mouse_x, mouse_y
71+
return :one if Olap.overlaps(c1x, c1y, x, y)
72+
return :two if Olap.overlaps(c2x, c2y, x, y)
73+
end
74+
75+
def mouse_pressed
76+
switch_curve_if_endpoint_clicked
77+
@control = clicked_control_point?
78+
return if @control
79+
curve = curves.detect { |c| c.contains(mouse_x, mouse_y) }
80+
@end_point = curve.contains(mouse_x, mouse_y) if curve
81+
end
82+
83+
def mouse_released
84+
@control, @end_point = nil, nil
85+
@hide = false
86+
end
87+
88+
def mouse_dragged
89+
offs = compute_offsets
90+
return if offs.map(&:abs).max > 100
91+
return move_control_point(*offs) if @control
92+
return move_end_point(*offs) && move_control_point(*offs) if @end_point
93+
move_current_curve(*offs)
94+
end
95+
96+
def switch_curve_if_endpoint_clicked
97+
become = curves.detect { |c| c.contains(mouse_x, mouse_y) }
98+
return unless become && become != current_curve
99+
current_curve.set_control_points(*control_points)
100+
set_control_points(*become.control_points)
101+
@current = curves.index(become)
102+
end
103+
104+
def move_current_curve(x_off, y_off)
105+
@c1x += x_off; @c2x += x_off
106+
@c1y += y_off; @c2y += y_off
107+
current_curve.set_control_points(*control_points)
108+
current_curve.x1 += x_off; current_curve.x2 += x_off
109+
current_curve.y1 += y_off; current_curve.y2 += y_off
110+
end
111+
112+
def move_control_point(x_off, y_off)
113+
case @control || @end_point
114+
when :one then @c1x += x_off and @c1y += y_off
115+
when :two then @c2x += x_off and @c2y += y_off
116+
end
117+
current_curve.set_control_points(*control_points)
118+
end
119+
120+
def move_end_point(x_off, y_off)
121+
c = current_curve
122+
case @end_point
123+
when :one then c.x1 += x_off and c.y1 += y_off
124+
when :two then c.x2 += x_off and c.y2 += y_off
125+
end
126+
end
127+
128+
def compute_offsets
129+
return mouse_x - pmouse_x, mouse_y - pmouse_y
130+
end
131+
132+
def draw_curves
133+
stroke 255
134+
no_fill
135+
stroke_width 2
136+
curves.each(&:draw)
137+
end
138+
139+
def draw_current_control_points
140+
fill color(*REDDISH)
141+
no_stroke
142+
oval c1x, c1y, 5, 5
143+
oval c2x, c2y, 5, 5
144+
end
145+
146+
def draw_control_tangent_lines
147+
c = current_curve
148+
stroke color(*REDDISH)
149+
stroke_width 1
150+
line c1x, c1y, c.x1, c.y1
151+
line c2x, c2y, c.x2, c.y2
152+
end
153+
end
154+
155+
BezierPlayground.new

examples/chladni.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env jruby
2+
# frozen_string_literal: true
3+
require 'picrate'
4+
5+
# Chladni plate interference surfaces
6+
# —
7+
# Based on :
8+
# http://paulbourke.net/geometry/chladni/
9+
#
10+
# —
11+
# Developed on:
12+
# - Processing 2.1.1 on MacOSX (10.9.2)
13+
# Julien Gachadoat aka v3ga
14+
# www.v3ga.net
15+
# www.2roqs.com
16+
#
17+
# Translated and tested on:
18+
# - picrate
19+
# Martin Prout
20+
21+
class Chladni < Processing::App
22+
23+
attr_reader :m, :n, :epsilon, :recompute
24+
25+
def settings
26+
size(500, 500, P2D)
27+
end
28+
29+
def setup
30+
sketch_title 'Chladni'
31+
@epsilon = 0.05
32+
@recompute = true
33+
@m = 10
34+
@n = 2
35+
end
36+
37+
def draw
38+
return unless recompute
39+
@recompute = false
40+
background(255)
41+
load_pixels
42+
grid(width, height) do |x, y|
43+
chladni = cos(n * PI * x / width) * cos(m * PI * y / width) - cos(m * PI * x / width) * cos(n * PI * y / width)
44+
pixels[x + y * width] = 0 if chladni.abs <= epsilon
45+
end
46+
update_pixels
47+
format_string = "m= %d n = %d \nepsilon= %0.3f"
48+
infos = format(format_string, m, n, epsilon)
49+
fill(255, 0, 0)
50+
text(infos, 4, 16)
51+
end
52+
53+
def key_pressed
54+
case key
55+
when '+'
56+
@epsilon += 0.01
57+
when '-'
58+
@epsilon -= 0.01
59+
when CODED
60+
case key_code
61+
when UP
62+
@m += 1
63+
when DOWN
64+
@m -= 1
65+
when LEFT
66+
@n -= 1
67+
when RIGHT
68+
@n += 1
69+
end
70+
end
71+
@recompute = true
72+
@m = constrain(m, 1, 20)
73+
@n = constrain(n, 1, 20)
74+
end
75+
end
76+
77+
Chladni.new

0 commit comments

Comments
 (0)