Skip to content

Commit e029122

Browse files
committed
re-organise code
1 parent 5cef83f commit e029122

File tree

4 files changed

+141
-43
lines changed

4 files changed

+141
-43
lines changed

external_library/java/pgs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Processing Geometry Suite
2+
---
3+
Processing Geometry Suite is a software project that provides easy access to 2D geometric algorithms in the form of a Processing library. Written by Michael Carleton aka Micycle.
4+
5+
See [repository](https://github.com/micycle1/PGS/tree/master).
Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,61 @@
1-
#!/usr/bin/env jruby --dev
1+
#!/usr/bin/env jruby
2+
23
require 'picrate'
3-
require_relative './vector_list'
4+
45
class ContourMap < Processing::App
5-
load_library :pgs
6-
java_import 'micycle.pgs.PGS_Contour'
6+
load_libraries :pgs, :vector_list
7+
java_import 'micycle.pgs.PGS_Contour'
78

8-
attr_reader :heights, :max, :min
9-
MAX = -1
10-
MIN = 9999
9+
attr_reader :polygon, :heights, :max, :min
10+
MAX = -1
11+
MIN = 9999
1112

12-
def setup
13-
sketch_title 'Contour Map'
14-
@max = MAX
15-
@min = MIN
16-
end
13+
def setup
14+
sketch_title 'Contour Map'
15+
@max = MAX
16+
@min = MIN
17+
end
1718

18-
def draw
19-
background(0, 0, 40)
20-
populate_height_map
21-
isolines = PGS_Contour.isolines(heights.array_list, 0.08, min, max)
22-
isolines.to_hash.each do |isoline, value|
23-
isoline.set_stroke(
24-
color(
25-
map1d(value, min..max, 50..255),
26-
map1d(isoline.get_vertex(0).x, 0..width, 50..255),
27-
map1d(isoline.get_vertex(0).y, 0..height, 50..255)
28-
)
19+
def draw
20+
background(0, 0, 40)
21+
populate_height_map
22+
isolines = PGS_Contour.isolines(heights.array_list, 0.08, min, max)
23+
isolines.to_hash.each do |isoline, value|
24+
isoline.set_stroke(
25+
color(
26+
map1d(value, min..max, 50..255),
27+
map1d(isoline.get_vertex(0).x, 0..width, 50..255),
28+
map1d(isoline.get_vertex(0).y, 0..height, 50..255)
2929
)
30-
shape(isoline)
31-
end
30+
)
31+
shape(isoline)
3232
end
33+
end
3334

34-
def populate_height_map
35-
@heights = VectorList.new
36-
37-
res = 16
38-
anim_speed = 0.005
39-
40-
grid(width + res, height + res, res, res) do |x, y|
41-
z = norm(noise(x*0.01 + frame_count*anim_speed, y*0.01 + frame_count*anim_speed), -1.0, 1.0)
42-
h = Vec3D.new(x, y, 0)
43-
z += h.dist(Vec3D.new(mouse_x, mouse_y, 0))*0.005
44-
h.z = z
45-
heights << h
46-
@max = [max, z].max
47-
@min = [min, z].min
48-
end
49-
end
35+
def populate_height_map
36+
@heights = VectorList.new
5037

51-
def settings
52-
size(800, 800)
38+
res = 16
39+
anim_speed = 0.005
40+
41+
grid(width + res, height + res, res, res) do |x, y|
42+
z = norm(SmoothNoise.noise(x*0.01 + frame_count*anim_speed, y*0.01 + frame_count*anim_speed), -1.0, 1.0)
43+
h = Vec3D.new(x, y, 0)
44+
z += h.dist(Vec3D.new(mouse_x, mouse_y, 0))*0.005
45+
h.z = z
46+
heights << h
47+
@max = [max, z].max
48+
@min = [min, z].min
49+
end
50+
if frame_count > 20
51+
save_frame(data_path('sk0####.tif')) if (5..100).include? frame_count
5352
end
53+
stop if frame_count > 120
54+
end
55+
56+
def settings
57+
size(800, 800, P2D)
58+
end
5459
end
5560

5661
ContourMap.new
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
8+
class Letter
9+
include Processing::Proxy
10+
11+
attr_reader :hue, :l, :xn, :yn, :pos, :letter, :serif
12+
13+
def initialize(c)
14+
@pos = Vec2D.new(rand(width), rand(height))
15+
list = Java::ProcessingCore::PFont.list.to_a
16+
@serif = list.select { |fon| fon =~ /Serif/i }
17+
random_font = list.sample
18+
font = createFont(random_font, 96, true)
19+
@l = font.get_shape(c.to_java(:char))
20+
@hue = rand
21+
@xn = rand(4096)
22+
@yn = rand(4096)
23+
end
24+
25+
def update
26+
@xn += 0.005
27+
@yn += 0.005
28+
pos.x = map1d(noise(xn), -1.0..1.0, 0..width)
29+
pos.y = map1d(noise(yn), -1.0..1.0, 0..height)
30+
@letter = PGS_Transformation.translate_to(l, 0, 0)
31+
@letter = PGS_Transformation.shear(letter,
32+
map1d(@pos.x, 0..width, -TWO_PI..TWO_PI),
33+
map1d(@pos.y, 0..height, -TWO_PI..TWO_PI)
34+
)
35+
@letter = PGS_Transformation.translate_to(letter, pos.x, pos.y)
36+
@letter = PGS_Morphology.simplify(letter, 1) # as some fonts have very dense vertices
37+
letter.setStroke(color(hue, 1, 1))
38+
end
39+
40+
def randomise
41+
@hue = rand
42+
list = Java::ProcessingCore::PFont.list.to_a
43+
random_font = serif.sample
44+
font = createFont(random_font, 128, true)
45+
@l = font.getShape(rand(0..9).to_s.to_java(:char))
46+
end
47+
end
48+
49+
50+
attr_reader :l1, :l2
51+
52+
def setup
53+
sketch_title 'Mink Shear'
54+
color_mode(HSB, 1.0)
55+
@l1 = Letter.new('M')
56+
@l2 = Letter.new('L')
57+
end
58+
59+
def draw
60+
fill(color(0.1, 0.2))
61+
rect(0, 0, width, height)
62+
begin
63+
l1.update
64+
l2.update
65+
mink = PGS_Morphology.mink_sum(l1.letter, l2.letter)
66+
mink = PGS_Transformation.translate_to(mink, (l1.pos.x + l2.pos.x) / 2, (l1.pos.y + l2.pos.y) / 2)
67+
shape(mink)
68+
shape(l1.letter)
69+
shape(l2.letter)
70+
shape(PGS_Contour.medialAxis(mink, 0.3, 0, 0.1))
71+
intersect = PGS_ShapeBoolean.intersect(l1.letter, mink)
72+
PGS_Conversion.setAllFillColor(intersect, color(0, 0.5))
73+
shape(intersect)
74+
intersect = PGS_ShapeBoolean.intersect(l2.letter, mink)
75+
PGS_Conversion.setAllFillColor(intersect, color(0, 0.5))
76+
shape(intersect)
77+
rescue Java::JavaLang::Exception => e
78+
# puts e.to_s
79+
end
80+
if (frame_count % 120).zero?
81+
l1.randomise
82+
l2.randomise
83+
end
84+
end
85+
86+
def settings
87+
size(800, 800)
88+
end

0 commit comments

Comments
 (0)