/* fern.cc code to draw the fractal "barnsley's fern." example of iterated affine transformations */ #include #include #include using std::endl; using std::cout; using std::ofstream; int main() { int i; double x, y, xn, yn, r; // setup file i/o: ofstream out_file; const char out_file_name[] = "fern.dat"; // save data in fern.dat out_file.open(out_file_name); x = 0.5; // starting point y = 0.0; const int max = 50000; // number of iterations srand((time(0))); // seed random nmbr generator. // srand & time built-in for(i=1; i<=max; ++i) // iterations { r = rand()/(double)RAND_MAX; // scale random nmbr if (r <= 0.02) // stem { xn = 0.5; yn = 0.27*y; } else if((r>0.02) && (r<=0.17)) // right leaf { xn = -0.139*x + 0.263*y + 0.57; yn = 0.246*x + 0.224*y - 0.036; } else if ((r>0.17) && (r<=0.3)) // left leaf { xn = 0.17*x - 0.215*y + 0.408; yn = 0.222*x + 0.176*y + 0.0893; } else // shoot { xn = 0.781*x + 0.034*y + 0.1075; yn = -0.032*x + 0.739*y + 0.27; } x=xn; y=yn; out_file << x << " " << y << endl; // write date to output file } out_file.close(); cout << "data stored in fern.dat" << endl; return 0; }