Computer Graphics (2) - Experiment 2: Algorithm for Generating Straight Lines

Experiment 2: Algorithm for generating straight lines

2.1 Experimental purpose

(1) Understand the basic principles of rasterization of basic graphic elements
(2) Master a basic graphic element rasterization algorithm
(3) Understand DDA algorithm, midpoint line method, Bresenham algorithm
(4) Master the usage of CDC class in VC++

2.2 Experiment content

(1) Writing the class
(2) Complete the DDA algorithm, the midpoint line method, and the Bresenham algorithm

2.3 Algorithm ideas

Numerical differentiation method (DDA method): First determine whether to step in the X direction or the Y direction according to the slope of the straight line, and then step along the step direction for each point (pixel), along another coordinate variable k , k is the slope of the straight line, because it is output to the dot matrix device, it is necessary to round off a pair of coordinates calculated each time.
Bresenham's algorithm: Connect the pixel centers of each row and column of the grating device to construct a set of virtual grid lines. Calculate the intersection of the straight line and each vertical grid line in the order from the start point to the end point of the straight line, and then determine the pixel closest to the intersection in the column of pixels.
Midpoint line drawing algorithm: Assuming that the slope k of the straight line is between 0 and 1, and the current pixel point is (xp, yp), there are two options for the next pixel point: P1 (xp+1, yp) or P2 ( xp+1, yp+1). If the midpoint (xp+1, yp+0.5) of P1 and P2 is called M, Q is the intersection of the ideal line and the vertical line of x=xp+1. When M is below Q, take P2 as the next pixel; when M is above Q, take P1 as the next pixel.

2.4 Flowchart

(1) Numerical differentiation method (DDA method):
insert image description here(2) Midpoint line algorithm:
insert image description here(3) Bresenham algorithm:
insert image description here

2.5 Experimental steps

(1) Numerical differentiation method (DDA method):
Draw a point (x, y) from the starting point toward the end point, and walk a unit length on the x-axis or y-axis (along the x-axis or the y-axis depends on the inclination angle of the line) , determine the increment of another coordinate by the inclination of the straight line (slope or the inverse of the slope), obtain the coordinate of the next point, round x or y to get (x, y), if (x, y) is not the end point, continue .
(2) Midpoint line drawing algorithm:
draw a point (x, y) from the starting point to the end point, and the current pixel point is (xp, yp), then there are two options for the next pixel point P1 (xp+1 , yp) or P2 (xp+1, yp+1), if the midpoint of P1 and P2 (xp+1, yp+0.5) is called M, and Q is the intersection of the ideal line and the vertical line of x=xp+1. When M is below Q, then take P2 as the next pixel; when M is above Q, then take P1 as the next pixel, get (x, y), if (x, y) If it is not the end, continue.
(3) Bresenham's algorithm:
Draw a point (x, y) from the starting point towards the end point. Prepare to draw the next point, add 1 to the X coordinate, and judge if the end point is reached, then complete, otherwise find the next point. The point to be drawn is either the right neighbor of the current point, or the upper-right neighbor of the current point. If the y-coordinate of the intersection of the line segment ax+by+c=0 and x=x1+1 is greater than (y+*y+1))/2, select the upper right point, otherwise select the lower right point to get (x, y).

2.6 Experimental code

(1) Numerical differentiation method (DDA method):

/
//DDA直线生成算法
/
void CLiHuchenView::OnDdaline() 
{
    
    
	// TODO: Add your command handler code here
	CDC *pDC=GetDC();//获取设备指针
	int xa=100,ya=300,xb=300,yb=200,c=RGB(255,0,0);//定义直线的两端点,直线颜色红色
	int x,y;//定义变量x,y
	float dx,dy,k;//定义增量dx,dy和斜率k
	dx=(float)(xb-xa),dy=(float)(yb-ya);//直线两端点之差为增量
	k=dy/dx;//直线斜率
	y=ya;//将第一个点纵坐标赋给y
	//k值判断
	if(abs(k)<1)
	{
    
    
		for(x=xa;x<=xb;x++)//从x左端向右端点步进
		{
    
    
			pDC->SetPixel(x,int(y+0.5),c);//添加光栅点
			y=y+k;//y增加k的单位
		}
	}
	if(abs(k)>=1)
	{
    
    
		for(y=ya;y<=yb;y++)
		{
    
    
			pDC->SetPixel(int(x+0.5),y,c);//添加光栅点
			x=x+1/k;//x增加1/k的单位
		}
	}
	ReleaseDC(pDC);//指针释放
}

(2) Midpoint line drawing algorithm:

/
//中点直线生成算法
/
void CLiHuchenView::OnMidpointline() 
{
    
    
	// TODO: Add your command handler code here
	CDC *pDC=GetDC();//获取设备指针
	int xa=300,ya=200,xb=450,yb=300,c=RGB(0,255,0);//定义直线的两端点,直线颜色绿色
	float a,b,d1,d2,d,x,y;//定义直线方程系数a,b,中点带入直线的值d,增量d1,d2,变量x,y
	a=ya-yb;b=xb-xa;d=2*a+b;//计算a,b,d
	d1=2*a;d2=2*(a+b);计算增量d1,d2
	x=xa,y=ya;//赋初值
	pDC->SetPixel(x,y,c);添加光栅点
	while(x<xb)
	{
    
    
		//判断d
		if(d<0)
		{
    
    
			x++,y++,d+=d2;
		}
		else
		{
    
    
			x++,d+=d1;
		}
		pDC->SetPixel(x,y,c);添加光栅点
	}
	ReleaseDC(pDC);//指针释放
}

(3) Bresenham algorithm:

/
//Bresenham直线生成算法
/
void CLiHuchenView::OnBresenhamline() 
{
    
    
	// TODO: Add your command handler code here
	CDC *pDC=GetDC();//获取设备指针
	int x1=100,y1=200,x2=300,y2=100,c=RGB(0,0,255);//定义直线的两端点,直线颜色蓝色
	int i,s1,s2,interchange;//定义变量
	float x,y,deltax,deltay,f,temp;//定义变量x,y;增量dx,dy
	x=x1;y=y1;//赋初值
	deltax=abs(x2-x1);deltay=abs(y2-y1);//直线两端点之差为增量
	if(x2-x1>=0){
    
     s1=1;}else {
    
    s1=-1;}//判断s1正负前进
	if(y2-y1>=0) {
    
    s2=1;}else {
    
    s2=-1;}//判断s2正负前进
	//判断dy和dx的大小
	if(deltay>deltax)
	{
    
    
		//交换dy和dx
		temp=deltax;
		deltax=deltay;
		deltay=temp;
		interchange=1;//设置交换
	}
	else {
    
    interchange=0;}
	f=2*deltay-deltax;//计算误差初值
	pDC->SetPixel(x,y,c);添加光栅点
	for(i=1;i<=deltax;i++)
	{
    
    
		if(f>=0)
		{
    
    
			if(interchange==1) x+=s1;
			else y+=s2;
			pDC->SetPixel(x,y,c);添加光栅点
			f=f-2*deltax;
		}
		else
		{
    
    
			if(interchange==1)  y+=s2;
			else x+=s1;
			f=f+2*deltay;
		}
	}
	ReleaseDC(pDC);//指针释放
}

2.7 Display of experimental results

insert image description hereRed: Numerical Differentiation Method (DDA Method)
Green: Midpoint Line Algorithm
Blue: Bresenham's Algorithm

Guess you like

Origin blog.csdn.net/chengzilhc/article/details/106728648