Skip to content

Commit 44d93fe

Browse files
committed
samples for Visor
1 parent 0f85a2f commit 44d93fe

File tree

6 files changed

+433
-0
lines changed

6 files changed

+433
-0
lines changed

samples/25_squares.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ===== Default : Default
2+
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
3+
require 'vecmath'
4+
PALETTE = %w[#a11220 #884444].freeze
5+
@colors = ColorUtil.webArray(PALETTE.to_java(:string))
6+
rect_mode(CORNER)
7+
no_stroke
8+
frame_rate(1) # set the frame rate to 1 draw call per second
9+
10+
11+
def draw
12+
background(180) # clear the screen to grey
13+
grid_size = 5 # rand(3..12) # select a rand number of squares each frame
14+
gap = 5 # rand(5..50) # select a rand gap between each square
15+
# calculate the size of each square for the given number of squares and gap between them
16+
cellsize = (width - (grid_size + 1) * gap) / grid_size
17+
position = -> (count) { gap * (count + 1) + cellsize * count + rand(-5..5) }
18+
MathTool::grid(grid_size, grid_size) do |x, y|
19+
rand(0..5) > 4 ? fill(color(@colors[0]), 180.0) : fill(color(@colors[1]), 180.0)
20+
rect(position.call(x), position.call(y), cellsize, cellsize)
21+
end
22+
end

samples/arc_tesselation.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# ===== Default : Default
2+
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
3+
require 'vecmath'
4+
5+
PALETTE = %w[#0F4155 #158CA7 #D63826 #F5C03E #152A3B #7EC873 #4B3331].freeze
6+
7+
8+
# create a java primitive array of signed int
9+
# @cols = web_array(PALETTE)
10+
@group = ColorUtil.web_array(PALETTE.to_java(:string))
11+
stroke_weight 1.5
12+
stroke_cap SQUARE
13+
stroke(0, 200)
14+
@coloured = true
15+
16+
17+
def draw
18+
arc_pattern
19+
end
20+
21+
def sep_index(idx, length)
22+
(idx - (length - 1) * 0.5).abs.floor
23+
end
24+
25+
def sep_color(idx, number)
26+
@group[sep_index(idx - 1, number + 1)]
27+
end
28+
29+
def arc_pattern
30+
circ_number = rand(4..10)
31+
block_size = rand(30..70)
32+
back_color = @coloured ? @group.last : 255
33+
fill(back_color)
34+
rect(0, 0, width, height)
35+
half_block = block_size / 2
36+
two_block = 2 * block_size
37+
MathTool::grid(width + two_block, height + two_block, block_size, block_size) do |x, y|
38+
push_matrix
39+
translate x, y
40+
rotate HALF_PI * rand(0..4)
41+
circ_number.downto(0) do |i|
42+
diam = two_block * i / (circ_number + 1)
43+
ccolor = i < 2 || !coloured ? back_color : sep_color(i, circ_number)
44+
fill ccolor
45+
arc(-half_block, -half_block, diam, diam, 0, HALF_PI)
46+
arc(half_block, half_block, diam, diam, PI, PI + HALF_PI)
47+
end
48+
pop_matrix
49+
end
50+
end
51+
52+
# def mouse_pressed
53+
# @group.shuffle if @coloured
54+
# loop
55+
# end
56+
57+
# def key_typed
58+
# case key
59+
# when 'c', 'C'
60+
# @coloured = !@coloured
61+
# end
62+
# end

samples/arcball_cube.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
2+
require 'arcball'
3+
4+
visor_class :Arc do
5+
def initialize
6+
ArcBall.init(sketch)
7+
end
8+
end
9+
10+
Arc.new
11+
12+
def draw
13+
background 255
14+
15+
lights
16+
17+
# translate width / 2, height / 2, 0
18+
19+
# rotate_x frameCount * 0.01
20+
# rotate_y frameCount * 0.0125
21+
# rotate_z frameCount * 0.015
22+
23+
stroke 0
24+
fill 255, 0, 0
25+
26+
box 100
27+
end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# ===== Default : Default
2+
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
3+
require 'vecmath'
4+
5+
rectMode CENTER
6+
7+
visor_class :Box do
8+
SIZE = 80
9+
10+
def initialize
11+
@pos = Vec2D.new(rand(width), rand(height))
12+
@vel = Vec2D.random * 2
13+
end
14+
15+
def update
16+
@pos += @vel
17+
18+
@pos.x = width + SIZE if @pos.x < -SIZE
19+
@pos.y = height + SIZE if @pos.y < -SIZE
20+
@pos.x = -SIZE if @pos.x > width + SIZE
21+
@pos.y = -SIZE if @pos.y > height + SIZE
22+
end
23+
24+
def draw
25+
with_translate @pos.x, @pos.y do
26+
noStroke
27+
fill 255, 150, 0
28+
rect 0, 0, SIZE, SIZE
29+
end
30+
end
31+
end
32+
33+
@boxes = []
34+
40.times do
35+
@boxes << Box.new
36+
end
37+
38+
def draw
39+
background 0
40+
41+
@boxes.each do |box|
42+
box.update
43+
box.draw
44+
end
45+
end
46+
# ===== circles : circles
47+
visor_class :Circle do
48+
RADIUS = 50
49+
50+
def initialize
51+
@pos = Vec2D.new(rand(width), rand(height))
52+
end
53+
54+
def update(a)
55+
@pos.y += speed * a
56+
@pos.y = -RADIUS if @pos.y > height + RADIUS
57+
end
58+
59+
def draw(a)
60+
with_translate @pos.x, @pos.y do
61+
scale a
62+
noStroke
63+
fill 255
64+
ellipse 0, 0, RADIUS, RADIUS
65+
end
66+
end
67+
end
68+
69+
@circles = []
70+
150.times do
71+
@circles << Circle.new
72+
end
73+
74+
@speed ||= 10
75+
set_range :@speed, 0, 30
76+
77+
def draw
78+
rectMode CORNER
79+
noStroke
80+
fill 0, 50
81+
rect 0, 0, width, height
82+
83+
@circles.each_with_index do |circle, i|
84+
a = i / @circles.size.to_f
85+
86+
circle.update a + 0.1
87+
circle.draw a + 0.1
88+
end
89+
end
90+
91+
set_blend_mode MULTIPLY

samples/mycelium.rb

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# ===== Default : Default
2+
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
3+
require 'vecmath'
4+
require 'forwardable'
5+
6+
# Here we use the JRubyArt Vec2D class, and AppRender class (for vec.to_vertex)
7+
# Further we use the power of ruby (metaprogramming) to make Branch enumerable
8+
# and use Forwardable to define which enumerable methods we want to use.
9+
visor_class :Branch do
10+
include Enumerable
11+
extend Forwardable
12+
def_delegators(:@children, :<<, :each, :length)
13+
# variance angle for growth direction per time step
14+
THETA = Math::PI / 6
15+
# max segments per branch
16+
MAX_LEN = 100
17+
# max recursion limit
18+
MAX_GEN = 3
19+
# branch chance per time step
20+
BRANCH_CHANCE = 0.05
21+
# branch angle variance
22+
BRANCH_THETA = Math::PI / 3
23+
attr_reader :position, :dir, :path, :children, :xbound, :speed, :ybound, :app
24+
25+
def initialize(pos, dir, speed)
26+
@renderer ||= AppRender.new(graphics)
27+
@position = pos
28+
@dir = dir
29+
@speed = speed
30+
@path = []
31+
@children = []
32+
@xbound = Boundary.new(0, width)
33+
@ybound = Boundary.new(0, height)
34+
path << pos
35+
end
36+
37+
def run
38+
grow
39+
display
40+
end
41+
42+
private
43+
44+
# NB: use of both rotate! (changes original) rotate (returns a copy) of Vec2D
45+
def grow
46+
check_bounds(position + (dir * speed)) if path.length < MAX_LEN
47+
@position += (dir * speed)
48+
dir.rotate!(rand(-0.5..0.5) * THETA)
49+
path << position
50+
if (length < MAX_GEN) && (rand < BRANCH_CHANCE)
51+
branch_dir = dir.rotate(rand(-0.5..0.5) * BRANCH_THETA)
52+
self << Branch.new(position.copy, branch_dir, speed * 0.99)
53+
end
54+
each(&:grow)
55+
end
56+
57+
def display
58+
begin_shape
59+
stroke(255)
60+
no_fill
61+
path.each { |vec| vec.to_vertex(@renderer) }
62+
end_shape
63+
each(&:display)
64+
end
65+
66+
def check_bounds(pos)
67+
dir.x *= -1 if xbound.exclude? pos.x
68+
dir.y *= -1 if ybound.exclude? pos.y
69+
end
70+
end
71+
72+
# we are looking for excluded values
73+
Boundary = Struct.new(:lower, :upper) do
74+
def exclude?(val)
75+
!(lower...upper).cover? val
76+
end
77+
end
78+
79+
@root = Branch.new(
80+
Vec2D.new(0, height / 2),
81+
Vec2D.new(1, 0),
82+
10.0
83+
)
84+
85+
def draw
86+
background(0)
87+
root.run
88+
end
89+
# ===== circles : circles
90+
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
91+
require 'vecmath'
92+
93+
visor_class :Circle do
94+
RADIUS = 50
95+
96+
def initialize
97+
@pos = Vec2D.new(rand(width), rand(height))
98+
end
99+
100+
def update(a)
101+
@pos.y += speed * a
102+
@pos.y = -RADIUS if @pos.y > height + RADIUS
103+
end
104+
105+
def draw(a)
106+
with_translate @pos.x, @pos.y do
107+
scale a
108+
noStroke
109+
fill 255
110+
ellipse 0, 0, RADIUS, RADIUS
111+
end
112+
end
113+
end
114+
115+
@circles = []
116+
150.times do
117+
@circles << Circle.new
118+
end
119+
120+
@speed ||= 10
121+
set_range :@speed, 0, 30
122+
123+
def draw
124+
rectMode CORNER
125+
noStroke
126+
fill 0, 50
127+
rect 0, 0, width, height
128+
129+
@circles.each_with_index do |circle, i|
130+
a = i / @circles.size.to_f
131+
132+
circle.update a + 0.1
133+
circle.draw a + 0.1
134+
end
135+
end
136+
137+
set_blend_mode MULTIPLY

0 commit comments

Comments
 (0)