Skip to content

Commit d4b7b3b

Browse files
committed
basics examples
1 parent 46f6111 commit d4b7b3b

File tree

209 files changed

+10048
-26
lines changed

Some content is hidden

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

209 files changed

+10048
-26
lines changed

basics/arrays/array.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
# An array is a list of data. Each piece of data in an array
4+
# is identified by an index number representing its position in
5+
# the array. Arrays are zero based, which means that the first
6+
# element in the array is [0], the second element is [1], and so on.
7+
# In this example, an array named "coswave" is created using ruby
8+
# map with the cosine values. This data is displayed three
9+
# separate ways on the screen.
10+
class SimpleArray < Processing::App
11+
12+
attr_reader :coswave
13+
14+
def setup
15+
sketch_title 'Simple Array'
16+
@coswave = (0..width).map do |i|
17+
Math.cos(map1d(i, (0..width), (0..PI))).abs
18+
end
19+
end
20+
21+
def draw
22+
coswave.each_with_index do |val, i|
23+
stroke(val * 255)
24+
line(i, 0, i, height / 3)
25+
stroke(val * 255 / 4)
26+
line(i, height / 3, i, height / 3 * 2)
27+
stroke(255 - val * 255)
28+
line(i, height / 3 * 2, i, height)
29+
end
30+
end
31+
32+
def settings
33+
size 640, 360
34+
end
35+
end
36+
37+
SimpleArray.new

basics/arrays/array_2d.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
# Demonstrates the use of :grid to create a two-dimensional (2D) array in ruby
4+
# Values in a 2D array are accessed through two index values.
5+
# 2D arrays are useful for storing images. In this example, each dot
6+
# is colored in relation to its distance from the center of the image.
7+
class Array2d < Processing::App
8+
attr_reader :distances
9+
10+
def setup
11+
sketch_title 'Array 2D'
12+
@distances = Array.new(width) { Array.new(height) }
13+
stroke_weight 2
14+
max_distance = dist(width / 2, height / 2, width, height)
15+
grid(width, height) do |x, y|
16+
distance = dist(width / 2, height / 2, x, y)
17+
distances[x][y] = distance / max_distance * 255
18+
end
19+
end
20+
21+
def draw
22+
background 0
23+
x = 0
24+
while x < distances.length
25+
y = 0
26+
while y < distances[x].length
27+
stroke distances[x][y]
28+
point x, y
29+
y += 2
30+
end
31+
x += 2
32+
end
33+
end
34+
35+
def settings
36+
size 640, 360
37+
end
38+
end
39+
40+
Array2d.new

basics/arrays/array_objects.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
4+
class ArrayObjects < Processing::App
5+
# Demonstrates the syntax for creating an array of custom objects.
6+
7+
UNIT = 40
8+
attr_reader :mods
9+
10+
def setup
11+
sketch_title 'Array of Objects'
12+
wide_count = width / UNIT
13+
height_count = height / UNIT
14+
@mods = []
15+
grid(wide_count, height_count) do |i, j|
16+
mods << CustomObject.new(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
17+
end
18+
no_stroke
19+
end
20+
21+
def draw
22+
background 0
23+
mods.each(&:run)
24+
end
25+
26+
def settings
27+
size 640, 360, FX2D
28+
end
29+
30+
module Runnable
31+
def run
32+
update
33+
draw
34+
end
35+
end
36+
37+
# the custom object
38+
class CustomObject
39+
include Processing::Proxy, Runnable
40+
attr_reader :x, :y, :mx, :my, :size
41+
def initialize(mx, my, x, y, speed)
42+
@mx, @my = my, mx # This is backwards to match original example.
43+
@x, @y = x.to_i, y.to_i
44+
@xdir, @ydir = 1, 1
45+
@speed = speed
46+
@size = UNIT
47+
end
48+
49+
def update
50+
@x += @speed * @xdir
51+
unless (0..size).cover? x
52+
@xdir *= -1
53+
@x += @xdir
54+
@y += @ydir
55+
end
56+
return unless @y >= @size || x <= 0
57+
@ydir *= -1
58+
@y += @ydir
59+
end
60+
61+
def draw
62+
fill(255)
63+
ellipse(mx + x, my + y, 6, 6)
64+
end
65+
end
66+
end
67+
68+
ArrayObjects.new

basics/arrays/custom_array.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
require 'forwardable'
4+
5+
# Demonstrates possible syntax for creating a custom array of objects.
6+
class CustomArray < Processing::App
7+
UNIT = 40
8+
attr_reader :custom_array
9+
10+
def setup
11+
sketch_title 'Custom Array'
12+
wide_count = width / UNIT
13+
height_count = height / UNIT
14+
@custom_array = CustomArray.new
15+
grid(wide_count, height_count) do |i, j|
16+
custom_array.add_object(i * UNIT, j * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
17+
end
18+
no_stroke
19+
end
20+
21+
def draw
22+
custom_array.update
23+
custom_array.draw
24+
end
25+
26+
def settings
27+
size 640, 360, FX2D
28+
end
29+
30+
# The Particle object
31+
Particle = Struct.new(:x, :y, :mx, :my, :size, :speed, :xdir, :ydir)
32+
33+
# The custom Array created using Forwardable
34+
# Processing::Proxy gives access to PApplet methods
35+
class CustomArray
36+
extend Forwardable
37+
def_delegators(:@objs, :each, :<<)
38+
include Enumerable, Processing::Proxy
39+
40+
def initialize
41+
@objs = []
42+
end
43+
44+
def add_object(mx, my, x, y, speed)
45+
self << Particle.new(x.to_i, y.to_i, mx, my, UNIT, speed, 1, 1)
46+
end
47+
48+
def update
49+
each do |obj|
50+
update_x obj
51+
next unless obj.y >= UNIT || obj.x <= 0
52+
obj.ydir *= -1
53+
obj.y += obj.ydir
54+
end
55+
end
56+
57+
def update_x(obj)
58+
obj.x += obj.speed * obj.xdir
59+
return if (0..UNIT).cover? obj.x
60+
obj.xdir *= -1
61+
obj.x += obj.xdir
62+
obj.y += obj.ydir
63+
end
64+
65+
def draw
66+
background(0)
67+
fill(255)
68+
each do |obj|
69+
ellipse(obj.mx + obj.x, obj.my + obj.y, 6, 6)
70+
end
71+
end
72+
end
73+
74+
end
75+
76+
CustomArray.new

basics/arrays/parallel_array.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
require 'parallel'
4+
# An array is a list of data. Each piece of data in an array
5+
# is identified by an index number representing its position in
6+
# the array. Arrays are zero based, which means that the first
7+
# element in the array is [0], the second element is [1], and so on.
8+
# In this example, an array named "coswave" is created using ruby
9+
# map with the cosine values. This data is displayed three
10+
# separate ways on the screen.
11+
class ParallelArray < Processing::App
12+
13+
attr_reader :coswave
14+
15+
def setup
16+
sketch_title 'Array'
17+
@coswave = Parallel.map(0..width) do |i|
18+
Math.cos(map1d(i, (0..width), (0..PI))).abs
19+
end
20+
end
21+
22+
def draw
23+
coswave.each_with_index do |val, i|
24+
stroke(val * 255)
25+
line(i, 0, i, height / 3)
26+
stroke(val * 255 / 4)
27+
line(i, height / 3, i, height / 3 * 2)
28+
stroke(255 - val * 255)
29+
line(i, height / 3 * 2, i, height)
30+
end
31+
end
32+
33+
def settings
34+
size 640, 360
35+
end
36+
end
37+
38+
ParallelArray.new

basics/camera/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, 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

basics/camera/arcball_perspektiv.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'picrate'
3+
require 'arcball'
4+
# Move the mouse left and right to change the field of view (fov).
5+
# Click to modify the aspect ratio. The perspektiv method
6+
# sets a perspective projection applying foreshortening, making
7+
# distant objects appear smaller than closer ones. The parameters
8+
# define a viewing volume with the shape of truncated pyramid.
9+
# while farther objects appear smaller. This projection simulates
10+
# the perspective of the world more accurately than orthographic projection.
11+
# The version of perspektiv without parameters sets the default
12+
# perspective and the version with up to four parameters allows the programmer
13+
# to set the environment more precisely.
14+
class ArcballPerspektiv < Processing::App
15+
attr_reader :aspect
16+
17+
def setup
18+
sketch_title 'Perspektiv Example'
19+
Processing::ArcBall.constrain self
20+
@aspect = width.to_f / height
21+
no_stroke
22+
end
23+
24+
def draw
25+
lights
26+
background 204
27+
camera_y = height / 2.0
28+
fov = mouse_x / width.to_f * PI / 2.0
29+
camera_z = camera_y / Math.tan(fov / 2.0)
30+
perspektiv(
31+
fov: fov,
32+
aspect_ratio: aspect,
33+
near_z: camera_z / 10.0,
34+
far_z: camera_z * 10.0
35+
)
36+
rotate_x -PI / 6
37+
box 45
38+
translate 0, 0, -50
39+
box 30
40+
end
41+
42+
def settings
43+
size 640, 360, P3D
44+
end
45+
46+
def key_pressed
47+
case key
48+
when ' '
49+
@aspect = width * 0.5 / height
50+
when 'n', 'N'
51+
@aspect = width.to_f / height
52+
end
53+
end
54+
end
55+
56+
ArcballPerspektiv.new

basics/camera/kmove_eye.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'picrate'
3+
# Move Eye.
4+
# by Simon Greenwold.
5+
#
6+
# The camera lifts up (controlled by mouseY) while looking at the same point.
7+
class KMoveEye < Processing::App
8+
def setup
9+
sketch_title 'KMove Eye Kamera'
10+
fill 204
11+
end
12+
13+
def draw
14+
lights
15+
background 0
16+
kamera eye: Vec3D.new(30, mouse_y, 220), center: Vec3D.new
17+
no_stroke
18+
box 90
19+
stroke 255
20+
line( -100, 0, 0, 100, 0, 0)
21+
line( 0, -100, 0, 0, 100, 0)
22+
line( 0, 0, -100, 0, 0, 100)
23+
end
24+
25+
def settings
26+
size 640, 360, P3D
27+
end
28+
end
29+
30+
KMoveEye.new

0 commit comments

Comments
 (0)