1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <algorithm> #include <math.h> #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> #include <gl/glut.h> using namespace std; #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
const double PI = atan(1.0) * 4; const int WINDOW_WIDTH = 640; const int WINDOW_HEIGHT = 480; const int WINDOW_POS_X = 300; const int WINDOW_POS_Y = 150; const double BIG_R = 100; const double MID_R = 50; const double SML_R = 10;
void myInit() { glClearColor(0.5, 0.5, 0.5, 0.0); glColor3f(0.0, 0.0, 1.0); glLineWidth(2.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT); }
void drawArc(double fX, double fY, double fR, double fBeg, double fEnd) { double fAdd = 0.0001;
fBeg = fBeg * PI / 180; fEnd = fEnd * PI / 180; if (fBeg > fEnd) { swap(fBeg, fEnd); }
glBegin(GL_POLYGON); while (fBeg < fEnd) { glVertex2f(fX + fR * cos(fBeg), fY + fR * sin(fBeg)); fBeg += fAdd; } glEnd(); }
void myDisplay() { glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0); drawArc(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, BIG_R, 0, 180); glColor3f(1.0, 1.0, 1.0); drawArc(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, BIG_R, 180, 360); glColor3f(0.0, 0.0, 0.0); drawArc(WINDOW_WIDTH / 2 - MID_R, WINDOW_HEIGHT / 2, MID_R, 0, 360); glColor3f(1.0, 1.0, 1.0); drawArc(WINDOW_WIDTH / 2 + MID_R, WINDOW_HEIGHT / 2, MID_R, 0, 360); drawArc(WINDOW_WIDTH / 2 - MID_R, WINDOW_HEIGHT / 2, SML_R, 0, 360); glColor3f(0.0, 0.0, 0.0); drawArc(WINDOW_WIDTH / 2 + MID_R, WINDOW_HEIGHT / 2, SML_R, 0, 360);
glFlush(); }
int main(int argc, char** argv) { glutInit(argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); glutInitWindowPosition(WINDOW_POS_X, WINDOW_POS_Y); glutCreateWindow("阴阳图"); glutDisplayFunc(myDisplay); myInit(); glutMainLoop();
return 0; }
|