// draws sierpinsky gasket fractal #include #include #include #include using std::cout; using std::endl; using std::ofstream; int main() { int i; double x, y, r; // set geometry double side_length = 300.0; // length of triangle side double x1 = 20.0; // x-coordinate of vertex 1 double y1 = x1; // y-coordinate of vertex 1 double x2 = x1 + side_length; // x coord of vertex 2 double y2 = y1; // y coord of vertex 2 double x3 = x1 + (side_length/2.0); // x coord of vertex 3 double y3 = y1 + sqrt(3)*(side_length/2.0); // y coord of vertex 3 // setup file io ofstream out_file; const char out_file_name[] = "sierpin.dat"; // save data in sierpin.dat out_file.open(out_file_name); x = 300.; // starting point y = 30.; const int max = 50000; // number of iterations srand((time(0))); // seed random nmbr generator. srand & time built-in for(i=1 ; i<=max ; ++i) // draw the gasket { r = rand()/(double)RAND_MAX; // scale random nmbr if (r <= 0.3333) { x = 0.5*(x + x1); y = 0.5*(y + y1); } else if(r > 0.3333 && r <= 0.6666) { x = 0.5*(x + x2); y = 0.5*(y + y2); } else { x = 0.5*(x + x3); y = 0.5*(y + y3); } out_file << x << " " << y << endl; } out_file.close(); // close output file std::cout << "data stored in " << out_file_name << std::endl; return 0; }