scorici

kochfract

Oct 12th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. /* Generate koch fractal in SVG format (Scalable vector graphics)
  4.  * http://ecademy.agnesscott.edu/~lriddle/ifs/kcurve/kcurve.htm
  5.  * Run ./a.out > kochfract.svg and look at the file in a browser
  6.  * SVG drawing commands: l=line, m=move, h=horiz. line, v=vert. line
  7.  * uppercase: absolute coordinates, lowercase: relative coordinates
  8.  * e.g. M x y = move to (x, y)
  9.  * l dx dy = line from current point, moving by dx, dy */
  10. void kochfract(double len, double ang) {
  11.     if (len < 30) {
  12.     printf("l %.1f %.1f ", len * cos(ang), -len * sin(ang));
  13. }
  14.     else {
  15.     double l3 = len/3;
  16.     kochfract(l3, ang);
  17.     kochfract(l3, ang+acos(.5));
  18.     kochfract(l3, ang-acos(.5));
  19.     kochfract(l3, ang);
  20. }
  21. }
  22. int main(void)
  23. {
  24.   printf("%s", "<?xml version=\"1.0\"?>\n"  // .svg file header
  25.        "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n"
  26.        "<path fill=\"none\" stroke=\"blue\" d=\"M 0 200 ");
  27.   kochfract(300, 0);
  28.   puts("\"/>\n</svg>");             // end of .svg file
  29.   return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment