Advertisement
jovanovski

КГ Лаб1 Етиописки

Mar 25th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <GL/gl.h>
  2. #include <GL/glut.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. int n;
  7. float a;
  8. void display();
  9. static void init()
  10. {
  11.   /* set clear color to white */
  12.   glClearColor (1.0, 1.0, 1.0, 0.0);
  13.   /* set fill  color to black */
  14.   glColor3f(0.0, 0.0, 0.0);
  15.  
  16.   glMatrixMode (GL_PROJECTION);
  17.   glLoadIdentity ();
  18.   gluOrtho2D(-0.5, 1.5, -0.5, 1.5);
  19.  
  20. } /* end init */
  21.  
  22. int main(int argc, char* argv[])
  23. {
  24.     if(argc>1)
  25.         n=atoi(argv[1]);
  26.     else
  27.         n=3;
  28.  
  29.  
  30.   glutInit(&argc,argv);
  31.   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  32.   glutInitWindowSize(600, 600);
  33.   glutInitWindowPosition(100,100);
  34.   glutCreateWindow(argv[0]);
  35.   glutDisplayFunc(display);
  36.   init();
  37.   glutMainLoop();
  38.   return 0;
  39. }
  40.  
  41. void display()
  42. {
  43.     glClear (GL_COLOR_BUFFER_BIT);
  44.     void etiop(float, float, float, float, int);
  45.     etiop(0.0,0.0,1.0,0,n);
  46.     glFlush();
  47. }
  48.  
  49. void etiop(float x1, float y1, float x2, float y2, int n)
  50. {
  51.     float xa,ya,xb,yb,xc,yc,xd,yd,xdg,ydg,xdd,ydd,k,d;
  52.     if(n<=0)
  53.     {
  54.         glBegin(GL_LINES);
  55.             glVertex2f(x1,y1);
  56.       glVertex2f(x2,y2);
  57.       glEnd();
  58.         return;
  59.     }
  60.     else
  61.     {
  62.         xa=x1+(x2-x1)/5;
  63.         ya=y1+(y2-y1)/5;
  64.         xb=x1+(x2-x1)*4/5;
  65.         yb=y1+(y2-y1)*4/5;
  66.  
  67.         k=atan2(yb-ya,xb-xa);
  68.         d= sqrt (( xb - xa) *(xb - xa)+(yb - ya) *(yb - ya) ) ;
  69.         d = d/sqrt(2);
  70.  
  71.         xc=xa+d*cos(k+M_PI/4);
  72.         yc=ya+d*sin(k+M_PI/4);
  73.         xd=xa+d*cos(k-M_PI/4);
  74.         yd=ya+d*sin(k-M_PI/4);
  75.  
  76.         d= sqrt (( xa - x1) *(xa - x1)+(ya - y1) *(ya - y1) ) ;
  77.  
  78.  
  79.         k=atan2(yb-ya,xb-xa);
  80.         xdg=xc+d*cos(k+M_PI/2);
  81.         ydg=yc+d*sin(k+M_PI/2);
  82.         xdd=xd+d*cos(k-M_PI/2);
  83.         ydd=yd+d*sin(k-M_PI/2);
  84.  
  85.  
  86.         etiop(xc,yc,xdg,ydg,n-1);
  87.         etiop(xd,yd,xdd,ydd,n-1);
  88.  
  89.  
  90.         etiop(x1,y1,xa,ya,n-1);
  91.         etiop(xa,ya,xd,yd,n-1);
  92.         etiop(xd,yd,xb,yb,n-1);
  93.         etiop(xa,ya,xc,yc,n-1);
  94.         etiop(xc,yc,xb,yb,n-1);
  95.         etiop(xb,yb,x2,y2,n-1);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement