Skip to content

Instantly share code, notes, and snippets.

@bhugueney
Created January 2, 2018 21:27
Show Gist options
  • Save bhugueney/affd277baac3eb8f8ae8801d54eee285 to your computer and use it in GitHub Desktop.
Save bhugueney/affd277baac3eb8f8ae8801d54eee285 to your computer and use it in GitHub Desktop.
début de réimplémentation du chaos game en C++. Sans paramétrage sur le type numérique : double
#include <iostream>
#include <tuple>
#include <cmath>
#include <cstdlib>
#include <vector>
/*
STEPS=100;
PREFIX=chaos-game-frame-${STEPS}
for i in $(seq $((3 * $STEPS)) $((8 * $STEPS))); do
./chaos-game-double $(bc -l <<< "scale=2;$i / $STEPS") | convert pgm:- ${PREFIX}-$i.png;
done
ffmpeg -start_number 300 -i ${PREFIX}-%03d.png -filter_complex "[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]" -map "[v]" \
chaos-game.mp4
*/
typedef std::tuple<double, double> point_t;
point_t midpoint(point_t const& p0, point_t const& p1){
// TODO
}
point_t rotate( point_t const& center, float a, point_t const& p){
double const x= std::get<0>(p)-std::get<0>(center);
double const y= std::get<1>(p)-std::get<1>(center);
return point_t(std::get<0>(center)+std::cos(a)*x - std::sin(a)*y
, std::get<1>(center)+std::sin(a)*x + std::cos(a)*y);
}
const double pi= std::atan(1)*4;
std::vector<point_t> polygon(point_t const& center, point_t p, float n){
std::vector<point_t> res;
for(int i= 0; i < std::ceil(n); ++i){
res.push_back(p);
p= rotate(center, 2*pi/n, p);
}
return res;
}
// TODO implementer brighten
std::vector<std::vector<int> > create_screen(int init, std::size_t w, std::size_t h){
std::vector<std::vector<int> > screen(h, std::vector<int>(w, init));
return screen;
}
template<typename C> std::ostream& operator<<(std::ostream& os, std::vector<std::vector<C> > const& screen){
for(auto row : screen){
for(auto c : row){
os << c << ' ';
}
}
return os;
}
int main(int argc, char* argv[]){
float n_edges= argc > 1 ? std::atof(argv[1]) : 3.f;
int n_e = static_cast<int>(n_edges);
int const w= 1024;
int const h= 1024;
int const white= 255;
int const black= 0;
std::size_t const n= (argc > 2 ? std::atol(argv[2]) : 10000000)*n_edges;
auto poly= polygon(point_t(w/2, h/2), point_t(w/2, 1.75*h/2), n_edges);
auto sc= create_screen(black, w, h);
std::size_t prev_idx= 0;
point_t p= poly[0];
for(std::size_t i=0; i != n; ++i){
std::size_t idx=0;
do{
idx= std::rand()%poly.size();
}while(idx == prev_idx);
p= midpoint(poly[idx], p);
prev_idx= idx;
brighten(sc, p, white);
}
std::cout << "P2" << std::endl << w << ' ' << h << std::endl << white << std::endl;
std::cout<< sc << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment