您的位置:首页 > 运维架构

用OpenGL实现 Breseham画圆算法

2013-11-01 20:24 197 查看
#include <GL/glut.h>
#include <math.h>
#include <iostream>

using namespace std;

int xs = 0;
int ys = 0;
int r = 0;

void BresenhamCircle(int x0, int y0, int r)
{
int x = 0;
int y = r;
int d = 2 * (1 -r);
int d1 = 0;
int d2 = 0;
int direction;
while (y >= 0)
{
glVertex2i(x0+x, y0+y);
glVertex2i(x0+x, y0-y);
glVertex2i(x0-x, y0-y);
glVertex2i(x0-x, y0+y);
if (d < 0)
{
d1 = 2 *(d + y) - 1;
if (d1 < 0)
{
direction = 1;
}
else
{
direction = 2;
}
}
else if (d > 0)
{
d2 = 2 * (d - x) -1;
if (d < 0)
{
direction = 2;
}
else
{
direction = 3;
}
}
else
direction = 2;
switch (direction)
{
case 1:
x++;
d += 2 * x + 1;
break;
case 2:
x++;
y--;
d += 2 * (x - y + 1);
break;
case 3:
y--;
d += (-2 * y + 1);
break;
}
}
}

void CircleSegment()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POINTS);
BresenhamCircle(xs, ys, r);
glEnd();
glFlush();
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
cout << "输入圆心坐标和半径(范围为 0 - 500, 0-500):";
cin >> xs >> ys >> r;
glutInitWindowPosition(50, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("Breseham画圆算法");
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 500, 0.0, 500.0);
glutDisplayFunc(CircleSegment);
glutMainLoop();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opengl c++ 算法