OpenGL阴阳图 //rorger,2011 //contact me:jhcyd112@163.com #include "windows.h" #include "glut.h" #include "math.h" class Point2{ public: double x, y ; void set(double x, double y){this->x=x ;this->y=y;} void set(Point2&p){x=p.x;y=p.y;} double operator *(Point2& rhn) { return this->x*rhn.x + this->y*rhn.y; } Point2 operator*(double rhn) { Point2 result ; result.x=this->x*rhn; result.y=this->y*rhn; return result; } double dotPorduct(Point2& rhn) { return this->x*rhn.x + this->y*rhn.y; } Point2 operator -(Point2& rhn) { Point2 result ; result.x=this->x-rhn.x; result.y=this->y-rhn.y; return result; } Point2 operator +(Point2& rhn) { Point2 result ; result.x=this->x+rhn.x; result.y=this->y+rhn.y; return result; } Point2 operator /(double rhn) { Point2 result ; result.x=this->x/rhn; result.y=this->y/rhn; return result; } double GetLength() { return sqrt(this->x*this->x+this->y*this->y); } }; typedef Point2 Vector2; int GetCircumCenter (Point2 A,Point2 B,Point2 C, Point2& S,double& radius) { Vector2 a,b,c,na; a=B-A; b=C-B; c=A-C; na.set(-a.y,a.x); S=A+ (a+na*((b*c)/(na*c))) / ((double)2.0); double temp = (b*c)/(na*c); radius=(a.GetLength())/2.0 * (sqrt( temp * temp + 1)); return 0; } void drawArc(double x,double y,double start_angle,double end_angle,double delta_angle,double radius,bool fill) { if (fill) { glBegin(GL_TRIANGLE_FAN); } else { glBegin(GL_LINE_STRIP); } for (double i=start_angle;i<=end_angle;i+=delta_angle) { double vx=x+radius * cos(i); double vy=y+radius*sin(i); glVertex2d(vx,vy); } glEnd(); } void drawCircle(double x, double y, double radius,bool fill) { double pi=acos(-1.0); drawArc(x,y,0,2*pi,pi/360,radius,fill); } void drawPie(double x,double y,double start_angle,double end_angle,double delta_angle,double radius,bool fill) { drawArc(x,y,start_angle,end_angle,delta_angle,radius,fill); if (fill) { glBegin(GL_TRIANGLE_FAN); } else { glBegin(GL_LINES); } glVertex2d(x,y); glVertex2d(x+radius*cos(start_angle),y+radius*sin(start_angle)); if (!fill) { glVertex2d(x,y); } glVertex2d(x+radius*cos(end_angle),y+radius*sin(end_angle)); glEnd(); } void drawYinyang(double x, double y, double radius) { double pi=acos(-1.0); glColor3f(1.0,0.45,0.33); drawArc(x,y,pi/2.0,3*pi/2.0,pi/180,radius,true); glColor3f(0.33,0.55,0.8); drawArc(x,y,-pi/2.0,pi/2.0,pi/180,radius,true); glColor3f(1.0,0.45,0.33); drawArc(x,(y+radius)/2.0,0,2*pi,pi/180.0,radius/2.0,true); glColor3f(0.33,0.55,0.8); drawArc(x,(y-radius)/2.0,0,2*pi,pi/180.0,radius/2.0,true); glColor3f(1.0,0.45,0.33); drawArc(x,(y-radius)/2.0,0,2*pi,pi/180.0,radius/8.0,true); glColor3f(0.33,0.55,0.8); drawArc(x,(y+radius)/2.0,0,2*pi,pi/180.0,radius/8.0,true); glLineWidth(3.5); glColor3f(0.33,0.55,0.8); drawCircle(x,y,radius,false); glFlush(); } void myDisplay(void) // plot the sinc function, using world coordinates { glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); drawYinyang(0,0,10); } void myInit(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glColor3f(0.0f, 0.0f, 0.0f); glPointSize(4.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-15, 15, -15, 15); glLineWidth(2.0); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(480,480); glutInitWindowPosition(100,150); glutCreateWindow("LineInterSectoin"); glutDisplayFunc(myDisplay); myInit( ); glutMainLoop( ); }