OpenGL通过中点法绘制直线和圆

本文实例为大家分享了OpenGL绘制直线和圆的具体代码,供大家参考,具体内容如下

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了临邑免费建站欢迎大家使用!

#include 
#include 
 
static int i=1;
 
void Initial(void)
{
 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //设置窗口背景颜色为白色
 glMatrixMode(GL_PROJECTION); //指定设置投影参数
 gluOrtho2D(-200.0,200.0,-200.0,200.0); //设置投影参数 //指的是视图范围,如果(0,200,0,200)的话就只能在第一象限显示,就1/4圆,第1,3的参数是左下角坐标,第2,4的参数是右上角坐标
}
/*void Display(void)
{
 glClear(GL_COLOR_BUFFER_BIT); //用当前背景色填充窗口
 glColor3f(1.0f, 0.0f, 0.0f);  //设置当前的绘图颜色为红色
 glRectf(50.0f, 100.0f, 150.0f, 50.0f); //绘制一个矩形
  glFlush();    //处理所有的OpenGL程序
}
*/
 
void DDALine(int x0,int y0,int x1,int y1)
{
 //glVertex2f(10,10);
 int dx,dy,epsl,k;
 float x,y,xIncre,yIncre;
 dx = x1 - x0;
 dy = y1 - y0;
 x = x0;
 y = y0;
 
 if(abs(dx)>abs(dy))
 epsl=abs(dx);
 else
 epsl=abs(dy);
 
 xIncre=(float)dx/(float)epsl;
 yIncre=(float)dy/(float)epsl;
 glPointSize(2);
 glBegin(GL_POINTS);
 for(k = 0;k <= epsl; k++)
 {
 glColor3f(1.0f, 0.0f, 0.0f);
 glVertex2f(int(x+0.5),int(y+0.5));
 x += xIncre;
 y += yIncre;
 }
 glEnd();
 
}
 
 
 
void Mid_B(int x0,int y0,int x1,int y1)
{
 int dx,dy,d,UpIncre,DownIncre,x,y;
 if(x0 > x1)
 {
 x = x1;
 x1 = x0;
 x0 = x;
 y = y1;
 y1 = y0;
 y0 = y;
 }
 x = x0;
 y = y0;
 dx = x1 - x0;
 dy = y1 - y0;
 d = dx - 2*dy;
 UpIncre = 2*dx - 2*dy;
 DownIncre =- 2*dy;
 
 glPointSize(2);
 glBegin(GL_POINTS);
 while(x <= x1)
 {
 glColor3f(1.0f, 0.0f, 0.0f);
 glVertex2f(x,y);
 x++;
 if(d < 0)
 {
 y++;
 d += UpIncre;
 }
 else
 d += DownIncre;
 }
 glEnd();
 
}
 
void G_B(int x0,int y0,int x1,int y1)
{
 int x,y,dx,dy,e;
 dx = x1 - x0;
 dy = y1 - y0;
 e =- dx;
 x=x0;
 y=y0;
 
 glPointSize(2);
 glBegin(GL_POINTS);
 while(x <= x1)
 {
 glColor3f(1.0f, 0.0f, 0.0f);
 glVertex2f(x,y);
 x++;
 e = e + 2*dy;
 if(e > 0)
 {
 y++;
 e = e - 2*dx;
 }
 
 }
 glEnd();
}
 
void CirclePoint(int x, int y)
{
 glPointSize(2);
 glBegin(GL_POINTS);
 glColor3f(1.0f, 0.0f, 0.0f);
 glVertex2f(x,y);
 glVertex2f(y,x); 
 glVertex2f(-y,x); 
 glVertex2f(-x,y); 
 glVertex2f(-x,-y); 
 glVertex2f(-y,-x); 
 glVertex2f(y,-x); 
 glVertex2f(x,-y);
 glEnd();
}
 
void MidBresenhamCircle(int r)
{
 int x = 0, y = r, d = 1-r;
 //glPointSize(2);
 //glBegin(GL_POINTS);
 while(x <= y)
 {
 CirclePoint(x,y);
 
 if(d < 0)
 d += 2*x+3;
 else
 {
 d += 2*(x-y)+5;
 y--;
 }
 x++;
 }
 glEnd();
}
 
void ProcessMenu(int value)
{
 i = value; 
 glutPostredisplay();
}
 
 
 
void Display(void)
{
 glClear(GL_COLOR_BUFFER_BIT); //用当前背景色填充窗口
 glViewport(0,0,400,400); //前两个参数改变原点坐标,后两个参数改变图形长宽(放大缩小)
 switch(i)
 {
 case 1:
 DDALine(2,3,55,83);
 break;
 case 2:
 Mid_B(2,3,55,83);
 break;
 case 3:
 G_B(2,3,55,83);
 break;
 case 4:
 MidBresenhamCircle(50);
 break;
 
 }
 glFlush(); 
}
 
int main(int argc, char* argv[])
 
{
 glutInit(&argc, argv); //初始化GLUT库,处理命令行参数 
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //初始化窗口的显示模式
 glutInitWindowSize(400,400);  //设置窗口的尺寸
 glutInitWindowPosition(100,100); //设置窗口的位置
 glutCreateWindow("直线");   //创建一个名为矩形的窗口
 
 int MainMenu = glutCreateMenu(ProcessMenu); //创建主菜单
 glutAddMenuEntry("DDA算法",1);
 glutAddMenuEntry("中点Bresenham算法",2);
 glutAddMenuEntry("改进的Bresenham算法",3);
 glutAddMenuEntry("中点bresenham画圆",4);
 glutAttachMenu(GLUT_RIGHT_BUTTON); 
 
 glutDisplayFunc(Display); //设置当前窗口的显示回调函数
 Initial();     //完成窗口初始化
 glutMainLoop();   //启动主GLUT事件处理循环
 return 0;
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网页题目:OpenGL通过中点法绘制直线和圆
标题链接:http://scjbc.cn/article/gjigsj.html

其他资讯