Advertisement
andreisophie

Koch Line

Mar 26th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <graphics.h>
  2. #include <cmath>
  3.  
  4. #define pi 3.141592
  5.  
  6. void koch(int x1, int y1, int l, double u, int n)
  7. {
  8.     int x2=x1+(l/3)*cos(u),y2=y1-(l/3)*sin(u);
  9.     int x3=x2+(l/3)*cos(u+pi/3),y3=y2-(l/3)*sin(u+pi/3);
  10.     int x4=x2+(l/3)*cos(u),y4=y2-(l/3)*sin(u);
  11.     int x5=x4+(l/3)*cos(u),y5=y4-(l/3)*sin(u);
  12.     if (n==0)
  13.         line(x1,y1,x5,y5);
  14.     else
  15.     {
  16.         koch(x1,y1,l/3,u,n-1);
  17.         koch(x2,y2,l/3,u+pi/3,n-1);
  18.         koch(x3,y3,l/3,u-pi/3,n-1);
  19.         koch(x4,y4,l/3,u,n-1);
  20.     }
  21. }
  22.  
  23. int main()
  24. {
  25.     initwindow(1800, 800);
  26.  
  27.     const int x1=25,y1=getmaxy()-100;
  28.  
  29.     setcolor(14);
  30.     koch(x1,y1,getmaxx()-50,0,4);
  31.  
  32.     getch();
  33.     cleardevice();
  34.     closegraph();
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement