Bresenham algorithm (1) code implementation

1. Code:

#include <GL/glut.h> 
#include<math.h>
#include<iostream>
using namespace std;
float x1,y11, x2, y2;
void Bresenhamline(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	float x, y,
	dx, dy;
	x = x1;
	y = y11;
  	int i;
	float k, e;
	dx = x2 - x1;
	dy = y2 - y11;
	k = dy / dx;
	e = -0.5for (i = 0;i<=dx; i++)
	{
		glBegin(GL_POINTS);
		glColor3f(2, 1, 1);
		glVertex2f(x / 1000, y / 1000);
		glEnd();
		x = x + 1;
		e = e + k;
		if (e>=0)
		{
			y++, e = e - 1;
		}
	}
		glFlush();
}
int main(int argc, char** argv)
 {
	glutInit(&argc, argv); //初始化glut
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
	//设置窗口的模式-深度缓存,单缓存,颜色模型
	glutInitWindowPosition(200, 200); //设置窗口的位置
	glutInitWindowSize(400, 400); //设置窗口的大小
	glutCreateWindow("3D Tech-GLUT Tutorial"); //创建窗口并赋予title
	cout << "输入起始点坐标,终点坐标" << endl;
	cin >> x1 >> y11 >> x2 >> y2;
	glutDisplayFunc(Bresenhamline);//调用renderScene把绘制传送到窗口
	glutMainLoop(); //进入循环等待
}

2. Screenshot:
Insert picture description here

Published 16 original articles · Like1 · Visits 180

Guess you like

Origin blog.csdn.net/weixin_44931542/article/details/105140552