/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; & CC Bordeianu, Univ Bucharest, 2007 Support by National Science Foundation */ // fern.c: Create fractal, fern-like pattern #include #include // if you don't have drand48 uncomment the following two lines #define drand48 1.0/RAND_MAX*rand #define srand48 srand #define max 30000 // number of iterations #define seed 68111 // seed for number generator main() { int i; double x, y, xn, yn, r; FILE *output; // save data in fern.dat output = fopen("fern.dat","w"); // seed number generator srand48(seed); // starting point x = 0.5; y = 0.0; for (i = 1; i<= max; i++) { // iterations r = drand48(); // case 1 if (r <= 0.02) { xn= 0.5; yn= 0.27*y; } // case 2 else if ((r>0.02) && (r<= 0.17)) { xn= -0.139*x + 0.263*y + 0.57; yn= 0.246*x + 0.224*y - 0.036; } // case 3 else if ((r>0.17) && (r<= 0.3)) { xn= 0.17*x - 0.215*y + 0.408; yn= 0.222*x + 0.176*y + 0.0893; } // case 4 else { xn= 0.781*x + 0.034*y + 0.1075; yn= -0.032*x + 0.739*y + 0.27; } fprintf(output, "%f %f\n", x, y); x = xn; y = yn; } printf("data stored in fern.dat\n"); fclose(output); }