This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
barnsley-fern.scad
62 lines (53 loc) · 1.62 KB
/
barnsley-fern.scad
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
// https://en.wikipedia.org/wiki/Barnsley_fern
// set to 0 for randomized seed values
seed = 37;
scale = 10;
count = 50000;
radius = 0.2;
s = seed > 0 ? seed : floor(rands(0, 100000, 1)[0]);
rand = rands(0, 1, count, s);
function nextx(p, r) =
r < 0.01
? 0
: r < 0.86
? 0.85 * p.x + 0.04 * p.y
: r < 0.93
? 0.20 * p.x - 0.26 * p.y
: -0.15 * p.x + 0.28 * p.y;
function nexty(p, r) =
r < 0.01
? 0.16 * p.y
: r < 0.86
? -0.04 * p.x + 0.85 * p.y + 1.6
: r < 0.93
? 0.23 * p.x + 0.22 * p.y + 1.6
: 0.26 * p.x + 0.24 * p.y + 0.44;
function next(p, r) = [
nextx(p, r),
nexty(p, r)
];
function points(cnt) = [
for (i = 0, p = [0, 0];i < cnt;i = i + 1, p = next(p, rand[i])) p
];
module barnsley_fern() {
echo(seed = s);
%if (seed == 0)
translate([0, -scale / 3])
linear_extrude(0.01)
text(str("seed = ", s), scale / 5, halign = "center");
for (p = points(count))
color("Green")
translate(scale * p)
cylinder(r = radius, h = 0.01);
}
barnsley_fern();
// Written in 2020 by Torsten Paul <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any
// warranty.
//
// You should have received a copy of the CC0 Public Domain
// Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.