SHARE
TWEET

Sierpinski Triangle

andreisophie Mar 26th, 2019 (edited) 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <graphics.h>
  2.  
  3. void triunghi(int x1, int y1, int x2, int y2, int x3, int y3)
  4. {
  5.     line(x1,y1,x2,y2);
  6.     line(x1,y1,x3,y3);
  7.     line(x2,y2,x3,y3);
  8. }
  9.  
  10. void triunghiR(int x1, int y1, int x2, int y2, int x3, int y3)
  11. {
  12.     triunghi((x1+x2)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2,(x2+x3)/2,(y2+y3)/2);
  13.     if (x3-x1>8)
  14.     {
  15.         triunghiR((x1+x2)/2,(y1+y2)/2,x2,y2,(x2+x3)/2,(y2+y3)/2);
  16.         triunghiR(x1,y1,(x1+x2)/2,(y1+y2)/2,(x1+x3)/2,(y1+y3)/2);
  17.         triunghiR((x1+x3)/2,(y1+y3)/2,(x2+x3)/2,(y2+y3)/2,x3,y3);
  18.     }
  19. }
  20.  
  21. void Sierpinski(int x1, int y1, int x2, int y2, int x3, int y3)
  22. {
  23.     triunghi(x1,y1,x2,y2,x3,y3);
  24.     triunghiR(x1,y1,x2,y2,x3,y3);
  25. }
  26.  
  27. int main()
  28. {
  29.     initwindow(1200, 1000);
  30.  
  31.     const int x1=100,x2=getmaxx()-100,x3=getmaxx()/2;
  32.     const int y1=40+(x2-x1)*0.866,y2=y1,y3=40;
  33.  
  34.  
  35.     setcolor(14);
  36.     Sierpinski(x1,y1,x2,y2,x3,y3);
  37.  
  38.     getch();
  39.     cleardevice();
  40.     closegraph();
  41.  
  42.     return 0;
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top