-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgalaxies.cpp
243 lines (198 loc) · 9.16 KB
/
galaxies.cpp
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
234
235
236
237
238
239
240
241
242
243
#define WINDOW
#define OUTPUT
#include "aural.hpp"
#include <iostream>
using namespace au;
using namespace au::app;
using namespace au::math;
using namespace au::rnd;
using namespace au::flame;
double ssin(double x, double p) {
if(p == 0) return asin(sin(x));
else return pow(sin(x), p);
}
double ccos(double x, double p) {
if(p == 0) return acos(sin(x));
else return pow(cos(x), p);
}
class baseApp : public App {
using App::App;
public:
double a[7], f[7], v, p, q, x = 0, y = 0, t = 0;
sf::Color colors[3];
double minx = 10, miny = 10, maxx = -10, maxy = -10, color_limit = 1;
bool colored = 0;
struct attractor {
double a[7], f[7], v, p, q, x = 0, y = 0, t = 0;
double centerx, centery, radius;
sf::Color colors[3];
double minx = 10, miny = 10, maxx = -10, maxy = -10, color_limit = 1;
void create() {
for(int i = 1; i <= 6; i++)
a[i] = random(0.7, 1.2) * (random(0, 100) > 50 ? -1: 1),
f[i] = random(0.7, 1.2) * (random(0, 100) > 50 ? -1: 1);
v = random(0.001, 0.5);
p = random(1, 5);
q = random(1, 5);
}
void init(bool colored) {
for(double x = -1; x <= 1; x += 0.02)
for(double y = -1; y <= 1; y += 0.02) {
double xx = x*sqrt(1-y*y/2);
double yy = y*sqrt(1-x*x/2);
sf::Color color1 = interpolate(colors[0], colors[1], map(x, -1, 1, 0, 1));
sf::Color color2 = interpolate(colors[0], colors[1], map(y, -1, 1, 0, 1));
sf::Color color3 = interpolate(color1, color2, 0.5);
color3.a = 80 * map(radius, 80, 800, 0.2, 1.2);
vec f = vec(xx, yy) - vec(0, 0);
color3.a *= f.mag2();
rect(xx*radius+centerx, yy*radius+centery, 2, 2, color3/*(colored ? sf::Color(255, 255, 255, 60) : sf::Color(0, 0, 0, 60))*/);
}
}
void iterate(int screen_width, int screen_height, bool colored) {
for(int i = 1; i <= 10000*map(radius, 80, 800, 0.2, 1); i++) {
double xx = x, yy = y;
xx = a[1]*ssin(f[1]*x, p) + a[2]*ccos(f[2]*y, q) + a[3]*ssin(f[3]*t, p);
yy = a[4]*ccos(f[4]*x, q) + a[5]*ssin(f[5]*y, p) + a[6]*ssin(f[6]*t, q);
t += v;
vec step(xx-x, yy-y);
step = step*10;
double tt = constrain(map(step.mag2(), 0, M_PI*M_PI*2, 0, 1), -color_limit, color_limit);
sf::Color color = interpolate(colors[0], colors[1], tt);
color.a = 10 * gaussian(1, 0.1);
//std::cout << step.mag2()*pow(10, 5) << ' ' << tt << "\n\n";
x = xx;
y = yy;
minx = std::min(minx, x);
miny = std::min(miny, y);
maxx = std::max(maxx, x);
maxy = std::max(maxy, y);
xx = map(x, minx, maxx, -1, 1);
yy = map(y, miny, maxy, -1, 1);
//xx -= screen_width/2;
//yy -= screen_height/2;
double xxx = xx * sqrt(1-(yy*yy)/2);//+screen_width/2;
double yyy = yy * sqrt(1-(xx*xx)/2);//+screen_height/2;
xxx *= radius;
yyy *= radius;
xxx += centerx;
yyy += centery;
vec f = vec(xxx, yyy) - vec(centerx, centery);
color.a *= map(f.mag(), 0, radius, 0, 1);
if(i > 2000) {
if(colored) rect(xxx, yyy, 1, 1, color, sf::BlendAdd);
else rect(xxx, yyy, 1, 1, color);
}
}
}
attractor(double cx, double cy, double r) : centerx(cx), centery(cy), radius(r) {}
attractor() {
}
};
std::vector<attractor> attractors;
int attractor_number = 10, timer = 0;
attractor bg;
virtual void init() {
#ifdef WINDOW
initWindow();
#endif
texture.create(screen_width, screen_height);
texture.setSmooth(true);
clock.restart();
seed = std::chrono::system_clock::now().time_since_epoch().count();
//seed = 1545768988279824;
}
void setup() {
#ifdef WINDOW
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
window.create(sf::VideoMode(screen_width, screen_height), window_name, sf::Style::Close, settings);
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
#endif
timeLimit = 30;
save_path = "./";
std::cout << seed << '\n';
/*for(int i = 1; i <= 6; i++)
std::cout << "a" << i << " = " << a[i] << '\n';
for(int i = 1; i <= 6; i++)
std::cout << "f" << i << " = " << f[i] << '\n';
std::cout << "v = " << v << "\np = " << p << "\nq = " << q << '\n';*/
double sum_angle = 0;
for(int i = 1; i <= attractor_number; i++) {
double radius = 600 * map(i, 1, attractor_number, 1, 0.125);//(random(0, 100) < 50 ? random(500.0, 800.0) : random(60.0, 250.0));
double angle = fmod(sum_angle + random(M_PI*0.8, M_PI*1.2), 2*M_PI);
if(sum_angle) {
sum_angle += angle;
sum_angle /= 2;
} else {
sum_angle += angle;
}
double center_radius = constrain(gaussian(800, 400), 0, 1000);
double centerx = cos(angle)*center_radius+screen_width/2;
double centery = sin(angle)*center_radius+screen_height/2;
attractors.push_back(attractor(centerx, centery, radius));
attractors.back().create();
}
bg = attractor(screen_width/2, screen_height/2, 1200);
bg.create();
int hue = random(0, 360);
color_limit = random(1.0, 7.0);
if(random(0, 100) < 70) {
colored = 1;
sf::Vertex background[] = {
sf::Vertex(sf::Vector2f(0, 0), convert(Hsv((hue+random(100, 260))%360, 0.4, 1.4*random(0.03, 0.04)))),
sf::Vertex(sf::Vector2f(screen_width, 0), convert(Hsv((hue+random(100, 260))%360, 0.4, 1.4*random(0.03, 0.04)))),
sf::Vertex(sf::Vector2f(screen_width, screen_height), convert(Hsv((hue+random(100, 260))%360, 0.4, 1.4*random(0.03, 0.04)))),
sf::Vertex(sf::Vector2f(0, screen_height), convert(Hsv((hue+random(100, 260))%360, 0.4, 1.4*random(0.03, 0.04))))
};
texture.draw(background, 4, sf::Quads);
for(int i = 0; i < attractor_number; i++)
attractors[i].colors[0] = convert(Hsv((hue+random(0, 20))%360, 0.5, 0.8)),
attractors[i].colors[1] = convert(Hsv((hue+random(100, 260))%360, 0.5, 0.8)),
attractors[i].colors[0].a = 10,
attractors[i].colors[1].a = 10;
bg.colors[0] = convert(Hsv((hue+random(0, 20))%360, 0.5, 0.6)),
bg.colors[1] = convert(Hsv((hue+random(100, 260))%360, 0.5, 0.6));
} else {
texture.clear(sf::Color(200, 200, 200));
for(int i = 0; i < attractor_number; i++)
attractors[i].colors[0] = convert(Hsv((hue+random(0, 20))%360, 0.4, 0.2)),
attractors[i].colors[1] = convert(Hsv((hue+random(100, 260))%360, 0.4, 0.2)),
attractors[i].colors[0].a = 10,
attractors[i].colors[1].a = 10;
bg.colors[0] = convert(Hsv((hue+random(0, 20))%360, 0.4, 0.1)),
bg.colors[1] = convert(Hsv((hue+random(100, 260))%360, 0.4, 0.1));
bg.colors[0].a = 10;
bg.colors[1].a = 10;
}
for(auto &a : attractors)
a.init(colored);
//std::cout << seed << '\n';
}
void loop() {
#ifdef WINDOW
checkForEvents();
#endif
//std::cout << x << ' ' << y << '\n';
for(auto &a : attractors)
a.iterate(screen_width, screen_height, colored);
if(timer++ < 50) bg.iterate(screen_width, screen_height, colored);
#ifdef WINDOW
drawTextureToWindow();
#endif
}
void close() {
#ifdef WINDOW
if(window.isOpen()) window.close();
#endif
#ifdef OUTPUT
std::string info = saveFile() + "#method=fujii attractor";
info+="#seed="+std::to_string(seed);
std::cout << info;
#else
saveFile();
#endif
}
} testApp(2048, 2048);
AURAL_APP(testApp)