You are on page 1of 6

#include <stdio.

h>
#include <glut.h>
#include <math.h>
#include <stdlib.h>

#define windowWidth 640


#define windowHeight 480
#define startX 0
#define startY 0
#define M_PI 3.14159265358979
#define RAD(goc) ((goc)*(M_PI/180.0))

int dgoc =0;


int dgoc1=0;

struct colorentry
{
unsigned char red;
unsigned char green;
unsigned char blue;
colorentry(int r, int g, int b):red(r),green(g),blue(b){};
};

void Init();
void Display();
void Reshape(int Width, int Height);
void keyCB(unsigned char key, int x, int y);
void specialKey(int key, int x, int y);
void OnIdle();
void DrawLine(int x1, int y1, int x2, int y2, colorentry c);
void PutPixel(int x, int y, colorentry c);
void OnTime1(int d);
//void OnTime2(int j);
void DrawSquareRotate(int x0, int y0, int dgoc, colorentry c);
void DrawElip (int xc, int yc, int a, int b, int i);

int Xt,Yt;
int n=8;
int w=150;

int main(int argc, char* argv[])


{
//Khoi tao cua so OpenGl
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(startX,startY);
glutCreateWindow("Vi Du");
Init();
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(keyCB);
glutSpecialFunc(specialKey);
glutTimerFunc(100,OnTime1,5);//100 miligiay thi OnTime goi 1 lan
//tham so dau vao la so 5
// glutTimerFunc(100,OnTime2,10);
glutIdleFunc(OnIdle);
glutMainLoop();
return 0;
}

void Init()
{
glClearColor(1.0,1.0,1.0,1.0);
//glClearColor(128,255,0,1.0);
Xt=0;
Yt=0;

void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
int xmax = glutGet(GLUT_WINDOW_WIDTH);
int ymax = glutGet(GLUT_WINDOW_HEIGHT);

DrawSquareRotate(Xt,Yt,dgoc,colorentry(255,255,0));

glFlush();
glutSwapBuffers();
}

void OnIdle()
{
glutPostRedisplay();
}

void Reshape(int Width, int Height)


{
glViewport(0,0,(GLsizei)Width,(GLsizei)Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-Width/2,Width/2,-Height/2,Height/2);
}

void keyCB(unsigned char key, int x, int y)


{
if(key=='q'||key=='Q')
exit(0);
switch(key)
{
case'+':
n++;
break;
case'-':
n--;
break;
}
}

void specialKey(int key, int x, int y)


{
int xmax = glutGet(GLUT_WINDOW_WIDTH);
int ymax = glutGet(GLUT_WINDOW_HEIGHT);

float R = sqrt(float(w)*w+w*w);

switch(key)
{
case GLUT_KEY_LEFT:
if(Xt>(-xmax/2+R))
Xt-=1;
break;
case GLUT_KEY_RIGHT:
if(Xt<(xmax/2-R))
Xt+=1;
break;
case GLUT_KEY_UP:
if(Yt<(ymax/2-R))
Yt+=1;
break;
case GLUT_KEY_DOWN:
if(Yt>(-ymax/2+R))
Yt-=1;
break;
}
}

void DrawLine(int x1, int y1, int x2, int y2, colorentry c)
{
glBegin(GL_LINES);
glColor3ub(c.red,c.green,c.blue);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}

void PutPixel(int x, int y, colorentry c)


{
glPointSize(2.0);
glBegin(GL_POINTS);
glColor3ub(c.red,c.green,c.blue);
glVertex2f(x,y);
glEnd();
}

void OnTime1(int d)
{
dgoc = (dgoc + d)%360;
glutTimerFunc(100,OnTime1,5);
}
void OnTime2(int j)
{
dgoc1 = (dgoc1 + j)%180;
glutTimerFunc(100,OnTime1,10);
}

void DrawSquareRotate(int xc, int yc, int dgoc, colorentry c)


{
/*float xi,yi;*/
GLfloat R=sqrt(GLfloat(w)*w+w*w);
// GLint dgoc[]={0,90,180,270};
float r=R/1.3;
float r1=R/3;
float r2=R/2;
/* glBegin(GL_LINE_LOOP);
glColor3ub(c.red,c.green,c.blue);
for (int i=0;i<n;i++)
{
xi=xc+R*cos(RAD(i*360/n));
yi=yc+R*sin(RAD(i*360/n));
glVertex2f(xi,yi);
glVertex2f(xc,yc);
glVertex2f(xi,yi);
}
glEnd();
float xj,yj;
glBegin(GL_POLYGON);
glColor3f(0,0,1);
for (int j=0;j<n;j++)
{
xj=xc+r*cos(RAD(j*360/n ));
yj=yc+r*sin(RAD(j*360/n ));

glVertex2f(xj,yj);
glVertex2f(xc,yc);
glVertex2f(xj,yj);

}
glEnd();*/

float xk,yk;
glBegin(GL_POLYGON);
glColor3f(255,255,0);
for (int k=90;k<=270;k++)
{
xk=xc+r1*cos(RAD(k-dgoc));
yk=yc+r1*sin(RAD(k-dgoc));
glVertex2f(xk,yk);
}
glEnd();

float xm,ym;
glBegin(GL_POLYGON);
glColor3f(0,0,0);
for (int m=271;m<450;m++)
{
xm=xc+r1*cos(RAD(m-dgoc));
ym=yc+r1*sin(RAD(m-dgoc));

glVertex2f(xm,ym);
}
glEnd();
//Nua vong tron vang nho
//float xn,yn;
// float _xc,_yc;
///* glBegin(GL_POLYGON);
// glColor3f(255,255,0);*/
// for (int n=271;n<272;n++)
// {
// /*xn=((xc+r1*cos(RAD(n+dgoc)))/2);
// yn=((yc-71+r1*sin(RAD(n+dgoc)))/2);*/
// _xc = xc+(r1*cos((RAD(n))))/2;
// // _yc = yc-35+r1+6*r1/11*sin(RAD(n+dgoc));
// _yc = yc-35+r1+r1/2*sin(RAD(n));
// DrawElip(_xc,_yc-r1/2,r1/2,r1/2,255);
//DrawElip(_xc,_yc-r1/2,r1/8,r1/8,0);
// /* glVertex2f(xn,yn);*/
// }
// glEnd();
//Nua vong tron den nho
// float xa,ya;
///*glBegin(GL_POLYGON);
//glColor3f(0,0,0);*/
//for (int k=90;k<91;k++)
//{
// xa=((xc+r1*cos(RAD(k+dgoc)))/2);
// ya=((yc+71+r1*sin(RAD(k+dgoc)))/2);
// /*glVertex2f(xa,ya);*/
//}
//glEnd();
float _xc,_yc;
/* glBegin(GL_POLYGON);
glColor3f(255,255,0);*/
for (int n=271;n<272;n++)
{
/*xn=((xc+r1*cos(RAD(n+dgoc)))/2);
yn=((yc-71+r1*sin(RAD(n+dgoc)))/2);*/
_xc=xc+(r1*cos((RAD(n-dgoc)))/2);
_yc=yc-36+r1+(r1/2)*sin(RAD(n-dgoc));
DrawElip(_xc,_yc-r1/2,r1/2,r1/2,255);
DrawElip(_xc,_yc-r1/2,r1/8,r1/8,0);
/* glVertex2f(xn,yn);*/
}
glEnd();
float xa,ya;
//glBegin(GL_POLYGON);
//glColor3f(0,0,0);*/
for (int j=90;j<91;j++)
{
xa=xc+(r1*cos((RAD(j-dgoc)))/2);
ya=yc-106+r1+(r1/2)*sin(RAD(j-dgoc));
DrawElip(xa,ya+r1/2,r1/2,r1/2,0);
DrawElip(xa,ya+r1/2,r1/8,r1/8,255);

/*glVertex2f(xa,ya);*/
}
glEnd();
/* float _xc,_yc;*/
for (int k =90;k<91;k++)
{
/* _xc = xc+r1*cos((RAD(k-90+dgoc/2)));
_yc = yc+r1/2+r1*sin((RAD(k-90+dgoc/2)));*/
/*_xc = xc+r1*cos(RAD(k));
_yc = yc+r1/4+r1*sin(RAD(k));*/
//DrawElip(_xc,_yc+r1/2,r1/2,r1/2,0);//tròn đen lớn
//DrawElip(_xc,_yc-r1/2,r1/2,r1/2,255);
/* DrawElip(_xc,_yc+r1/2,r1/8,r1/8,255);
DrawElip(_xc,_yc-r1/2,r1/8,r1/8,0);*/

void DrawElip (int xc, int yc, int a, int b, int i)


{
float xh,yh;
// float _xc,_yc;
glBegin(GL_POLYGON);
glColor3f(i,i,0);
for(int h=0; h<360; h++)
{
xh=xc+a*cos(RAD(h));
yh=yc+b*sin(RAD(h));
glVertex2f(xh,yh);
}
glEnd();
}

You might also like