Skip to content

Commit 554950c

Browse files
committed
arc tesselation example
1 parent 0dad759 commit 554950c

File tree

2 files changed

+98
-47
lines changed

2 files changed

+98
-47
lines changed

contributed/arc_tesselation.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
4+
# After an openprocessing sketch by Manoylov AC
5+
# https://www.openprocessing.org/user/23616/
6+
# press mouse to generate new pattern
7+
# use `c` key to toggle colored / greyscale
8+
# use 's' to save
9+
10+
class ArcTesselation < Propane::App
11+
attr_reader :cols, :coloured
12+
13+
PALETTE = %w[#152A3B #158ca7 #F5C03E #D63826 #0F4155 #7ec873 #4B3331].freeze
14+
15+
def settings
16+
size(600, 600)
17+
end
18+
19+
def setup
20+
background 0
21+
sketch_title 'Arc Tesselation'
22+
# create a java primitive array of signed int
23+
@cols = web_to_color_array(PALETTE)
24+
stroke_weight 1.5
25+
stroke_cap SQUARE
26+
stroke(0, 200)
27+
@coloured = true
28+
end
29+
30+
def draw
31+
arc_pattern
32+
end
33+
34+
def sep_index(idx, length)
35+
(idx - (length - 1) * 0.5).abs.floor
36+
end
37+
38+
def sep_color(idx, number)
39+
cols[sep_index(idx - 1, number + 1)]
40+
end
41+
42+
def arc_pattern
43+
circ_number = rand(4..10)
44+
block_size = rand(30..70)
45+
back_color = coloured ? cols.last : 255
46+
fill(back_color)
47+
rect(0, 0, width, height)
48+
half_block = block_size / 2
49+
two_block = 2 * block_size
50+
grid(width + two_block, height + two_block, block_size, block_size) do |x, y|
51+
push_matrix
52+
translate x, y
53+
rotate HALF_PI * rand(0..4)
54+
circ_number.downto(0) do |i|
55+
diam = two_block * i / (circ_number + 1)
56+
ccolor = i < 2 || !coloured ? back_color : sep_color(i, circ_number)
57+
fill ccolor
58+
arc(-half_block, -half_block, diam, diam, 0, HALF_PI)
59+
arc(half_block, half_block, diam, diam, PI, PI + HALF_PI)
60+
end
61+
pop_matrix
62+
end
63+
no_loop
64+
end
65+
66+
def mouse_pressed
67+
@cols = shuffle_array(cols) if coloured
68+
loop
69+
end
70+
71+
def key_typed
72+
case key
73+
when 'c', 'C'
74+
@coloured = !coloured
75+
when 's', 'S'
76+
save(data_path('arc_pattern.png'))
77+
end
78+
end
79+
80+
# do a ruby shuffle! on a primitive java array
81+
def shuffle_array(arr)
82+
cols = arr.to_a
83+
cols.shuffle!
84+
cols.to_java(Java::int)
85+
end
86+
end
87+
88+
ArcTesselation.new

contributed/mandelbrot.rb

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@ def setup
1515

1616
# main drawing method
1717
def draw
18-
(0...900).each do |x|
19-
(0...600).each do |y|
20-
c = Complex.new(map1d(x, (0...900), (-3..1.5)), map1d(y, (0...600), (-1.5..1.5)))
21-
# mandel will return 0 to 20 (20 is strong)
22-
# map this to 0, 255 (and flip it)
23-
pixels[x + y * 900] = color(255 - map1d(mandel(c, 20), (0..20), (0..255)).to_i)
24-
end
18+
grid(900, 600) do |x, y|
19+
c = Complex(
20+
map1d(x, (0...900), (-3..1.5)), map1d(y, (0...600), (-1.5..1.5))
21+
)
22+
# mandel will return 0..20 (20 is strong) map this to 255..0 (NB: reverse)
23+
pixels[x + y * 900] = color(map1d(mandel(c, 20), (0..20), (255..0)).to_i)
2524
end
2625
update_pixels
2726
end
2827

2928
# calculates the "accuracy" of a given point in the mandelbrot set
30-
# : how many iterations the number survives without becoming chaotic
29+
# how many iterations the number survives without becoming chaotic
3130
def mandel(z, max = 10)
3231
score = 0
33-
c = z.clone
32+
c = z
3433
while score < max
3534
# z = z^2 + c
36-
z.square
37-
z.add c
35+
z *= z
36+
z += c
3837
break if z.abs > 2
3938
score += 1
4039
end
@@ -44,42 +43,6 @@ def mandel(z, max = 10)
4443
def settings
4544
size 900, 600
4645
end
47-
# rolled my own Complex class
48-
# including only the functionality I need (abs, square, add, to_s)
49-
#
50-
# Using this class, runs in ~12.5s on my MacBook Air
51-
# compared to ~22s using ruby's Complex struct
52-
class Complex
53-
attr_accessor :real, :imag
54-
55-
def initialize(real, imag)
56-
@real = real
57-
@imag = imag
58-
end
59-
60-
# squares a complex number - overwriting it
61-
def square
62-
r = real * real - imag * imag
63-
i = 2 * real * imag
64-
@real = r
65-
@imag = i
66-
end
67-
68-
# adds a given complex number
69-
def add(c)
70-
@real += c.real
71-
@imag += c.imag
72-
end
73-
74-
# computes the magnitude (HelperMethods dist is a safer version of Math.hypot)
75-
def abs
76-
Mandelbrot.dist(0, 0, real, imag)
77-
end
78-
79-
def to_s
80-
format('(%f+%fi)', real, imag)
81-
end
82-
end
8346
end
8447

8548
Mandelbrot.new

0 commit comments

Comments
 (0)