dev c++怎么在一个窗口画出直线和圆 程序已给出求调用方法 10
#include<GL/glut.h>voidinit(void){glClearColor(1.0,1.0,1.0,1.0);gluOrtho2D(-210,210,-...
#include <GL/glut.h>
void init(void)
{
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(-210,210,-210,210);
}
void plot_cicrle_points(int xc,int yc,int x,int y)
{
glBegin(GL_POINTS);
glVertex2d(xc+x,yc+y);
glVertex2d(xc-x,yc+y);
glVertex2d(xc+x,yc-y);
glVertex2d(xc-x,yc-y);
glVertex2d(xc+y,yc+x);
glVertex2d(xc-y,yc+x);
glVertex2d(xc+y,yc-x);
glVertex2d(xc-y,yc-x);
glEnd();
}
void circle(int xc,int yc,int radius)
{
int x,y,p;
x=0;
y=radius;
p=3-2*radius;
while(x<y){
plot_cicrle_points(xc,yc,x,y);
if(p<0)p=p+4*x+6;
else{
p=p+4*(x-y)+10;
y=y-1;
}
x=x+1;
}
if(x==y)plot_cicrle_points(xc,yc,x,y);
}
void display1(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
circle(0,0,100);
glFlush();
}
int abs(int x)
{
int y;
if(x<=0)
y=-x;
else
y=x;
return y;
}
void set_pixel(float x,float y)
{
glPointSize(3);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void dda_line(int xa,int ya,int xb,int yb)
{
float delta_x,delta_y,x,y;
int dx,dy,steps,k;
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
delta_x=(float)dx/(float)steps;
delta_y=(float)dy/(float)steps;
x=xa;
y=ya;
set_pixel(x,y);
for(k=1;k<=steps;k++){
x+=delta_x;
y+=delta_y;
set_pixel(x,y);
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
dda_line(5,5,50,10500);
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(521,521);
glutInitWindowPosition(100,100);
glutCreateWindow("Bresenham画圆算法");
init();
glutDisplayFunc(display1);
glutMainLoop();
return 0;
glutCreateWindow("生成直线的DDA算法");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
} 展开
void init(void)
{
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(-210,210,-210,210);
}
void plot_cicrle_points(int xc,int yc,int x,int y)
{
glBegin(GL_POINTS);
glVertex2d(xc+x,yc+y);
glVertex2d(xc-x,yc+y);
glVertex2d(xc+x,yc-y);
glVertex2d(xc-x,yc-y);
glVertex2d(xc+y,yc+x);
glVertex2d(xc-y,yc+x);
glVertex2d(xc+y,yc-x);
glVertex2d(xc-y,yc-x);
glEnd();
}
void circle(int xc,int yc,int radius)
{
int x,y,p;
x=0;
y=radius;
p=3-2*radius;
while(x<y){
plot_cicrle_points(xc,yc,x,y);
if(p<0)p=p+4*x+6;
else{
p=p+4*(x-y)+10;
y=y-1;
}
x=x+1;
}
if(x==y)plot_cicrle_points(xc,yc,x,y);
}
void display1(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
circle(0,0,100);
glFlush();
}
int abs(int x)
{
int y;
if(x<=0)
y=-x;
else
y=x;
return y;
}
void set_pixel(float x,float y)
{
glPointSize(3);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void dda_line(int xa,int ya,int xb,int yb)
{
float delta_x,delta_y,x,y;
int dx,dy,steps,k;
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
delta_x=(float)dx/(float)steps;
delta_y=(float)dy/(float)steps;
x=xa;
y=ya;
set_pixel(x,y);
for(k=1;k<=steps;k++){
x+=delta_x;
y+=delta_y;
set_pixel(x,y);
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
dda_line(5,5,50,10500);
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(521,521);
glutInitWindowPosition(100,100);
glutCreateWindow("Bresenham画圆算法");
init();
glutDisplayFunc(display1);
glutMainLoop();
return 0;
glutCreateWindow("生成直线的DDA算法");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询