角度离散法绘制圆弧 | 椭圆

来源:互联网 发布:腾讯网络电视 编辑:程序博客网 时间:2024/05/22 15:37
</pre><pre name="code" class="cpp">
#include<GL/glut.h>     //绘制圆弧程序#include<stdio.h>#include<math.h>double pi=3.1415936;int x,y;double r,as,ae;void arc(int x,int y,double r,double as,double ae){if(ae<as)ae+=2*pi;double dt=0.4/r;//取角度离散值,反比,半径越大该值越小int n=(int)((ae-as)/dt);//一个圆弧上总共取n个点glColor3f(0.0,1.0,0.0);int xs=(int)(x+r*(cos(as)));//圆弧起始点int ys=(int)(y+r*(sin(ae)));glVertex2i(xs,ys);glBegin(GL_LINE_STRIP);//通过绘制折线来逼近圆弧for(int i=0;i<n;i++){double cost=r*cos(as+i*dt);double sint=r*sin(as+i*dt);glVertex2i(int(x+cost),int(y+sint));}glEnd();}void display(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);arc(x,y,r,as,ae);glFlush();}int main(int argc,char* argv[]){printf("please enter the position of Arc which include x_value,y_value,radiius,arc_start_angle,arc_end_angle\n");scanf("%d%d%lf%lf%lf",&x,&y,&r,&as,&ae);printf("x=%d,y=%d,r=%lf,as=%lf,ae=%lf\n",x,y,r,as,ae);glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowSize(800,800);glutCreateWindow("hello world!");gluOrtho2D(0,1000,0,1000);glutDisplayFunc(display);glutMainLoop();return 0;}
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">#include<stdio.h><span style="white-space:pre"></span>//绘制椭圆程序
#include<math.h>double pi=3.1415936;int a,b;void swap(int* a,int* b){int temp;temp=*a;*a=*b;*b=temp;}void ellipse(int x,int y,int a,int b){glColor3f(0.0,1.0,0.0);int flag=0;if (a<b)//比较a,b 的值确保a始终是长轴{    flag=1;  swap(&a,&b);}double d=a*a-b*b;double c=sqrt(d);double dt=1/c;//取角度离散值,反比,c越大该值越小int n=(int)(2*pi/dt);//一个圆弧上总共取n个点glPointSize(3);glBegin(GL_POINTS);if(!flag){glVertex2f(x+c,y); glVertex2f(x-c,y); }else{   glVertex2f(y,x+c); glVertex2f(y,x-c); }glEnd();glBegin(GL_LINE_LOOP);for(int i=0;i<n;i++){int cost=a*cos(i*dt);int sint=b*sin(i*dt);if(!flag)glVertex2i(x+cost,y+sint);elseglVertex2i(y+sint,x+cost);//printf("x=%d,y=%d\n",cost,sint);}glEnd();}void display(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);ellipse(500,500,300,100);glFlush();}int main(int argc,char* argv[]){//printf("please enter the position of ellipse which include x_value,y_value,long_axiel,short_exiel\n");//scanf("%d%d",&a,&b);//printf("a=%d,b=%d\n",a,b);glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowSize(800,800);glutCreateWindow("hello world!");gluOrtho2D(0,1000,0,1000);glutDisplayFunc(display);glutMainLoop();return 0;}



0 0
原创粉丝点击