-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
233 lines (197 loc) · 6.42 KB
/
tree.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// Created by Gabriel Mitterrutzner on 2019-02-12.
//
#include <SDL2/SDL.h>
#include <stdbool.h>
#include <SDL2/SDL_ttf.h>
#include <string.h>
#include <stdlib.h>
#define SCREEN_W 640
#define SCREEN_H 380
#define SCREEN_SCALE 1
#define SCREEN_NAME "Fractal tree"
void game_init(void);
void game_quit(void);
struct {
SDL_bool running;
struct {
unsigned int w;
unsigned int h;
const char *name;
SDL_Window *window;
SDL_Renderer *renderer;
} screen;
void (*init)(void);
void (*quit)(void);
} Game = {
SDL_FALSE,
{
SCREEN_SCALE * SCREEN_W,
SCREEN_SCALE * SCREEN_H,
SCREEN_NAME,
NULL,
NULL
},
game_init,
game_quit
};
#define ANGLE 20
#define LENGTH_DEVIDE 1.125
#define FRACTAL_DEPTH 10
#define LINE_LENGTH 45
void game_init(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
printf("Error %s", SDL_GetError());
exit(-1);
}
Game.screen.window = SDL_CreateWindow(Game.screen.name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
Game.screen.w,
Game.screen.h, SDL_WINDOW_SHOWN);
if (Game.screen.window == NULL) {
printf("Error %s", SDL_GetError());
exit(-2);
}
Game.screen.renderer = SDL_CreateRenderer(Game.screen.window, -1, SDL_RENDERER_ACCELERATED);
if (Game.screen.renderer == NULL) {
printf("Error %s", SDL_GetError());
SDL_DestroyWindow(Game.screen.window);
exit(-3);
}
Game.running = SDL_TRUE;
}
void game_quit(void) {
SDL_DestroyRenderer(Game.screen.renderer);
SDL_DestroyWindow(Game.screen.window);
Game.screen.renderer = NULL;
Game.screen.window = NULL;
SDL_Quit();
}
void hsv_to_rgb(float *r, float *g, float *b, float h, float s, float v) {
int i;
float f, p, q, t;
if (s == 0) {
*r = *g = *b = v;
return;
}
h /= 60;
i = (int) floorf(h);
f = h - i;
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch (i) {
case 0:
*r = v * 255;
*g = t * 255;
*b = p * 255;
break;
case 1:
*r = q * 255;
*g = v * 255;
*b = p * 255;
break;
case 2:
*r = p * 255;
*g = v * 255;
*b = t * 255;
break;
case 3:
*r = p * 255;
*g = q * 255;
*b = v * 255;
break;
case 4:
*r = t * 255;
*g = p * 255;
*b = v * 255;
break;
default:
*r = v * 255;
*g = p * 255;
*b = q * 255;
break;
}
}
double deg_to_rad(double angle) {
return angle * (M_PI / 180);
}
int map(int x, int in_min, int in_max, int out_min, int out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void set_color(int color) {
float r, g, b;
hsv_to_rgb(&r, &g, &b, color, 1, 1);
SDL_SetRenderDrawColor(Game.screen.renderer, (uint8_t) floorf(r), (uint8_t) floorf(g), (uint8_t) floorf(b), 255);
}
SDL_Point create_point(int x, int y) {
SDL_Point new_point = {x, y};
return new_point;
}
SDL_Point get_new_vector(double cur_angle, int length, SDL_Point start_point) {
SDL_Point new_vect;
if (cur_angle <= 90) {
new_vect = create_point((int) (start_point.x - length * cos(deg_to_rad(cur_angle))),
(int) (start_point.y - length * sin(deg_to_rad(cur_angle))));
} else if (cur_angle <= 180) {
new_vect = create_point((int) (start_point.x + length * -cos(deg_to_rad(cur_angle))),
(int) (start_point.y - length * sin(deg_to_rad(cur_angle))));
} else if (cur_angle + 90 <= 270) {
new_vect = create_point((int) (start_point.x + length * -cos(deg_to_rad(cur_angle))),
(int) (start_point.y + length * -sin(deg_to_rad(cur_angle))));
} else {
new_vect = create_point((int) (start_point.x - length * cos(deg_to_rad(cur_angle))),
(int) (start_point.y + length * -sin(deg_to_rad(cur_angle))));
}
return new_vect;
}
void branch(SDL_Point start_point, int depth, double last_angle) {
int color = map(depth, 0, FRACTAL_DEPTH, 0, 255);
SDL_Point new_point = create_point(start_point.x, start_point.y - LINE_LENGTH);
if (depth == 0) {
set_color(color);
SDL_RenderDrawLine(Game.screen.renderer, start_point.x, start_point.y, start_point.x,
start_point.y - LINE_LENGTH);
branch(new_point, depth + 1, last_angle);
} else if (depth < FRACTAL_DEPTH) {
int length = LINE_LENGTH;
double cur_angle_plus = last_angle + ANGLE;
double cur_angle_minus = last_angle - ANGLE;
if (pow(LENGTH_DEVIDE, depth) != 0) {
length = (LINE_LENGTH / (int) pow(LENGTH_DEVIDE, depth));
}
SDL_Point left_vect = get_new_vector(cur_angle_plus, length, start_point);
SDL_Point right_vect = get_new_vector(cur_angle_minus, length, start_point);
set_color(color);
SDL_RenderDrawLine(Game.screen.renderer, start_point.x, start_point.y, left_vect.x, left_vect.y);
SDL_RenderDrawLine(Game.screen.renderer, start_point.x, start_point.y, right_vect.x, right_vect.y);
branch(left_vect, depth + 1, cur_angle_plus);
branch(right_vect, depth + 1, cur_angle_minus);
}
}
int main(void) {
Game.init();
SDL_Point start_point = create_point(Game.screen.w / 2, Game.screen.h - 20);
while (Game.running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
Game.running = SDL_FALSE;
else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case 'q':
Game.running = SDL_FALSE;
break;
default :
break;
}
}
}
SDL_RenderClear(Game.screen.renderer);
branch(start_point, 0, 90);
SDL_SetRenderDrawColor(Game.screen.renderer, 0, 0, 0, 255);
SDL_RenderPresent(Game.screen.renderer);
SDL_Delay(16);
}
Game.quit();
return EXIT_SUCCESS;
}