-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgalaxy.c
More file actions
219 lines (184 loc) · 4.91 KB
/
galaxy.c
File metadata and controls
219 lines (184 loc) · 4.91 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
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
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <tgmath.h>
#include <unistd.h>
#include "hsv.c"
SDL_Window* window;
SDL_Renderer* renderer;
// Config
const double radius = 64; // ly
const double star_num = 100; // Probability correlation
const int screen_size = 1024;
// Randomised Values
// =================
double twistval; // rad/ly
double tiltval;
double angle;
double ab; // Major axis / minor axis of stellar orbits
rgb star_color;
// =================
struct Point {
double x;
double y;
};
struct PointPolar {
double r;
double phi;
};
static SDL_Texture *tex_star;
// Random float in [-1.0, 1.0]
double drand() {
return rand() / (double)RAND_MAX * 2.0 - 1.0;
}
void
init() {
srand(getpid());
//Check SDL Version
SDL_version compiled;
SDL_version linked;
SDL_VERSION(&compiled);
SDL_GetVersion(&linked);
if (compiled.major != linked.major) {
fprintf(stderr, "SDL version mismatch! Found version %d, require version %d", linked.major, compiled.major);
exit(EXIT_FAILURE);
}
if(SDL_Init(SDL_INIT_VIDEO)) {
fprintf(stderr, "Initialisation Error: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
if(SDL_CreateWindowAndRenderer(screen_size, screen_size, SDL_WINDOW_BORDERLESS, &window, &renderer)) {
fprintf(stderr, "Window creation failed: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
if(IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG) == 0) {
printf("Failed to load image libraries: %s\n", IMG_GetError());
} else {
tex_star = IMG_LoadTexture(renderer, "./star.png");
if(!tex_star) {
printf("Couldn't load star image: %s\n", IMG_GetError());
} else {
SDL_SetTextureBlendMode(tex_star, SDL_BLENDMODE_ADD);
hsv randcolor;
randcolor.h = fabs(drand()) * 360.0;
randcolor.s = 0.75;
randcolor.v = 1;
star_color = hsv_to_rgb(randcolor);
SDL_SetTextureColorMod(tex_star, star_color.r, star_color.g, star_color.b);
}
}
SDL_SetWindowTitle(window, "Galaxy");
// Settings
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
// Hints
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
twistval = 1.0/256*M_PI * (1 + drand() * 0.1); //+- 10%
ab = 1.2 * (1 + drand() * 0.1);
tiltval = fabs(drand());
angle = 2*M_PI*fabs(drand());
}
void
quit(int exit_code) {
SDL_Quit();
exit(exit_code);
}
void
wait_for_key(SDL_Keycode sym) {
bool wait = true;
SDL_Event event;
while(wait) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_KEYDOWN) {
if(event.key.keysym.sym == sym) {
wait = false;
}
} else if(event.type == SDL_QUIT) {
quit(EXIT_SUCCESS);
}
}
SDL_RenderPresent(renderer);
SDL_Delay(16);
}
}
struct Point
polar_to_cart(struct PointPolar pp) {
struct Point p;
p.x = pp.r*cos(pp.phi);
p.y = pp.r*sin(pp.phi);
return p;
}
struct PointPolar
cart_to_polar(struct Point p) {
struct PointPolar pp;
pp.r = sqrt(p.x*p.x + p.y*p.y);
pp.phi = atan2(p.y, p.x);
return pp;
}
double
pdf(struct PointPolar pp) {
double val = 1/radius * exp(-pp.r/radius);
//printf("Pdf = %.100f\n", val);
return val;
}
struct PointPolar
twist(struct PointPolar pin) {
struct PointPolar pout;
pout.r = pin.r;
pout.phi = pin.phi;
double a = ab*pin.r;
double b = pin.r;
//"Ellipse Number"
double en = a*b/(sqrt(pow(b * cos(pin.phi), 2) + pow(a * sin(pin.phi), 2)));
pout.phi = pin.phi + twistval * en;
return pout;
}
struct Point tilt(struct Point p, double tiltval) {
p.y *= tiltval;
return p;
}
struct Point rotate(struct Point p, double rads) {
struct PointPolar pp = cart_to_polar(p);
pp.phi += rads;
return polar_to_cart(pp);
}
void draw_star(SDL_Renderer *renderer, int x, int y) {
if(tex_star) {
SDL_RenderCopy(renderer, tex_star, NULL, &(SDL_Rect){x - 1, y - 1, 3, 3});
} else {
SDL_RenderDrawPoint(renderer, x, y);
}
//SDL_RenderFillRect(renderer, &(SDL_Rect){x-1, y-1, 3, 3});
}
int
main(int argc, char **argv) {
init();
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
int star_count = 0;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 1);
for(double y = -screen_size/2; y < screen_size/2; y++) {
for(double x = -screen_size/2; x < screen_size/2; x++) {
double randval = (rand() / (double)RAND_MAX) / star_num;
struct PointPolar pp = cart_to_polar((struct Point){x, y});
if(pdf(pp) > randval) {
struct Point p = rotate(tilt(polar_to_cart(twist(pp)), tiltval), angle);
draw_star(renderer, p.x + screen_size/2, p.y + screen_size/2);
star_count++;
}
////Alternative: Set brightness (smooth galaxy), factor is no good
//int i = 1e6*pdf(pp);
//SDL_SetRenderDrawColor(renderer, I,I,I,255);
//struct Point p = polar_to_cart(twist(pp));
//SDL_RenderDrawPoint(renderer, p.x + screen_size/2, p.y + screen_size/2);
}
}
SDL_RenderPresent(renderer);
printf("%d stars\n", star_count);
wait_for_key(SDLK_ESCAPE);
return 0;
}