Skip to content

Commit

Permalink
basic examples
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed May 5, 2018
1 parent d9af127 commit 4bbe7a0
Show file tree
Hide file tree
Showing 44 changed files with 2,971 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Contributing picrate examples

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).

### Style guide

* Prefer `Vec2D` and `Vec3D` to `PVector` (it is confusing and sometimes plain wrong).
* Prefer `Struct` over `Vec2D` and `Vec3D` if you don't need their methods.
* No trailing whitespace.
* Use spaces not tabs.
* Avoid explicit return statements.
* Avoid using semicolons.
* Don't use `self` explicitly anywhere except class methods (`def self.method`)
and assignments (`self.attribute =`).
* Prefer `&:method_name` to `{ |item| item.method_name }` for simple method
calls.
* Use `CamelCase` for classes and modules, `snake_case` for variables and
methods, `SCREAMING_SNAKE_CASE` for constants.
* Use `def self.method`, not `def Class.method` or `class << self`.
* Use `def` with parentheses when there are arguments.
* Don't use spaces after required keyword arguments.
* Use `each`, not `for`, for iteration.

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.
30 changes: 30 additions & 0 deletions examples/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Simple demo Rakefile to autorun samples in current directory
# adjust path to jruby executable, and or opts as required

SAMPLES_DIR = './'

desc 'run demo'
task default: [:demo]

desc 'demo'
task :demo do
samples_list.shuffle.each { |sample| run_sample sample }
end

def samples_list
files = []
Dir.chdir(SAMPLES_DIR)
Dir.glob('*.rb').each do |file|
files << File.join(SAMPLES_DIR, file)
end
return files
end

def run_sample(sample_name)
puts "Running #{sample_name}...quit to run next sample"
open("|jruby --dev #{sample_name}", 'r') do |io|
while l = io.gets
puts(l.chop)
end
end
end
55 changes: 55 additions & 0 deletions examples/animator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env jruby
require 'picrate'

class Animator < Processing::App
FRAME_COUNT = 12

def settings
size 350, 350
# pixel_density(2) # for HiDpi screens
# smooth see https://processing.org/reference/smooth_.html
end

def setup
sketch_title 'Animator'
@frames = []
@last_time = 0
@current_frame = 0
@draw = false
@back_color = 204
stroke_weight 4
background @back_color
FRAME_COUNT.times { @frames << get }
end

def draw
time = millis
if time > @last_time + 100
next_frame
@last_time = time
end
line(pmouse_x, pmouse_y, mouse_x, mouse_y) if @draw
end

def mouse_pressed
@draw = true
end

def mouse_released
@draw = false
end

def key_pressed
background @back_color
@frames.size.times { |i| @frames[i] = get }
end

def next_frame
@frames[@current_frame] = get
@current_frame += 1
@current_frame = 0 if @current_frame >= @frames.size
image(@frames[@current_frame], 0, 0)
end
end

Animator.new
155 changes: 155 additions & 0 deletions examples/bezier_playground.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env jruby
# frozen_string_literal: true
require 'picrate'

X1, Y1, X2, Y2 = 50.0, 50.0, 250.0, 250.0
REDDISH = [250, 100, 100].freeze
RADIUS = 7

# A Bezier playground. Click to shape the curve. Drag to move it.
# Arrows toggle between curves, delete removes them.
# You can print out the parametric equations for t = 0..1
class BezierPlayground < Processing::App
load_libraries :control_panel, :curve
include Olap
attr_reader :curves, :c1x, :c1y, :c2x, :c2y

def settings
size 300, 300
# pixel_density(2) # for HiDpi screens
# smooth # see https://processing.org/reference/smooth_.html
end

def setup
sketch_title 'Bezier Playground'
@curves = []
@hide = false
control_panel do |c|
c.look_feel 'Nimbus'
c.button :new_curve
c.button :print_equations
end
generate_curve
end

def draw
background 50
draw_control_tangent_lines
draw_curves
draw_current_control_points
end

def print_equations
curves.each_with_index { |c, i| c.print_equation(i + 1) }
end

def control_points
return c1x, c1y, c2x, c2y
end

def set_control_points(*points)
@c1x, @c1y, @c2x, @c2y = points.any? ? points : [X1, Y1, X2, Y2]
end

def generate_curve
curves << current_curve = Curve.new
@current = curves.length - 1
set_control_points(*current_curve.control_points)
end

def current_curve
curves[@current]
end

def new_curve
current_curve.set_control_points(c1x, c1y, c2x, c2y)
generate_curve
end

def clicked_control_point?
x, y = mouse_x, mouse_y
return :one if Olap.overlaps(c1x, c1y, x, y)
return :two if Olap.overlaps(c2x, c2y, x, y)
end

def mouse_pressed
switch_curve_if_endpoint_clicked
@control = clicked_control_point?
return if @control
curve = curves.detect { |c| c.contains(mouse_x, mouse_y) }
@end_point = curve.contains(mouse_x, mouse_y) if curve
end

def mouse_released
@control, @end_point = nil, nil
@hide = false
end

def mouse_dragged
offs = compute_offsets
return if offs.map(&:abs).max > 100
return move_control_point(*offs) if @control
return move_end_point(*offs) && move_control_point(*offs) if @end_point
move_current_curve(*offs)
end

def switch_curve_if_endpoint_clicked
become = curves.detect { |c| c.contains(mouse_x, mouse_y) }
return unless become && become != current_curve
current_curve.set_control_points(*control_points)
set_control_points(*become.control_points)
@current = curves.index(become)
end

def move_current_curve(x_off, y_off)
@c1x += x_off; @c2x += x_off
@c1y += y_off; @c2y += y_off
current_curve.set_control_points(*control_points)
current_curve.x1 += x_off; current_curve.x2 += x_off
current_curve.y1 += y_off; current_curve.y2 += y_off
end

def move_control_point(x_off, y_off)
case @control || @end_point
when :one then @c1x += x_off and @c1y += y_off
when :two then @c2x += x_off and @c2y += y_off
end
current_curve.set_control_points(*control_points)
end

def move_end_point(x_off, y_off)
c = current_curve
case @end_point
when :one then c.x1 += x_off and c.y1 += y_off
when :two then c.x2 += x_off and c.y2 += y_off
end
end

def compute_offsets
return mouse_x - pmouse_x, mouse_y - pmouse_y
end

def draw_curves
stroke 255
no_fill
stroke_width 2
curves.each(&:draw)
end

def draw_current_control_points
fill color(*REDDISH)
no_stroke
oval c1x, c1y, 5, 5
oval c2x, c2y, 5, 5
end

def draw_control_tangent_lines
c = current_curve
stroke color(*REDDISH)
stroke_width 1
line c1x, c1y, c.x1, c.y1
line c2x, c2y, c.x2, c.y2
end
end

BezierPlayground.new
77 changes: 77 additions & 0 deletions examples/chladni.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env jruby
# frozen_string_literal: true
require 'picrate'

# Chladni plate interference surfaces
# —
# Based on :
# http://paulbourke.net/geometry/chladni/
#
# —
# Developed on:
# - Processing 2.1.1 on MacOSX (10.9.2)
# Julien Gachadoat aka v3ga
# www.v3ga.net
# www.2roqs.com
#
# Translated and tested on:
# - picrate
# Martin Prout

class Chladni < Processing::App

attr_reader :m, :n, :epsilon, :recompute

def settings
size(500, 500, P2D)
end

def setup
sketch_title 'Chladni'
@epsilon = 0.05
@recompute = true
@m = 10
@n = 2
end

def draw
return unless recompute
@recompute = false
background(255)
load_pixels
grid(width, height) do |x, y|
chladni = cos(n * PI * x / width) * cos(m * PI * y / width) - cos(m * PI * x / width) * cos(n * PI * y / width)
pixels[x + y * width] = 0 if chladni.abs <= epsilon
end
update_pixels
format_string = "m= %d n = %d \nepsilon= %0.3f"
infos = format(format_string, m, n, epsilon)
fill(255, 0, 0)
text(infos, 4, 16)
end

def key_pressed
case key
when '+'
@epsilon += 0.01
when '-'
@epsilon -= 0.01
when CODED
case key_code
when UP
@m += 1
when DOWN
@m -= 1
when LEFT
@n -= 1
when RIGHT
@n += 1
end
end
@recompute = true
@m = constrain(m, 1, 20)
@n = constrain(n, 1, 20)
end
end

Chladni.new
Loading

0 comments on commit 4bbe7a0

Please sign in to comment.