Skip to content

Commit d72f1b3

Browse files
committed
more examples
1 parent 58cc704 commit d72f1b3

File tree

24 files changed

+1475
-14
lines changed

24 files changed

+1475
-14
lines changed

contributed/bezier_playground.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env jruby
2+
# frozen_string_literal: true
23
require 'propane'
34

45
X1, Y1, X2, Y2 = 50.0, 50.0, 250.0, 250.0
5-
REDDISH = [250, 100, 100]
6+
REDDISH = [250, 100, 100].freeze
67
RADIUS = 7
78

89
# A Bezier playground. Click to shape the curve. Drag to move it.
@@ -11,9 +12,7 @@
1112
class BezierPlayground < Propane::App
1213
load_libraries :control_panel, :curve
1314
include Olap
14-
attr_accessor :curves, :c1x, :c1y, :c2x, :c2y
15-
attr_reader :panel, :hide
16-
15+
attr_reader :curves, :c1x, :c1y, :c2x, :c2y, :panel, :hide
1716

1817
def settings
1918
size 300, 300
@@ -103,7 +102,7 @@ def switch_curve_if_endpoint_clicked
103102
become = curves.detect { |c| c.contains(mouse_x, mouse_y) }
104103
return unless become && become != current_curve
105104
current_curve.set_control_points(*control_points)
106-
self.set_control_points(*become.control_points)
105+
set_control_points(*become.control_points)
107106
@current = curves.index(become)
108107
end
109108

contributed/fire.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#!/usr/bin/env jruby
2+
# frozen_string_literal: true
13
require 'propane'
2-
34
# After original by Alex Young https://github.com/alexyoung/fire-p5r
45

56
# Algorithm:
@@ -76,4 +77,4 @@ def offset(x, y)
7677
end
7778
end
7879

79-
Fire.new
80+
Fire.new

contributed/full_screen.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def draw
1717
new_x, new_y = x * 100, y * 100
1818
push_matrix
1919
translate new_x + 50, new_y + 50
20-
rotate_y(((mouse_x.to_f + new_x) / width) * Math::PI)
21-
rotate_x(((mouse_y.to_f + new_y) / height) * Math::PI)
20+
rotate_y(((mouse_x.to_f + new_x) / width) * PI)
21+
rotate_x(((mouse_y.to_f + new_y) / height) * PI)
2222
box 90
2323
pop_matrix
2424
end

contributed/library/palette/palette.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# frozen_string_literal: true
2+
# To Avoid the:-
3+
# warning: ambiguous Java methods found, using color(float,float,float)
4+
# we explicitly cast the array of color values `to_java(:float)`, as they are
5+
# passed to the (overloaded) java/processing color method
6+
17
class Palette
28
include Propane::Proxy # needed to access 'color' and 'map1d'
39
attr_reader :palette
@@ -11,13 +17,13 @@ def make_palette
1117
# Create the bands of colour for the palette (256 is the maximum colour))
1218
case i
1319
when 0..64 # Range of reds
14-
color(*[map1d(i, 0..64, 1..255), 0, 0])
20+
color(*[map1d(i, 0..64, 1..255), 0, 0].to_java(:float))
1521
when 64..128 # Range of orange
16-
color(*[255, map1d(i, 64..128, 1..255), 0])
22+
color(*[255, map1d(i, 64..128, 1..255), 0].to_java(:float))
1723
when 128..172 # range of yellow
18-
color(*[255, 255, map1d(i, 128..172, 1..255)])
24+
color(*[255, 255, map1d(i, 128..172, 1..255)].to_java(:float))
1925
else
20-
color(*[180, 0, 0])
26+
color(*[180, 0, 0].to_java(:float))
2127
end
2228
end
2329
end

contributed/tree.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/env jruby -v -W2
1+
#!/usr/bin/env jruby
2+
# frozen_string_literal: true
23
require 'propane'
34

45
class Tree < Propane::App
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Simple demo Rakefile to autorun samples in current directory
2+
# adjust path to k9 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+
Dir.chdir(SAMPLES_DIR)
16+
Dir.glob('*.rb').map { |file| File.join(SAMPLES_DIR, file) }
17+
end
18+
19+
def run_sample(sample_name)
20+
puts format('Running %s...quit to run next sample', sample_name)
21+
open("|jruby #{sample_name}", 'r') do |io|
22+
while l = io.gets
23+
puts(l.chop)
24+
end
25+
end
26+
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
4+
class ChequerSketch < Propane::App
5+
########################################################
6+
# Chequer implemented using a grammar library
7+
# Lindenmayer System in JRubyArt by Martin Prout
8+
########################################################
9+
load_library :grammar
10+
attr_reader :chequer
11+
12+
def setup
13+
sketch_title 'Chequer'
14+
@chequer = Chequer.new width * 0.9, height / 10
15+
chequer.create_grammar 4
16+
no_loop
17+
end
18+
19+
def draw
20+
background 0
21+
chequer.render
22+
end
23+
24+
def settings
25+
size 600, 600
26+
end
27+
end
28+
29+
# Chequer class
30+
class Chequer
31+
include Propane::Proxy
32+
attr_accessor :axiom, :grammar, :production, :draw_length, :theta, :xpos, :ypos
33+
DELTA = Math::PI / 2
34+
35+
def initialize(xpos, ypos)
36+
@xpos = xpos
37+
@ypos = ypos
38+
@axiom = 'F-F-F-F' # Axiom
39+
@grammar = Grammar.new(axiom, 'F' => 'FF-F-F-F-FF')
40+
@draw_length = 500
41+
stroke 0, 255, 0
42+
stroke_weight 2
43+
@theta = 0
44+
end
45+
46+
def render
47+
production.each do |element|
48+
case element
49+
when 'F'
50+
x_temp = xpos
51+
y_temp = ypos
52+
@xpos -= draw_length * cos(theta)
53+
@ypos -= draw_length * sin(theta)
54+
line(x_temp, y_temp, xpos, ypos)
55+
when '+'
56+
@theta += DELTA
57+
when '-'
58+
@theta -= DELTA
59+
else
60+
puts format("Character '%s' is not in grammar", element)
61+
end
62+
end
63+
end
64+
65+
##############################
66+
# create grammar from axiom and
67+
# rules (adjust scale)
68+
##############################
69+
70+
def create_grammar(gen)
71+
@draw_length /= 3**gen
72+
@production = @grammar.generate gen
73+
end
74+
end
75+
76+
77+
ChequerSketch.new
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
require 'arcball'
4+
5+
class CSplantSketch < Propane::App
6+
########################################################
7+
# csplant.rb
8+
# A 3D Plant implemented using a Context Sensitive
9+
# Lindenmayer System in JRubyArt
10+
# by Martin Prout (30 January 2013)
11+
# Hold down 'y' key and drag mouse to rotate about y axis
12+
########################################################
13+
14+
load_libraries :cs_grammar
15+
attr_reader :csplant
16+
17+
def setup
18+
sketch_title 'Context Sensitive Plant'
19+
Processing::ArcBall.constrain(self) # Arcball constrained to y-axis rotation
20+
@csplant = CSPlant.new(height)
21+
csplant.create_grammar 5
22+
no_stroke
23+
end
24+
25+
def draw
26+
background 0
27+
lights
28+
translate(0, height * 0.3)
29+
csplant.render
30+
end
31+
32+
def settings
33+
size 800, 800, P3D
34+
end
35+
end
36+
37+
############
38+
# CSPlant
39+
############
40+
class CSPlant
41+
include Propane::Proxy
42+
43+
IGNORE = '[]+-^&3'
44+
attr_reader :grammar, :axiom, :production, :premis, :rule,
45+
:theta, :scale_factor, :len, :phi, :len
46+
47+
def initialize(len)
48+
@axiom = 'F'
49+
@grammar = Grammar.new(
50+
axiom,
51+
{ 'F' => 'F[-EF[3&A]]E[+F[3^A]]', 'F<E' => 'F[&F[3+A]][^F[3-A]]' },
52+
IGNORE
53+
)
54+
@production = axiom
55+
@len = len
56+
@theta = 25.radians
57+
@phi = 25.radians
58+
no_stroke
59+
end
60+
61+
def render
62+
fill(0, 75, 152)
63+
light_specular(204, 204, 204)
64+
specular(255, 255, 255)
65+
shininess(1.0)
66+
repeat = 1
67+
production.each_char do |ch|
68+
case ch
69+
when 'F'
70+
translate(0, len / -2, 0)
71+
box(len / 9, len, len / 9)
72+
translate(0, len / -2, 0)
73+
when '+'
74+
rotateX(-theta * repeat)
75+
repeat = 1
76+
when '-'
77+
rotateX(theta * repeat)
78+
repeat = 1
79+
when '&'
80+
rotateZ(-phi * repeat)
81+
repeat = 1
82+
when '^'
83+
rotateZ(phi * repeat)
84+
repeat = 1
85+
when '3'
86+
repeat = 3
87+
when '['
88+
push_matrix
89+
when ']'
90+
pop_matrix
91+
when 'E', 'A'
92+
else
93+
puts("character '#{ch}' not in grammar")
94+
end
95+
end
96+
end
97+
##############################
98+
# create grammar from axiom and
99+
# rules (adjust scale)
100+
##############################
101+
102+
def create_grammar(gen)
103+
@len *= 0.6**gen
104+
@production = grammar.generate gen
105+
end
106+
end
107+
108+
CSplantSketch.new
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
4+
class CSTest < Propane::App
5+
###########################
6+
# cstest.rb (use jruby ..
7+
# test of cs_grammar library
8+
###########################
9+
load_library 'cs_grammar'
10+
11+
def setup
12+
sketch_title 'Context Sensitive Test'
13+
background 0
14+
fill(200, 200, 0)
15+
f = create_font('Arial', 16, true)
16+
text_font(f)
17+
no_loop
18+
end
19+
20+
def draw
21+
(0..7).each do |i|
22+
grammar = Grammar.new(
23+
'baaaaaa',
24+
'b<a' => 'b', # context sensitive rule replace a when preceded by b
25+
'b' => 'a'
26+
)
27+
text grammar.generate(i), 30, i * 25
28+
end
29+
end
30+
31+
def settings
32+
size 125, 250, FX2D
33+
end
34+
end
35+
36+
CSTest.new
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-Djruby.ji.objectProxyCache=false -XX:CompileCommand=dontinline,org.jruby.runtime.invokedynamic.InvokeDynamicSupport::invocationFallback
2+
-XX:+OptimizeStringConcat
3+

0 commit comments

Comments
 (0)