-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
184 lines (156 loc) · 4.12 KB
/
Copy pathtest.c
File metadata and controls
184 lines (156 loc) · 4.12 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define WIDTH 2048
#define HEIGHT 2048
#define MAX_LEN 1000000
#define STACK_SIZE 100000
#define MAX_DEPTH 10000
typedef struct {
double x, y;
double angle;
int depth;
} turtle_t;
typedef struct {
unsigned char r, g, b;
} pixel_t;
pixel_t image[HEIGHT][WIDTH];
// TODO: use pixel_t instead of individual rgb components!
// also add alpha and later 4*float
void set_pixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) {
if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT) {
image[y][x].r = r;
image[y][x].g = g;
image[y][x].b = b;
}
}
void get_branch_color(int depth, int max_depth, unsigned char * r, unsigned char * g, unsigned char * b) {
double t = max_depth > 0 ? (double)depth / max_depth : 0;
double it = 1.0 - t;
*r = (unsigned char)(it * 139 + t * 0);
*g = (unsigned char)(it * 69 + t * 128);
*b = (unsigned char)(it * 19 + t * 0);
}
void draw_line(double x0, double y0, double x1, double y1, int depth, int max_depth) {
int steps = (int)hypot(x1 - x0, y1 - y0);
unsigned char r, g, b;
get_branch_color(depth, max_depth, &r, &g, &b);
for (int i = 0; i <= steps; ++i) {
double t = (double)i / steps;
int x = (int)(x0 + t * (x1 - x0));
int y = (int)(y0 + t * (y1 - y0));
set_pixel(x, y, r, g, b);
}
}
void draw_dot(int cx, int cy, int radius, unsigned char r, unsigned char g, unsigned char b) {
int radius2 = radius * radius;
for(int dy = -radius; dy <= radius; ++dy) {
for(int dx = -radius; dx <= radius; ++dx) {
int x = cx + dx;
int y = cy + dy;
if(dx*dx + dy*dy <= radius2) {
set_pixel(x, y, r, g, b);
}
}
}
}
char* apply_rules(const char *input, char * rule) {
char *output = malloc(MAX_LEN);
output[0] = '\0';
for (size_t i = 0; input[i]; ++i) {
if (input[i] == 'F') {
strcat(output, rule); // "FF+[+F-X-F-F]-[-F+F+F+X]");
} else {
strncat(output, &input[i], 1);
}
}
return output;
}
void save_ppm(const char *filename) {
FILE *f = fopen(filename, "wb");
fprintf(f, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x)
fwrite(&image[y][x], 1, 3, f);
fclose(f);
}
void interpret(const char *commands, double angle_deg, double step_len, int max_depth) {
// Start at bottom center, pointing up
turtle_t turtle = { WIDTH / 2, HEIGHT / 2, -90, 0};
turtle_t stack[STACK_SIZE]; // TODO: use malloc instead
int sp = 0;
for (size_t i = 0; commands[i]; ++i) {
char c = commands[i];
switch (c) {
case 'F': {
double rad = turtle.angle * M_PI / 180.0;
double nx = turtle.x + cos(rad) * step_len;
double ny = turtle.y + sin(rad) * step_len;
draw_line(turtle.x, turtle.y, nx, ny, turtle.depth, max_depth);
turtle.x = nx;
turtle.y = ny;
break;
}
case 'X': {
int x = rand() % 10;
if(x > 5) {
draw_dot(turtle.x, turtle.y, 1, 255, 0, 0);
} else {
draw_dot(turtle.x, turtle.y, 1, 0, 0, 255);
}
}
case '+':
turtle.angle += angle_deg;
break;
case '-':
turtle.angle -= angle_deg;
break;
case '[':
if (sp < STACK_SIZE)
stack[sp++] = turtle;
turtle.depth++;
break;
case ']':
if (sp > 0)
turtle = stack[--sp];
break;
}
}
}
int main(int argc, char ** argv) {
if(argc < 2) {
printf("Give a ruleset to the program!\n");
printf("Rules: F-+[]X\n");
}
char rule_buf[1024] = { 0 };
memcpy(rule_buf, argv[1], strlen(argv[1]));
char path[8192] = { 0 };
// TODO: make option
double degree = 1;
double step = 1;
double degree_inc = 0.3612;
double step_inc = 0.075;
int index = 0;
for(index = 0; index < 120; index++) {
printf("L-System image %i\n", index);
memset(image, 255, sizeof(image));
char *state = strdup("F");
int iterations = 4;
for (int i = 0; i < iterations; ++i) {
char *next = apply_rules(state, rule_buf);
free(state);
state = next;
}
interpret(state, (double)degree, (double)step, iterations * 2);
snprintf(path, 8191,
"output/A%f_S%f_RS_%s_%i.ppm",
degree, step, rule_buf, index);
// save_ppm("tree.ppm");
save_ppm(path);
free(state);
degree += degree_inc;
step += step_inc;
}
return 0;
}