Skip to content

Commit 5cef83f

Browse files
committed
A couple of new examples
1 parent 73d427f commit 5cef83f

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

demo/library/generator/generator.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# Noise Generator
4+
class Generator
5+
include SmoothNoise
6+
attr_reader :nx, :ny, :nz
7+
8+
R = 6
9+
RR = 1
10+
11+
def initialize(phi, theta)
12+
base = R + RR * Math.cos(phi)
13+
tx = base * Math.cos(theta)
14+
ty = base * Math.sin(theta)
15+
tz = RR * Math.sin(phi)
16+
@nx = MathTool.norm(tx, 0.0, R + RR)
17+
@ny = MathTool.norm(ty, 0.0, R + RR)
18+
@nz = MathTool.norm(tz, 0.0, RR)
19+
end
20+
21+
def noisef(time)
22+
(
23+
(SmoothNoise.noise(nx, ny, nz, time) + 1) * 128
24+
).floor
25+
end
26+
end

demo/psychedelic_noise.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
4+
# Based on a sketch by Javi F Gorostiza
5+
# https://openprocessing.org/sketch/432548
6+
class PsychedelicNoise < Processing::App
7+
load_library :generator
8+
attr_reader :img, :colors, :remap1, :remap2
9+
10+
def setup
11+
sketch_title 'Psychedelic Noise Pattern'
12+
color_mode(RGB, 1.0)
13+
@theta = 0
14+
@phi = 0
15+
# the dimensions of the image are twice the dimentions of
16+
# the canvas to add antialiasing when the image is reduced
17+
@img = create_image(width, height, RGB)
18+
@colors = (0..255).map { color(rand, rand, rand) }
19+
end
20+
21+
22+
def draw
23+
# fill the array with rand colors
24+
# create pattern
25+
img.load_pixels
26+
iwidth = img.width
27+
iheight = img.height
28+
29+
grid(iwidth, iheight) do |x, y|
30+
# map x and y to angles between 0 and TWO_PI
31+
theta = map1d(x, 0..iwidth, 0..TWO_PI)
32+
phi = map1d(y, 0..iheight, 0..TWO_PI)
33+
generator = Generator.new(phi, theta)
34+
# apply noise twice and use the equivalent color on the pallete
35+
img.pixels[x + y * iwidth] = colors[generator.noisef(frame_count / 5)]
36+
end
37+
img.update_pixels
38+
# display pattern
39+
background(img)
40+
end
41+
42+
def key_pressed
43+
save(data_path('image.png'))
44+
end
45+
46+
def settings
47+
size(500, 500)
48+
end
49+
end
50+
51+
PsychedelicNoise.new
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env jruby --dev
2+
require 'picrate'
3+
require_relative './vector_list'
4+
class ContourMap < Processing::App
5+
load_library :pgs
6+
java_import 'micycle.pgs.PGS_Contour'
7+
8+
attr_reader :heights, :max, :min
9+
MAX = -1
10+
MIN = 9999
11+
12+
def setup
13+
sketch_title 'Contour Map'
14+
@max = MAX
15+
@min = MIN
16+
end
17+
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+
)
29+
)
30+
shape(isoline)
31+
end
32+
end
33+
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
50+
51+
def settings
52+
size(800, 800)
53+
end
54+
end
55+
56+
ContourMap.new
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
java_import 'java.util.ArrayList'
2+
java_import 'processing.core.PVector'
3+
4+
class VectorList
5+
attr_reader :array_list
6+
def initialize
7+
@array_list = ArrayList.new
8+
end
9+
10+
def <<(val)
11+
array_list.add(PVector.new(val.x, val.y, val.z))
12+
end
13+
end

0 commit comments

Comments
 (0)