Bresenham algorithm (1)

Bresenham algorithm (1)

1. Bresenham's straight line algorithm (1)
Basic principle :
Bresenham's straight line algorithm is an algorithm that determines the points of the n-dimensional grating that should be selected so as to form an approximate approximation of the straight line between the two points. It is usually used to draw line primitives in bitmap images (for example on a computer screen) because it uses only integer addition, subtraction and displacement, all of which are very cheap operations in standard computer architectures. It is an incremental error algorithm. It is one of the earliest developed algorithms in the field of computer graphics

Insert picture description here
Consider the relationship between d2 and d1:

(1) If d2> = d1, the straight line is closer to the lower point

(2) If d2 <d1, the straight line is closer to the upper point

2. Bresenham's straight line algorithm (2)
Construct a set of virtual grid lines through the pixel centers of each row and column. Calculate the intersection of the straight line and each vertical grid line in the order of the straight line from the start point to the end point, and then determine the pixel closest to this intersection point in the column of pixels according to the sign of the error term.
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Algorithm :



void Bresenhamline (int x0,int y0,int x1, int y1,int color)
{  
	int x, y, dx, dy;
	float k, e;
	dx = x1-x0, dy = y1- y0,k=dy/dx; 
	e=-0.5, x=x0, y=y0;
	for (i=0; i£dx; i++)
	{    
		drawpixel (x, y, color);
		x=x+1,e=e+k;
		if (0)
		{ 
			y++, e=e-1;
		}
}
}

 

Published 16 original articles · Like1 · Visits 180

Guess you like

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