Skip to content

Instantly share code, notes, and snippets.

@foota
Created May 13, 2012 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foota/2689074 to your computer and use it in GitHub Desktop.
Save foota/2689074 to your computer and use it in GitHub Desktop.
Tetration fractal (C++)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#ifdef _MSC_VER
inline double get_time()
{
return static_cast<double>(std::clock()) / CLOCKS_PER_SEC;
}
#else
#include <sys/time.h>
inline double get_time()
{
timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + 1e-6 * tv.tv_usec;
}
#endif
using namespace std;
const double PI = 3.1415926535897931;
// left, top, width , height, screen width, screen height, max iterations, data[sw*sh]
void tetration(float l, float t, float w, float h, int sw, int sh, int mit, vector<int>& data)
{
for (int ky = 0; ky < sh; ky++) {
for (int kx = 0; kx < sw; kx++) {
float x = l + w * kx / (sw - 1);
float y = t + h * ky / (sh - 1);
int i = 0;
for (; i < mit - 1; i++) {
float e = exp(-0.5 * PI * y);
if (e != e) break;
float p = PI * x * 0.5;
x = e * cos(p);
y = e * sin(p);
if (x * x + y * y > 1e12) break;
}
data.push_back(i);
}
}
}
int main(int argc, char* argv[])
{
if (argc < 6) {
cerr << "Usage: " << argv[0] << " outfile left top width height [screen_width=512 screen_height=512 max_iters=256]" << endl;
cerr << " Ex. " << argv[0] << " tetration.out -1.5 0.0 0.75 0.75" << endl;
exit(1);
}
fstream fs(argv[1], ios_base::out);
float l = atof(argv[2]);
float t = atof(argv[3]);
float w = atof(argv[4]);
float h = atof(argv[5]);
int sw = 512;
int sh = 512;
int mit = 256;
if (argc >= 7) sw = atoi(argv[6]);
if (argc >= 8) sh = atoi(argv[7]);
if (argc >= 9) mit = atoi(argv[8]);
vector<int> data;
cout << "Range: (" << l << ", " << t << ") - (" << l + w << ", " << t + h << ")" << endl;
cout << "Image: " << sw << " x " << sh << endl;
cout << "Max number of iterations: " << mit << endl;
double start_time = get_time();
tetration(l, t, w, h, sw, sh, mit, data);
cout << "Time: " << get_time() - start_time << endl;
int cnt = 0;
fs << sw << " " << sh << endl;
for (vector<int>::iterator p = data.begin(); p != data.end(); ++p, cnt++) {
fs << *p << " ";
if ((cnt + 1) % sw == 0) fs << endl;
}
return 0;
}
@foota
Copy link
Author

foota commented May 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment