#include #include #include #define SIN 0.8660254 typedef struct{ double x, y; } coordinate; // Draws the computation void draw_koch_snowflake(coordinate a, coordinate b, int t, FILE *f){ if(!t){ fprintf(f, "M%f %f L%f %F ", a.x, a.y, b.x, b.y); return; } coordinate tmp = { (b.x - a.x)/3, (b.y - a.y)/3}; coordinate c = {a.x + tmp.x, a.y + tmp.y}; coordinate d = {a.x + (1.5 * tmp.x - SIN * tmp.y), a.y + (1.5 * tmp.y + SIN * tmp.x)}; coordinate e = {a.x + 2 * tmp.x, a.y + 2 * tmp.y}; draw_koch_snowflake(a, c, t-1, f); draw_koch_snowflake(c, d, t-1, f); draw_koch_snowflake(d, e, t-1, f); draw_koch_snowflake(e, b, t-1, f); } // Provides a frame for the path void framework(coordinate a, coordinate b, int t, FILE *f){ fprintf(f, ""); } int main(int argc, char *argv[]){ int w, h, r; w = h = 512; r = w / 3; int n = atoi(argv[1]); FILE *f; if( !(f=fopen("koch.svg","w")) ) return -1; fprintf(f,"", w, h); coordinate a = {w / 2, h / 2 + r}; coordinate b = {w / 2 - r * SIN, h / 2 - r * 0.5}; coordinate c = {w / 2 + r * SIN, h / 2 - r * 0.5}; framework(b, a, n, f); framework(c, b, n, f); framework(a, c, n, f); fprintf(f, ""); fclose(f); return 0; }