Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <windows.h>
- #include <GL/glut.h>
- typedef struct
- {
- float x, y;
- } point2D_t;
- typedef struct
- {
- float r, g, b;
- } color_t;
- void drawPolygon(point2D_t p[], int n)
- {
- glBegin(GL_POLYGON);
- for(int i=0; i<n; i++)
- {
- glVertex2f(p[i].x,p[i].y);
- }
- glEnd();
- }
- void drawPolygon(point2D_t p[], color_t col, int n)
- {
- glColor3f(col.r, col.g, col.b);
- glBegin(GL_POLYGON);
- for(int i=0; i<n; i++)
- {
- glVertex2f(p[i].x,p[i].y);
- }
- glEnd();
- }
- void drawPolygon(point2D_t p[], color_t col[], int n)
- {
- glBegin(GL_POLYGON);
- for(int i=0; i<n; i++)
- {
- glColor3f(col[i].r, col[i].g, col[i].b);
- glVertex2f(p[i].x,p[i].y);
- }
- glEnd();
- }
- void drawCircle(float r, float xp, float yp, int n)
- {
- point2D_t p[360];
- float teta;
- for(int i=0; i<n; i++)
- {
- teta=(float)i*6.28/n;
- p[i].x=xp+r*cos(teta);
- p[i].y=yp+r*sin(teta);
- }
- drawPolygon(p,n);
- }
- typedef struct
- {
- int n;
- point2D_t p[100];
- } object2D_t;
- object2D_t createCircle(float r, int n)
- {
- object2D_t obj;
- obj.n=n;
- for(int i=0; i<n; i++)
- {
- float s=i*6.28/n;
- obj.p[i].x=r*cos(s);
- obj.p[i].y=r*sin(s);
- }
- return obj;
- }
- typedef struct
- {
- float v[3];
- } vector2D_t;
- typedef struct
- {
- float m[3][3];
- } matrix2D_t;
- vector2D_t point2vector(point2D_t p)
- {
- vector2D_t vec;
- vec.v[0]=p.x;
- vec.v[1]=p.y;
- vec.v[2]=1;
- return vec;
- }
- point2D_t vector2point(vector2D_t vec)
- {
- point2D_t p;
- p.x=vec.v[0];
- p.y=vec.v[1];
- return p;
- }
- vector2D_t operator * (matrix2D_t mat, vector2D_t vec)
- {
- vector2D_t vec1;
- for(int i=0; i<3; i++)
- {
- vec1.v[i]=0;
- for(int j=0; j<3; j++)
- {
- vec1.v[i]+=mat.m[i][j]*vec.v[j];
- }
- }
- return vec1;
- }
- matrix2D_t operator * (matrix2D_t mat1, matrix2D_t mat2)
- {
- matrix2D_t mat;
- for(int i=0; i<3; i++)
- {
- for(int j=0; j<3; j++)
- {
- mat.m[i][j]=0;
- for(int k=0; k<3; k++)
- {
- mat.m[i][j]+=mat1.m[i][k]*mat2.m[k][j];
- }
- }
- }
- return mat;
- }
- matrix2D_t createIdentity()
- {
- matrix2D_t mat;
- for(int i=0; i<3; i++)
- {
- for(int j=0; j<3; j++)
- {
- mat.m[i][j]=0;
- }
- mat.m[i][i]=1;
- }
- return mat;
- }
- matrix2D_t translationMTX(float dx, float dy)
- {
- matrix2D_t mat=createIdentity();
- mat.m[0][2]=dx;
- mat.m[1][2]=dy;
- return mat;
- }
- matrix2D_t scalingMTX(float sx, float sy)
- {
- matrix2D_t mat=createIdentity();
- mat.m[0][0]=sx;
- mat.m[1][1]=sy;
- return mat;
- }
- matrix2D_t rotationMTX(float a)
- {
- matrix2D_t mat=createIdentity();
- mat.m[0][0]=cos(a);
- mat.m[0][1]=-sin(a);
- mat.m[1][0]=sin(a);
- mat.m[1][1]=cos(a);
- return mat;
- }
- object2D_t createCircle(float ra, float rb, float xp, float yp, int n)
- {
- object2D_t obj;
- obj.n=n;
- for(int i=0; i<n; i++)
- {
- float s=i*6.28/n;
- obj.p[i].x=xp+ra*cos(s);
- obj.p[i].y=yp+rb*sin(s);
- }
- return obj;
- }
- void userdraw(void)
- {
- int i;
- static float a=0;
- matrix2D_t mat=rotationMTX(a)*translationMTX(50,0);
- vector2D_t vec;
- color_t w2[3]= {{1,1,1},{0.7,0.7,0.7},{0.7,0.7,0.7}};
- point2D_t p2[3]= {{0,20},{8,0},{-8,0}};
- for(i=0; i<3; i++)
- {
- vec=point2vector(p2[i]);
- vec=mat*vec;
- p2[i]=vector2point(vec);
- }
- point2D_t p1[3]= {{0,13},{15,0},{-15,0}};
- for(i=0; i<3; i++)
- {
- vec=point2vector(p1[i]);
- vec=mat*vec;
- p1[i]=vector2point(vec);
- }
- point2D_t pc1= {0,10};
- object2D_t c1=createCircle(2,4,pc1.x,pc1.y,30);
- for(i=0; i<30; i++)
- {
- vec=point2vector(c1.p[i]);
- vec=mat*vec;
- c1.p[i]=vector2point(vec);
- }
- drawPolygon(p1,w2,3);
- drawPolygon(p2,w2,3);
- drawPolygon(c1.p, {1,1,1},30);
- static float ta=-5;
- static float dta=0.05;
- ta=ta+dta;
- if(ta<=-5)
- dta=-dta;
- if(ta>=-2)
- dta=-dta;
- point2D_t api1[3]= {{3,0},{9,0},{6,ta}};
- for(i=0; i<3; i++)
- {
- vec=point2vector(api1[i]);
- vec=mat*vec;
- api1[i]=vector2point(vec);
- }
- color_t w1[3]= {{1,0,0},{1,0,0},{1,1,0}};
- point2D_t api2[3]= {{-3,0},{-9,0},{-6,ta}};
- for(i=0; i<3; i++)
- {
- vec=point2vector(api2[i]);
- vec=mat*vec;
- api2[i]=vector2point(vec);
- }
- drawPolygon(api1,w1,3);
- drawPolygon(api2,w1,3);
- a=a+0.001;
- }
- void display(void)
- {
- glClear( GL_COLOR_BUFFER_BIT);
- userdraw();
- glutSwapBuffers();
- }
- static int xk;
- void keyboard(unsigned char key, int x, int y)
- {
- if(key == 'a')
- {
- xk = xk - 5;
- }
- else if(key == 'd')
- {
- xk = xk + 5;
- }
- else if(key == 27)
- {
- exit(0);
- }
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(800, 800);
- glutCreateWindow ("Drawing by ken");
- glClearColor(0.0, 0.0, 0.0, 0.0);
- gluOrtho2D(-100, 100, -100, 100);
- glutIdleFunc(display);
- glutKeyboardFunc(keyboard);
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement