-
Notifications
You must be signed in to change notification settings - Fork 0
/
barnsley_fern_ifs.sf
88 lines (75 loc) · 1.42 KB
/
barnsley_fern_ifs.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/ruby
# Code from a lesson by Keith Peters.
# See: https://www.youtube.com/watch?v=geqq63WFLr0
require('Imager')
var (width, height) = (500, 500)
var img = %O<Imager>.new(xsize => width, ysize => height)
struct Rule {
a, b, c, d, tx, ty, w
}
var rules = [
Rule(
a: 0.85,
b: 0.04,
c: -0.04,
d: 0.85,
tx: 0,
ty: 1.6,
w: 0.85,
),
Rule(
a: -0.15,
b: 0.28,
c: 0.26,
d: 0.24,
tx: 0,
ty: 0.44,
w: 0.07,
),
Rule(
a: 0.2,
b: -0.26,
c: 0.23,
d: 0.22,
tx: 0,
ty: 1.6,
w: 0.07,
),
Rule(
a: 0,
b: 0,
c: 0,
d: 0.16,
tx: 0,
ty: 0,
w: 0.01,
)
]
func plot(x, y) {
static green = %O<Imager::Color>.new('#00ff00')
img.setpixel(
x => width/2 + Math.map(2*x, 0, 10, 0, width/2),
y => height - Math.map(2*y, 0, 10, 0, height/2),
color => green
)
}
func getRule {
var r = 1.rand
for rule in (rules) {
if (r < rule.w) {
return rule
}
r -= rule.w
}
}
var (x, y) = 2.of { 1.rand }...
var iterate = {
var rule = getRule()
var x1 = (x*rule.a + y*rule.b + rule.tx)
var y1 = (x*rule.c + y*rule.d + rule.ty)
x = x1
y = y1
plot(x, y)
}
iterate * 2000
img.write(file => 'barnsley_fern.png')