Advertisement
Shailrshah

Koch Curve

Oct 28th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <graphics.h>
  4.  
  5. double factor;
  6.  
  7. void koch( int level, int scale, int rot )
  8. {
  9.     if(level > 0){
  10.         koch(level-1, scale/3, rot);
  11.         koch(level-1, scale/3, (rot+1)%6);
  12.         koch(level-1, scale/3, (rot+5)%6);
  13.         koch(level-1, scale/3, rot);
  14.     }else{
  15.         switch (rot){
  16.             case 0: linerel(scale, 0); break;
  17.             case 1: linerel(scale/2, scale*factor); break;
  18.             case 2: linerel(-scale/2, scale*factor); break;
  19.             case 3: linerel(-scale, 0); break;
  20.             case 4: linerel(-scale/ 2, -scale*factor); break;
  21.             case 5: linerel(scale/2, -scale*factor); break;
  22.         }
  23.     }
  24. }
  25.  
  26. void startGraphics(){
  27.     int gd=DETECT, gm;
  28.     initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
  29. }
  30.  
  31. void main(){
  32.     int n, i;
  33.     factor = sqrt(3)/-2;
  34.     printf("Enter the level: ");
  35.     scanf("%d", &n);
  36.     startGraphics();
  37.     for(i = 0; i <= n; i++){
  38.         clearviewport();
  39.         moveto(0, getmaxy()-50);
  40.         koch(i, getmaxx(), 0);
  41.         delay(1000);
  42.     }
  43.     getch();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement