-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxuehua.c
61 lines (53 loc) · 1.47 KB
/
xuehua.c
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
作者:Milo Yip
链接:https://zhuanlan.zhihu.com/p/24688522
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char* image;
static int x, y, w, h;
static void Put(int i, int j, char c) {
char *p = image + j * w + i;
if (*p == ' ' || *p == '_') // _ can be overridden by / or \.
*p = c;
}
static void KochCurve(int n, int dir) {
if (n == 0)
switch (dir % 6) {
case 0: Put(x++, y , '_' );
Put(x++, y, '_' ); break;
case 1: Put(x++, y--, '/' ); break;
case 2: Put(--x, y--, '\\'); break;
case 3: Put(--x, y, '_' );
Put(--x, y, '_' ); break;
case 4: Put(--x, ++y, '/' ); break;
case 5: Put(x++, ++y, '\\'); break;
}
else {
KochCurve(n - 1, dir );
KochCurve(n - 1, dir + 1);
KochCurve(n - 1, dir + 5);
KochCurve(n - 1, dir );
}
}
int main() {
w = 2;
h = 1;
for (int n = 0; n < 4; n++) {
image = (char*)malloc(w * h);
memset(image, ' ', w * h);
x = 0;
y = h - 1;
KochCurve(n, 0);
printf("\nn=%d\n\n", n);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++)
putchar(image[j * w + i]);
putchar('\n');
}
w *= 3;
h = n == 0 ? 1 : h * 3;
free(image);
}
}