/* Koch curve * E.Somfai, CO904 class, 2008-2011. */ #include #include #define MAX_ITER 6 #define C cos(M_PI/3.) #define S sin(M_PI/3.) void plot(double x1, double y1, double x2, double y2, int iter); void plot(double x1, double y1, double x2, double y2, int iter) { if (iter == 0) { printf("%lf %lf\n", x1, y1); } else { double dx = (x2-x1)/3; // 1/3 vector double dy = (y2-y1)/3; double xx = C * dx - S * dy; // rotated 1/3 vector double yy = S * dx + C * dy; plot(x1, y1, x1+dx, y1+dy, iter-1); plot(x1+dx, y1+dy, x1+dx+xx, y1+dy+yy, iter-1); plot(x1+dx+xx, y1+dy+yy, x1+2*dx, y1+2*dy, iter-1); plot(x1+2*dx, y1+2*dy, x2, y2, iter-1); } } int main() { plot(0., 0., 1., 0., MAX_ITER); plot(1., 0., C, -S, MAX_ITER); // two more starting lines plot(C, -S, 0., 0., MAX_ITER); // to complete the init. triangle return 0; }