-
Notifications
You must be signed in to change notification settings - Fork 0
/
sierpinski_basic.pde
152 lines (135 loc) · 2.37 KB
/
sierpinski_basic.pde
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// http://www.openprocessing.org/sketch/108475
// sierpinski fractal, basic version, no color, @Soesilo Wijono
// ref: en.wikipedia.org/wiki/Sierpi%C5%84ski_curve
// play around with n and nOmit, warning: larger n will slow down
int n; // levels
int h0;
int h, x, y, x0, y0;
int nOmit; // levels to be omitted
int prevx, prevy;
boolean isFirst, isOne;
void setup() {
size(800, 600);
n = 4; // levels
nOmit = 2; // levels to be omitted, must < n
h0 = 512;
inits();
stroke(#ffffff); // default
strokeWeight(2);
noLoop();
}
void draw() {
background(0);
display(); // display objects
drive(); // variables changes
}
void inits() {
// initial values for each cycle
prevx = -1;
prevy = -1;
isFirst = false;
isOne = true;
}
void display() {
// objects to display each cycle
ABCD();
}
void drive() {
inits();
// put cycle variable changes here..
}
void plot() {
// plot the sierpinski vertices,
// basic no color
if (prevx < 0 && prevy < 0) {
prevx = x0;
prevy = y0;
}
if (isOne) { // to be omitted
stroke(#ffffff, 0);
fill(#ffffff, 0);
}
else {
stroke(#ffffff);
fill(#ffffff);
}
if (isFirst) {
line(x0, y0, x, y);
isFirst = false;
}
else {
line(prevx, prevy, x, y);
}
prevx = x;
prevy = y;
}
// all sierpinski recursive..
void A(int i) {
if (i > 0) {
A(i-1);
x += h; y -= h; plot();
B(i-1);
x += 2*h; plot();
D(i-1);
x += h; y += h; plot();
A(i-1);
}
}
void B(int i) {
if (i > 0) {
B(i-1);
x -= h; y -= h; plot();
C(i-1);
y -= 2*h; plot();
A(i-1);
x += h; y -= h; plot();
B(i-1);
}
}
void C(int i) {
if (i > 0) {
C(i-1);
x -= h; y += h; plot();
D(i-1);
x -= 2*h; plot();
B(i-1);
x -= h; y -= h; plot();
C(i-1);
}
}
void D(int i) {
if (i > 0) {
D(i-1);
x += h; y += h; plot();
A(i-1);
y += 2*h; plot();
C(i-1);
x -= h; y += h; plot();
D(i-1);
}
}
void ABCD() {
h = h0/4;
x0 = 2*h;
y0 = 3*h;
for (int i=1; i <= n; i++) {
if (i <= nOmit)
isOne = true;
else
isOne = false;
x0 -= h;
h /= 2;
y0 += h;
x = x0;
y = y0;
isFirst = true;
A(i);
x += h; y -= h; plot();
B(i);
x -= h; y -= h; plot();
C(i);
x -= h; y += h; plot();
D(i);
x += h; y += h; plot();
}
}