Skip to content

Commit 560c98c

Browse files
committed
Another pgs library example
1 parent f3b3817 commit 560c98c

File tree

5 files changed

+109
-16
lines changed

5 files changed

+109
-16
lines changed

external_library/java/pgs/contour_map.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,20 @@ def populate_height_map
3535
anim_speed = 0.005
3636

3737
grid(width + res, height + res, res, res) do |x, y|
38-
z = norm(noise(x*0.01 + frame_count*anim_speed, y*0.01 + frame_count*anim_speed), -1.0, 1.0)
38+
z = norm(SmoothNoise.noise(x*0.01 + frame_count*anim_speed, y*0.01 + frame_count*anim_speed), -1.0, 1.0)
3939
h = Vec3D.new(x, y, 0)
4040
z += h.dist(Vec3D.new(mouse_x, mouse_y, 0))*0.005
4141
h.z = z
4242
heights << h
4343
@max = [max, z].max
4444
@min = [min, z].min
4545
end
46+
if frame_count > 20
47+
save_frame(data_path('sk0####.tif')) if (5..100).include? frame_count
48+
end
49+
stop if frame_count > 120
4650
end
4751

4852
def settings
49-
size(800, 800)
50-
smooth
53+
size(800, 800, P2D)
5154
end

external_library/java/pgs/letter.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
%w[PGS_Morphology PGS_Transformation].each do |klass|
2+
java_import "micycle.pgs.#{klass}"
3+
end
4+
5+
class Letter
6+
include Processing::Proxy
7+
attr_reader :hue, :l, :xn, :yn, :pos, :letter
8+
9+
def initialize(c)
10+
@pos = Vec2D.new(rand(width), rand(height))
11+
list = Java::ProcessingCore::PFont.list.to_a
12+
random_font = list.sample
13+
font = createFont(random_font, 96, true)
14+
@l = font.get_shape(c.to_java(:char))
15+
@hue = rand
16+
@xn = rand(4096)
17+
@yn = rand(4096)
18+
end
19+
20+
def update
21+
@xn += 0.005
22+
@yn += 0.005
23+
pos.x = map1d(noise(xn), -1.0..1.0, 0..width)
24+
pos.y = map1d(noise(yn), -1.0..1.0, 0..height)
25+
@letter = PGS_Transformation.translate_to(l, 0, 0)
26+
@letter = PGS_Transformation.shear(letter,
27+
map1d(@pos.x, 0..width, -TWO_PI..TWO_PI),
28+
map1d(@pos.y, 0..height, -TWO_PI..TWO_PI)
29+
)
30+
@letter = PGS_Transformation.translate_to(letter, pos.x, pos.y)
31+
@letter = PGS_Morphology.simplify(letter, 1) # as some fonts have very dense vertices
32+
letter.setStroke(color(hue, 1, 1))
33+
end
34+
35+
def randomise
36+
@hue = rand
37+
list = Java::ProcessingCore::PFont.list.to_a
38+
random_font = list.sample
39+
font = createFont(random_font, 128, true)
40+
@l = font.getShape(rand(0..9).to_s.to_java(:char))
41+
end
42+
end
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
load_library :pgs
2+
%w[
3+
PGS_Contour PGS_Conversion PGS_Morphology PGS_Transformation PGS_ShapeBoolean
4+
].each do |klass|
5+
java_import "micycle.pgs.#{klass}"
6+
end
7+
require_relative 'letter'
8+
9+
attr_reader :l1, :l2
10+
11+
def setup
12+
sketch_title 'Mink Shear'
13+
color_mode(HSB, 1.0)
14+
@l1 = Letter.new('M')
15+
@l2 = Letter.new('L')
16+
end
17+
18+
def draw
19+
fill(color(0.1, 0.2))
20+
rect(0, 0, width, height)
21+
begin
22+
l1.update
23+
l2.update
24+
mink = PGS_Morphology.mink_sum(l1.letter, l2.letter)
25+
mink = PGS_Transformation.translate_to(mink, (l1.pos.x + l2.pos.x) / 2, (l1.pos.y + l2.pos.y) / 2)
26+
shape(mink)
27+
shape(l1.letter)
28+
shape(l2.letter)
29+
shape(PGS_Contour.medialAxis(mink, 0.3, 0, 0.1))
30+
intersect = PGS_ShapeBoolean.intersect(l1.letter, mink)
31+
PGS_Conversion.setAllFillColor(intersect, color(0, 0.5))
32+
shape(intersect)
33+
intersect = PGS_ShapeBoolean.intersect(l2.letter, mink)
34+
PGS_Conversion.setAllFillColor(intersect, color(0, 0.5))
35+
shape(intersect)
36+
rescue Java::JavaLang::Exception => e
37+
puts e.to_s
38+
end
39+
if (frame_count % 120).zero?
40+
l1.randomise
41+
l2.randomise
42+
end
43+
end
44+
45+
def settings
46+
size(800, 800)
47+
end

processing_app/basics/typography/explore_fonts.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
java_import 'processing.core.PFont'
2+
13
attr_reader :sans, :serif, :sans_font, :serif_font
24

35
def settings
@@ -8,9 +10,8 @@ def setup
810
sketch_title 'System Fonts Explorer'
911
sans_fonts = PFont.list.select { |f| f =~ /Sans/i }
1012
serif_fonts = PFont.list.select { |f| f =~ /Serif/i }
11-
# adjust array index as required
12-
@sans = sans_fonts[sans_fonts.length / 2]
13-
@serif = serif_fonts[serif_fonts.length / 2]
13+
@sans = sans_fonts[0]
14+
@serif = serif_fonts.sample
1415
@sans_font = create_font(sans, 40, true)
1516
@serif_font = create_font(serif, 40, true)
1617
puts sans_fonts

processing_app/basics/typography/words.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Words.
2-
#
3-
# The text() function is used for writing words to the screen.
1+
# Words.
2+
#
3+
# The text() function is used for writing words to the screen.
44

5-
PFont = Java::ProcessingCore::PFont
5+
java_import 'processing.core.PFont'
66

77
def setup
88
sketch_title 'Words'
99
@x = 30
1010
puts 'Available Fonts:'
11-
PFont::list.each{ |f| puts f }
12-
@font = create_font('Georgia', 24)
13-
text_font @font, 32
11+
PFont::list.each{ |f| puts f }
12+
@font = create_font('SansSerif.bold', 24)
13+
text_font @font, 32
1414
no_loop
1515
end
1616

@@ -24,17 +24,17 @@ def draw
2424
draw_type(width * 0.75)
2525
end
2626

27-
def draw_type(x)
27+
def draw_type(x)
2828
line(x, 0, x, 65)
29-
line(x, 220, x, height)
29+
line(x, 220, x, height)
3030
fill 0
3131
text 'ichi', x, 95
3232
fill 51
3333
text 'ni', x, 130
3434
fill 204
3535
text 'san', x, 165
3636
fill 255
37-
text 'shi', x, 210
37+
text 'shi', x, 210
3838
end
3939

4040
def settings

0 commit comments

Comments
 (0)