#include <stdio.h>
typedef struct __xy {
double x;
double y;
} XY, *PXY;
double root(double d, int q);
void printXY(XY p);
XY addXY(XY p1, XY p2);
XY subXY(XY p1, XY p2);
XY mulXY(XY p, double v);
void koch(XY p1, XY p2, int n);
double cos60, sin60, div1p3, div2p3;
int main(void) {
XY p1 = { 0.0, 0.0 };
XY p2 = { 100.0, 0.0 };
int n;
cos60 = 0.5;
sin60 = root(3.0, 25) / 2.0;
div1p3 = 1.0 / 3.0;
div2p3 = 2.0 / 3.0;
koch(p1, p2, n);
printXY(p2);
return 0;
}
void printXY(XY p) {
printf("%10.8lf %10.8lf\n", p.
x, p.
y); }
XY addXY(XY p1, XY p2) {
XY r = p1;
r.x += p2.x;
r.y += p2.y;
return r;
}
XY subXY(XY p1, XY p2) {
XY r = p1;
r.x -= p2.x;
r.y -= p2.y;
return r;
}
XY mulXY(XY p, double v) {
XY r = p;
r.x *= v;
r.y *= v;
return r;
}
double root(double d, int q) {
double p1 = 0;
double p2 = d;
double m, p;
do {
m = ((p2 - p1) / 2.0) + p1;
p = m * m;
if (p > d) {
p2 = m;
} else if (p < d) {
p1 = m;
} else {
return m;
}
--q;
} while (q > 0);
return m;
}
void koch(XY p1, XY p2, int n) {
XY df, p13, p23, t;
if (n > 0) {
--n;
df = subXY(p2, p1);
p13 = addXY(mulXY(df, div1p3), p1);
p23 = addXY(mulXY(df, div2p3), p1);
t.x = df.x * cos60 - df.y * sin60;
t.y = df.x * sin60 + df.y * cos60;
t = addXY(mulXY(t, div1p3), p13);
koch(p1, p13, n);
koch(p13, t, n);
koch(t, p23, n);
koch(p23, p2, n);
} else {
printXY(p1);
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp0eXBlZGVmIHN0cnVjdCBfX3h5IHsKCWRvdWJsZSB4OwoJZG91YmxlIHk7Cn0gWFksICpQWFk7Cgpkb3VibGUgcm9vdChkb3VibGUgZCwgaW50IHEpOwp2b2lkIHByaW50WFkoWFkgcCk7ClhZIGFkZFhZKFhZIHA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