#include<freeglut.h>
#include<cmath>
typedef GLint Vertex3[3];
GLint winwidth = 300, winheight = 300;
void reshapeFcn(GLint newwidth,GLint newheight)
{
glViewport(0, 0, newwidth, newheight);
winwidth = newwidth;
winheight = newheight;
}
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(0, winwidth, 0, winheight);
}
void test()
{
int x0 = 10, y0 = 15, xE = 100, yE = 145; //(x0,y0) (xE,yE);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0,0);
int dx = fabs(xE - x0), dy = fabs(yE - y0);
int twody = 2 * dy;
int twod = twody - 2 * dx;
int p = twody - dx;
int x, y;
if (x0 > xE)
{
x = xE;
y = yE;
xE = x0;
}
else
{
x = x0; y = y0;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
while (x < xE)
{
x++;
if (p < 0)
{
p = p + twody;
}
else
{
p = p + twod;
y++;
}
glVertex2i(x, y);
}
glEnd();
glFlush();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winwidth, winheight);
glutCreateWindow("nbb");
init();
glutReshapeFunc(reshapeFcn);
glutDisplayFunc(test);
glutMainLoop();
}
Bresenham画线算法简单实线
最新推荐文章于 2021-07-28 15:17:31 发布