fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct __xy {
  4. double x;
  5. double y;
  6. } XY, *PXY;
  7.  
  8. double root(double d, int q);
  9. void printXY(XY p);
  10. XY addXY(XY p1, XY p2);
  11. XY subXY(XY p1, XY p2);
  12. XY mulXY(XY p, double v);
  13. void koch(XY p1, XY p2, int n);
  14.  
  15. double cos60, sin60, div1p3, div2p3;
  16.  
  17. int main(void) {
  18. XY p1 = { 0.0, 0.0 };
  19. XY p2 = { 100.0, 0.0 };
  20. int n;
  21.  
  22. cos60 = 0.5;
  23. sin60 = root(3.0, 25) / 2.0;
  24. div1p3 = 1.0 / 3.0;
  25. div2p3 = 2.0 / 3.0;
  26.  
  27. scanf("%d", &n);
  28.  
  29. koch(p1, p2, n);
  30. printXY(p2);
  31.  
  32. return 0;
  33. }
  34.  
  35. void printXY(XY p) {
  36. printf("%10.8lf %10.8lf\n", p.x, p.y);
  37. }
  38.  
  39. XY addXY(XY p1, XY p2) {
  40. XY r = p1;
  41. r.x += p2.x;
  42. r.y += p2.y;
  43. return r;
  44. }
  45.  
  46. XY subXY(XY p1, XY p2) {
  47. XY r = p1;
  48. r.x -= p2.x;
  49. r.y -= p2.y;
  50. return r;
  51. }
  52.  
  53. XY mulXY(XY p, double v) {
  54. XY r = p;
  55. r.x *= v;
  56. r.y *= v;
  57. return r;
  58. }
  59.  
  60. double root(double d, int q) {
  61. double p1 = 0;
  62. double p2 = d;
  63. double m, p;
  64.  
  65. do {
  66. m = ((p2 - p1) / 2.0) + p1;
  67. p = m * m;
  68. if (p > d) {
  69. p2 = m;
  70. } else if (p < d) {
  71. p1 = m;
  72. } else {
  73. return m;
  74. }
  75. --q;
  76. } while (q > 0);
  77.  
  78. return m;
  79. }
  80.  
  81. void koch(XY p1, XY p2, int n) {
  82. XY df, p13, p23, t;
  83. if (n > 0) {
  84. --n;
  85. df = subXY(p2, p1);
  86. p13 = addXY(mulXY(df, div1p3), p1);
  87. p23 = addXY(mulXY(df, div2p3), p1);
  88. t.x = df.x * cos60 - df.y * sin60;
  89. t.y = df.x * sin60 + df.y * cos60;
  90. t = addXY(mulXY(t, div1p3), p13);
  91. koch(p1, p13, n);
  92. koch(p13, t, n);
  93. koch(t, p23, n);
  94. koch(p23, p2, n);
  95. } else {
  96. printXY(p1);
  97. }
  98. }
Success #stdin #stdout 0s 2252KB
stdin
2
stdout
0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250481
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250481
33.33333277 19.24500962
44.44444444 19.24500962
50.00000000 28.86751443
55.55555556 19.24500962
66.66666723 19.24500962
61.11111111 9.62250481
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250481
88.88888889 0.00000000
100.00000000 0.00000000