图形学实验一:Bresenham 画直线算法

Bresenham's画线算法作图如下:


给定两个点起点P1(x1, y1), P2(x2, y2),如何画它们直连的直线呢,即是如何得到上图所示的蓝色的点。假设直线的斜率0<k>0,直线在第一象限,Bresenham算法的过程如下:
1.画起点(x1, y1).
2.准备画下一个点,X座标加1,判断如果达到终点,则完成。否则找下一个点,由图可知要画的点要么为当前点的右邻接点,要么是当前点的右上邻接点。
  2.1.如果线段ax+by+c=0与x=x1+1的交点y座标大于(y+*y+1))/2则选右上那个点
  2.2.否则选右下那个点。
3.画点
4.跳回第2步
5.结束

主要代码:

// ====== Computer Graphics Experiment #1 ======
// |     Bresenham's Line Drawing Algorithm    |
// |                                           |
// =============================================
//
// Requirement:
// Implement a general Bresenham's Line Drawing Algorithm.
// Only use GL_POINTS drawing mode.
// Do not use OpenGL line drawing capability.


#include <windows.h>
#include <GL/glut.h>
#include <math.h>

#define PI 3.14159265359
int width, height;

void SetPixel(int x, int y)		// 填充一个像素
{
	glBegin(GL_POINTS);
		glVertex2i(x, y);
	glEnd();
}

// Bresenham line algorithm
void MyLine(int xs, int ys, int xe, int ye)
{
	// Write your code here
	//方法一
	//glBegin(GL_LINES);
    //glVertex2f(xs, ys);
    //glVertex2f(xe, ye);
    //glEnd();
    //方法二
    int x = xs;
	int y = ys;
	int dx = abs(xe - xs);
	int dy = abs(ye - ys);
	int s1 = xe > xs ? 1 : -1;
	int s2 = ye > ys ? 1 : -1;

	bool interchange = false;	// 默认不互换 dx、dy
	if (dy > dx)				// 当斜率大于 1 时,dx、dy 互换
	{
		int temp = dx;
		dx = dy;
		dy = temp;
		interchange = true;
	}

	int e = 2 * dy - dx;
	for(int i = 0; i <= dx; i++)
	{
		SetPixel(x, y);	// SetPixel
		if (e >= 0)
		{
			if (!interchange)		// 当斜率 < 1 时,选取上下象素点
				y += s2;
			else					// 当斜率 > 1 时,选取左右象素点
				x += s1;
			e -= 2 * dx;
		}
		if (!interchange)
			x += s1;				// 当斜率 < 1 时,选取 x 为步长
		else
			y += s2;				// 当斜率 > 1 时,选取 y 为步长
		e += 2 * dy;
	}
}

// Initialization function
void init(void)
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
}

// Display callback function
void display(void)
{
	int i, x0, y0, x, y;
	double a;

	glClear (GL_COLOR_BUFFER_BIT);

	x0=width/2;
	y0=height/2;

	glColor3f(1.0, 1.0, 1.0);

	// Draw lines
	for (i=0; i<360; i+=15)
	{
		a=(double)i/180.0*PI;
		x=0.45*width*cos(a);
		y=0.45*height*sin(a);
		if (i==0 || i==180) y=0;
		if (i==90 || i==270) x=0;
		MyLine(x0, y0, x0+x, y0+y);
	}

	glFlush();
}

// Reshape callback function
void reshape(int w, int h)
{
	// Record width and height of program window
	width=w;
	height=h;

	// Set clipping window and viewport equal to
	// view area of program window
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D (0.0, w, 0.0, h);
}

// Keyboard callback function
void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
		case 27:
			exit(0);

	}
}

// Main program entrance
int main(int argc, char* argv[])
{
	// Create window
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutCreateWindow("Lines");

	// Initialization
	init();

	// Define callback functions
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutDisplayFunc(display);

	// Main program loop. (Event loop)
	glutMainLoop();

	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章