/* 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 */ // sierpin.c: Creates Sierpinsky gasket fractal // Plot data without connecting datapoints with lines. #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 #define a1 20.0 // vertex 1 #define b1 20.0 #define a2 320.0 // vertex 2 #define b2 20.0 #define a3 170.0 // vertex 3 #define b3 280.0 main() { int i; double x, y, r; FILE *output; // save data in sierpin.dat output= fopen("sierpin.dat","w"); x = 180.; // starting point y = 150.; srand48(seed); // seed number generator for (i = 1 ; i<= max ; i++) { // draw the gasket r = drand48(); if (r <= 0.3333) { x = 0.5*(x + a1); y = 0.5*(y + b1); } else if (r > 0.3333 && r <= 0.6666) { x = 0.5*(x + a2); y = 0.5*(y + b2); } else { x = 0.5*(x + a3); y = 0.5*(y + b3); } fprintf(output, "%f %f\n", x, y); } printf("data stored in sierpin.dat\n"); fclose(output); }