Skip to content

Commit 7303b75

Browse files
committed
some vecmath examples
1 parent c5fcab7 commit 7303b75

File tree

6 files changed

+223
-0
lines changed

6 files changed

+223
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# The current local time can be read with the Time.now
5+
# the seconds Time.now.sec, minutes Time.now.min etc...
6+
# functions. In this example, DegLut.sin() and DegLut.cos() values are used to
7+
# set the position of the hands, perfect for degree precision Lookup Table.
8+
class Clock < Propane::App
9+
10+
def setup
11+
sketch_title 'Clock'
12+
stroke 255
13+
@font = create_font 'Roman', 20
14+
text_font @font
15+
end
16+
17+
def draw
18+
background 0
19+
fill 80
20+
no_stroke
21+
# adj factor to map 0-60 to 0-360 (sec/min) & 0-12 to 0-360 (hours)
22+
# since angles for DegLut.sin() and DegLut.cos() start at 3 o'clock we
23+
# subtract 90 degrees to make them start at the top.
24+
clock_x = lambda do |val, adj, length|
25+
DegLut.cos((val * adj).to_i - 90) * length + width / 2
26+
end
27+
clock_y = lambda do |val, adj, length|
28+
DegLut.sin((val * adj).to_i - 90) * length + height / 2
29+
end
30+
ellipse 100, 100, 160, 160
31+
stroke 220
32+
stroke_weight 6
33+
t = Time.now
34+
line(100, 100, clock_x.call(t.hour % 12 + (t.min / 60.0), 30, 50),
35+
clock_y.call(t.hour % 12 + (t.min / 60.0), 30, 50))
36+
stroke_weight 3
37+
line(100, 100, clock_x.call(t.min + (t.sec / 60.0), 6, 60),
38+
clock_y.call(t.min + (t.sec / 60.0), 6, 60))
39+
stroke 255, 0, 0
40+
stroke_weight 1
41+
line(100, 100, clock_x.call(t.sec, 6, 72), clock_y.call(t.sec, 6, 72))
42+
# Draw the minute ticks
43+
stroke_weight 2
44+
stroke 255
45+
(0..360).step(6) do |a|
46+
x = 100 + DegLut.cos(a) * 72
47+
y = 100 + DegLut.sin(a) * 72
48+
point x, y
49+
end
50+
fill 200
51+
text t.strftime('%H:%M:%S'), 50, 200
52+
end
53+
54+
def settings
55+
size 200, 220, P2D
56+
smooth 8
57+
end
58+
end
59+
60+
Clock.new
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 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
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
require 'arcball'
5+
# The current local time can be read with the Time.now
6+
# the seconds Time.now.sec, minutes Time.now.min etc...
7+
# functions. In this example, DegLut.sin() and DegLut.cos() values are used to
8+
# set the position of the hands, perfect for degree precision Lookup Table.
9+
class ArcBallBox < Propane::App
10+
############################
11+
# Use mouse drag to rotate
12+
# the arcball. Use mousewheel
13+
# to zoom. Hold down x, y, z
14+
# to constrain rotation axis.
15+
############################
16+
def setup
17+
sketch_title 'Arcball Box'
18+
Processing::ArcBall.init self, 300, 300
19+
fill 180
20+
end
21+
22+
def draw
23+
background 50
24+
box 300, 300, 300
25+
end
26+
27+
def settings
28+
size 600, 600, P3D
29+
smooth 8
30+
end
31+
end
32+
33+
ArcBallBox.new
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
require 'arcball'
5+
6+
class ArcBallShape < Propane::App
7+
attr_reader :my_cube
8+
9+
def setup
10+
sketch_title 'Arcball Shape'
11+
Processing::ArcBall.init(self)
12+
@my_cube = create_shape BOX, 400, 400, 400
13+
my_cube.set_fill(color(100, 10, 100))
14+
end
15+
16+
def draw
17+
background(50, 50, 100)
18+
define_lights
19+
lights
20+
stroke(0)
21+
shape(my_cube)
22+
end
23+
24+
def define_lights
25+
ambient(20, 20, 20)
26+
ambient_light(50, 50, 50)
27+
point_light(30, 30, 30, 200, -150, 0)
28+
directional_light(0, 30, 50, 1, 0, 0)
29+
spot_light(30, 30, 30, 0, 40, 200, 0, -0.5, -0.5, PI / 2, 2)
30+
end
31+
32+
def settings
33+
size 600, 600, P3D
34+
smooth 8
35+
end
36+
end
37+
38+
ArcBallShape.new
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
require 'arcball'
5+
6+
############################
7+
# Use mouse drag to rotate
8+
# the arcball. Use mousewheel
9+
# to zoom. Constrained to
10+
# rotation around y-axis.
11+
############################
12+
class Constrain < Propane::App
13+
14+
def setup
15+
sketch_title 'Arcball Box'
16+
Processing::ArcBall.constrain self
17+
fill 180
18+
end
19+
20+
def draw
21+
background 50
22+
box 300, 300, 300
23+
end
24+
25+
def settings
26+
size 600, 600, P3D
27+
smooth 8
28+
end
29+
end
30+
31+
Constrain.new
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
require 'arcball'
5+
6+
############################
7+
# Use mouse drag to rotate
8+
# the arcball. Use mousewheel
9+
# to zoom. Constrained to
10+
# rotation around z-axis
11+
############################
12+
class Constrain < Propane::App
13+
14+
def setup
15+
sketch_title 'Arcball Box'
16+
Processing::ArcBall.constrain self, :zaxis
17+
fill 180
18+
end
19+
20+
def draw
21+
background 50
22+
box 300, 300, 300
23+
end
24+
25+
def settings
26+
size 600, 600, P3D
27+
smooth 8
28+
end
29+
end
30+
31+
Constrain.new

0 commit comments

Comments
 (0)