温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-25 12:52:20 来源:脚本之家 阅读:403 作者:陳紋欽 栏目:编程语言

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

#include <gl/glut.h>
#include <math.h>
 
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;
 
 
}

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI