-LIR-

FractalEdi

May 30th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <graphics.h>
  4.  
  5. using namespace std;
  6.  
  7. float x,y;
  8. float pi = 3.1415;
  9. int angle;
  10. int WIDTH = 900;
  11. int HEIGHT = 600;
  12.  
  13. /// mareste unghiul la care este desenata o linie din fractal cu "alpha" de grade
  14. void Plus(int alpha)
  15. {
  16.     angle += alpha;
  17.     if( angle >= 360 )
  18.         angle = angle - 360;
  19. }
  20.  
  21.  
  22. /// micsoreasa unghiul la care este desenata o linie din fractal cu "alpha" de grade
  23. void Minus(int alpha)
  24. {
  25.     angle -= alpha;
  26.     if( angle < 0 )
  27.         angle = angle + 360;
  28. }
  29.  
  30.  
  31. /// deseneaza o linie din fractal la unghiul "angle"
  32. void DrawLine(float length)
  33. {
  34.     float new_x,new_y;
  35.     float angle_radians = 2*3.141592*angle/360;
  36.     new_x = x + length*cos(angle_radians) ;
  37.     new_y = y - length*sin(angle_radians);
  38.  
  39.     line(x,y,new_x,new_y);
  40.  
  41.     x = new_x;
  42.     y = new_y;
  43. }
  44.  
  45. /// functiile recursive care deseneaza fractalul
  46. void X(float length, int order);
  47. void Y(float length, int order);
  48.  
  49. void X(float length, int order)
  50. {
  51.     if( order >= 1 )
  52.     {
  53.         X(length,order-1);
  54.         Plus(90);
  55.         Y(length,order-1);
  56.         DrawLine(length);
  57.         Plus(90);
  58.     }
  59. }
  60.  
  61. void Y(float length, int order)
  62. {
  63.     if( order >= 1 )
  64.     {
  65.         Minus(90);
  66.         DrawLine(length);
  67.         X(length,order-1);
  68.         Minus(90);
  69.         Y(length,order-1);
  70.     }
  71. }
  72.  
  73. int main()
  74. {
  75.     initwindow(WIDTH,HEIGHT);
  76.  
  77.     for (int order = 1; order <= 12; order++)
  78.     {
  79.         x = 2*(WIDTH-20)/3;
  80.         y = 2*HEIGHT/3;
  81.         angle = 0;
  82.  
  83.         cleardevice();
  84.         DrawLine(7);
  85.         X(7,order);
  86.         outtextxy(150, 30, "Press any key...");
  87.         getch();
  88.     }
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment