Last active
January 24, 2016 07:00
-
-
Save razimantv/b0dd615ea187057d45cc to your computer and use it in GitHub Desktop.
Generate a wallpaper from random non-intersecting circles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <algorithm> | |
#include <iostream> | |
#include <tuple> | |
#include <vector> | |
using namespace std; | |
random_device rd; | |
mt19937_64 gen(rd()); | |
uniform_real_distribution<double> dist(0.0, 1.0); | |
uniform_int_distribution<int> rgb(0, 255); | |
vector<tuple<double, double, double>> circles; | |
tuple<double, double, double> getxyr(double xmax, double ymax, double rmax) { | |
double x = dist(gen) * xmax; | |
double y = dist(gen) * ymax; | |
double r = rmax; | |
for (int i = 0; i < circles.size(); ++i) { | |
double cdist = hypot(x - get<0>(circles[i]), y - get<1>(circles[i])); | |
r = min(r, abs(cdist - get<2>(circles[i]))); | |
} | |
r *= (dist(gen) + 1) / 2.5; | |
circles.push_back(make_tuple(x, y, r)); | |
return make_tuple(x, y, r); | |
} | |
string rgbtostring(int r, int g, int b) { | |
string ret = ""; | |
int clr = r * 65536 + g * 256 + b; | |
for (int i = 0; i < 6; ++i, clr >>= 4) { | |
int cur = clr & 15; | |
if (cur < 10) { | |
ret = (char)(cur + '0') + ret; | |
} else { | |
ret = (char)(cur - 10 + 'A') + ret; | |
} | |
} | |
return ret; | |
} | |
int main() { | |
cout << "set size ratio -1\n"; | |
cout << "set xrange[0:16]\n"; | |
cout << "set yrange[0:9]\n"; | |
cout << "set lmargin 0\n"; | |
cout << "set rmargin 0\n"; | |
cout << "set bmargin 0\n"; | |
cout << "set tmargin 0\n"; | |
cout << "unset border\n"; | |
cout << "unset tics\n"; | |
cout << "set style fill transparent solid 0.8 noborder\n"; | |
cout << "set term pngcairo enhanced size 1920,1080\n"; | |
cout << "set output 'circlewallpaper.png'\n"; | |
for (int i = 0; i < 5000; ++i) { | |
double x, y, r; | |
tie(x, y, r) = getxyr(16, 9, 5); | |
string col = rgbtostring(rgb(gen), rgb(gen), rgb(gen)); | |
cout << "set object circle at " << x << "," << y << " size " << r | |
<< " fc rgb '#" << col << "'\n"; | |
} | |
cout << "plot 1/0 notitle\n"; | |
cout << "set output\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment