Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <windows.h>
  5. #include <GL/glut.h>
  6.  
  7. typedef struct
  8. {
  9.     float x, y;
  10. } point2D_t;
  11.  
  12. typedef struct
  13. {
  14.     float r, g, b;
  15. } color_t;
  16.  
  17. void drawPolygon(point2D_t p[], int n)
  18. {
  19.     glBegin(GL_POLYGON);
  20.     for(int i=0; i<n; i++)
  21.     {
  22.         glVertex2f(p[i].x,p[i].y);
  23.     }
  24.     glEnd();
  25. }
  26.  
  27. void drawPolygon(point2D_t p[], color_t col, int n)
  28. {
  29.     glColor3f(col.r, col.g, col.b);
  30.     glBegin(GL_POLYGON);
  31.     for(int i=0; i<n; i++)
  32.     {
  33.         glVertex2f(p[i].x,p[i].y);
  34.     }
  35.     glEnd();
  36. }
  37.  
  38. void drawPolygon(point2D_t p[], color_t col[], int n)
  39. {
  40.     glBegin(GL_POLYGON);
  41.     for(int i=0; i<n; i++)
  42.     {
  43.         glColor3f(col[i].r, col[i].g, col[i].b);
  44.         glVertex2f(p[i].x,p[i].y);
  45.     }
  46.     glEnd();
  47. }
  48.  
  49. void drawCircle(float r, float xp, float yp, int n)
  50. {
  51.     point2D_t p[360];
  52.     float teta;
  53.     for(int i=0; i<n; i++)
  54.     {
  55.         teta=(float)i*6.28/n;
  56.         p[i].x=xp+r*cos(teta);
  57.         p[i].y=yp+r*sin(teta);
  58.     }
  59.     drawPolygon(p,n);
  60. }
  61. typedef struct
  62. {
  63.     int n;
  64.     point2D_t p[100];
  65. } object2D_t;
  66.  
  67. object2D_t createCircle(float r, int n)
  68. {
  69.     object2D_t obj;
  70.     obj.n=n;
  71.     for(int i=0; i<n; i++)
  72.     {
  73.         float s=i*6.28/n;
  74.         obj.p[i].x=r*cos(s);
  75.         obj.p[i].y=r*sin(s);
  76.     }
  77.     return obj;
  78. }
  79.  
  80. typedef struct
  81. {
  82.     float v[3];
  83. } vector2D_t;
  84.  
  85. typedef struct
  86. {
  87.     float m[3][3];
  88. } matrix2D_t;
  89.  
  90. vector2D_t point2vector(point2D_t p)
  91. {
  92.     vector2D_t vec;
  93.     vec.v[0]=p.x;
  94.     vec.v[1]=p.y;
  95.     vec.v[2]=1;
  96.     return vec;
  97. }
  98.  
  99. point2D_t vector2point(vector2D_t vec)
  100. {
  101.     point2D_t p;
  102.     p.x=vec.v[0];
  103.     p.y=vec.v[1];
  104.     return p;
  105. }
  106.  
  107. vector2D_t operator * (matrix2D_t mat, vector2D_t vec)
  108. {
  109.     vector2D_t vec1;
  110.     for(int i=0; i<3; i++)
  111.     {
  112.         vec1.v[i]=0;
  113.         for(int j=0; j<3; j++)
  114.         {
  115.             vec1.v[i]+=mat.m[i][j]*vec.v[j];
  116.         }
  117.     }
  118.     return vec1;
  119. }
  120.  
  121. matrix2D_t operator * (matrix2D_t mat1, matrix2D_t mat2)
  122. {
  123.     matrix2D_t mat;
  124.     for(int i=0; i<3; i++)
  125.     {
  126.         for(int j=0; j<3; j++)
  127.         {
  128.             mat.m[i][j]=0;
  129.             for(int k=0; k<3; k++)
  130.             {
  131.                 mat.m[i][j]+=mat1.m[i][k]*mat2.m[k][j];
  132.             }
  133.         }
  134.     }
  135.     return mat;
  136. }
  137.  
  138. matrix2D_t createIdentity()
  139. {
  140.     matrix2D_t mat;
  141.     for(int i=0; i<3; i++)
  142.     {
  143.         for(int j=0; j<3; j++)
  144.         {
  145.             mat.m[i][j]=0;
  146.         }
  147.         mat.m[i][i]=1;
  148.     }
  149.     return mat;
  150. }
  151.  
  152. matrix2D_t translationMTX(float dx, float dy)
  153. {
  154.     matrix2D_t mat=createIdentity();
  155.     mat.m[0][2]=dx;
  156.     mat.m[1][2]=dy;
  157.     return mat;
  158. }
  159.  
  160. matrix2D_t scalingMTX(float sx, float sy)
  161. {
  162.     matrix2D_t mat=createIdentity();
  163.     mat.m[0][0]=sx;
  164.     mat.m[1][1]=sy;
  165.     return mat;
  166. }
  167.  
  168. matrix2D_t rotationMTX(float a)
  169. {
  170.     matrix2D_t mat=createIdentity();
  171.     mat.m[0][0]=cos(a);
  172.     mat.m[0][1]=-sin(a);
  173.     mat.m[1][0]=sin(a);
  174.     mat.m[1][1]=cos(a);
  175.     return mat;
  176. }
  177.  
  178. object2D_t createCircle(float ra, float rb, float xp, float yp, int n)
  179. {
  180.     object2D_t obj;
  181.     obj.n=n;
  182.     for(int i=0; i<n; i++)
  183.     {
  184.         float s=i*6.28/n;
  185.         obj.p[i].x=xp+ra*cos(s);
  186.         obj.p[i].y=yp+rb*sin(s);
  187.     }
  188.     return obj;
  189. }
  190.  
  191. void userdraw(void)
  192. {
  193.     int i;
  194.     static float a=0;
  195.     matrix2D_t mat=rotationMTX(a)*translationMTX(50,0);
  196.     vector2D_t vec;
  197.     color_t w2[3]= {{1,1,1},{0.7,0.7,0.7},{0.7,0.7,0.7}};
  198.     point2D_t p2[3]= {{0,20},{8,0},{-8,0}};
  199.     for(i=0; i<3; i++)
  200.     {
  201.         vec=point2vector(p2[i]);
  202.         vec=mat*vec;
  203.         p2[i]=vector2point(vec);
  204.     }
  205.     point2D_t p1[3]= {{0,13},{15,0},{-15,0}};
  206.     for(i=0; i<3; i++)
  207.     {
  208.         vec=point2vector(p1[i]);
  209.         vec=mat*vec;
  210.         p1[i]=vector2point(vec);
  211.     }
  212.     point2D_t pc1= {0,10};
  213.     object2D_t c1=createCircle(2,4,pc1.x,pc1.y,30);
  214.     for(i=0; i<30; i++)
  215.     {
  216.         vec=point2vector(c1.p[i]);
  217.         vec=mat*vec;
  218.         c1.p[i]=vector2point(vec);
  219.     }
  220.     drawPolygon(p1,w2,3);
  221.     drawPolygon(p2,w2,3);
  222.     drawPolygon(c1.p, {1,1,1},30);
  223.  
  224.     static float ta=-5;
  225.     static float dta=0.05;
  226.     ta=ta+dta;
  227.     if(ta<=-5)
  228.         dta=-dta;
  229.     if(ta>=-2)
  230.         dta=-dta;
  231.  
  232.     point2D_t api1[3]= {{3,0},{9,0},{6,ta}};
  233.     for(i=0; i<3; i++)
  234.     {
  235.         vec=point2vector(api1[i]);
  236.         vec=mat*vec;
  237.         api1[i]=vector2point(vec);
  238.     }
  239.     color_t w1[3]= {{1,0,0},{1,0,0},{1,1,0}};
  240.     point2D_t api2[3]= {{-3,0},{-9,0},{-6,ta}};
  241.     for(i=0; i<3; i++)
  242.     {
  243.         vec=point2vector(api2[i]);
  244.         vec=mat*vec;
  245.         api2[i]=vector2point(vec);
  246.     }
  247.     drawPolygon(api1,w1,3);
  248.     drawPolygon(api2,w1,3);
  249.  
  250.     a=a+0.001;
  251. }
  252.  
  253. void display(void)
  254. {
  255.     glClear( GL_COLOR_BUFFER_BIT);
  256.     userdraw();
  257.     glutSwapBuffers();
  258. }
  259.  
  260. static int xk;
  261.  
  262. void keyboard(unsigned char key, int x, int y)
  263. {
  264.     if(key == 'a')
  265.     {
  266.         xk = xk - 5;
  267.     }
  268.     else if(key == 'd')
  269.     {
  270.         xk = xk + 5;
  271.     }
  272.     else if(key == 27)
  273.     {
  274.         exit(0);
  275.     }
  276. }
  277.  
  278. int main(int argc, char **argv)
  279. {
  280.     glutInit(&argc,argv);
  281.     glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
  282.     glutInitWindowPosition(100, 100);
  283.     glutInitWindowSize(800, 800);
  284.     glutCreateWindow ("Drawing by ken");
  285.     glClearColor(0.0, 0.0, 0.0, 0.0);
  286.     gluOrtho2D(-100, 100, -100, 100);
  287.     glutIdleFunc(display);
  288.     glutKeyboardFunc(keyboard);
  289.     glutDisplayFunc(display);
  290.     glutMainLoop();
  291.     return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement