# Experiment 2: Algorithms for generating straight lines

## 2.1 Experimental Purpose

(1) Understanding the basic principles of rasterization of basic graphic elements
(2) Master a rasterization algorithm for basic graphic elements
(3) Understanding DDA algorithm, Midpoint Line Drawing method, Bresenham algorithm
(4) Master the usage of CDC class in VC++.

## 2.2 Experimental Contents

(1) Writing Classes
(2) Complete DDA algorithm, Midpoint Line Drawing method, Bresenham algorithm

## 2.3 algorithm ideas

Digital Differential Method (DDA): First, the slope of a straight line is used to determine whether to step in the X direction or the Y direction. Then, each point (pixel) in the step direction is followed by another coordinate variable, k, which is the slope of a straight line. Because it is output to a dot matrix device, a pair of coordinates calculated at a time needs to be rounded.
Bresenham algorithm: Connect the pixel centers of each row and column of the raster device to construct a set of virtual grid lines.The intersection of a straight line with each vertical grid line is calculated in the order from the beginning to the end of the line, and then the nearest pixel in the column pixel to the intersection is determined.
Midpoint Line Drawing Algorithm: Assuming that the slope of the line k is between 0 and 1 and the current pixel point is (xp,yp), there are two alternative points P1 (xp+1,yp) or P2 (xp+1,yp+1) for the next pixel point.If the midpoint of P1 and P2 (xp+1,yp+0.5) is called M, Q is the intersection of the ideal straight line and the perpendicular line x=xp+1.When M is below Q, take P2 as the next pixel point; when M is above Q, take P1 as the next pixel point.

## 2.4 Flowchart

(1) Numerical differentiation (DDA): (2) Midpoint drawing algorithm: (3) Bresenham algorithm: ## 2.5 EXPERIMENTAL STEPS

(1) Numerical differentiation (DDA):
Draw a dot (x, y) toward the end point from the start, take a unit length along the x-axis or y-axis (depending on the oblique angle of the line) and increment the other coordinate by the oblique degree of the line (slope or reciprocal of slope). Round X or y to get (x, y), and continue if (x, y) is not the end point.
(2) Midpoint drawing algorithm:
Draw a dot (x, y) toward the end point from the start, and the current pixel point is (xp,yp). Then there are two alternative points for the next pixel point, P1 (xp+1,yp+1), or P2 (xp+1,yp+1). If the midpoint of P1 and P2 (xp+1,yp+0.5) is called M, Q is the intersection of the ideal straight line and the perpendicular line x=xp+1.When M is below Q, take P2 as the next pixel point; when M is above Q, take P1 as the next pixel point to get (x, y), and continue if (x, y) is not the end point.
(3) Bresenham algorithm:
Draw a dot (x, y) from the start towards the end point.Prepare to draw the next point, X coordinate plus 1, judge if the end point is reached, then complete, otherwise find the next point.The point to be drawn is either the right adjacent point of the current point or the upper right adjacent point of the current point.If the y-coordinate of the intersection of 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 and get (x,y).

## 2.6 Experimental Code

(1) Numerical differentiation (DDA):

```/////////////////////////////////////////////////////////////////////////////
//DDA Line Generation Algorithm
/////////////////////////////////////////////////////////////////////////////
void CLiHuchenView::OnDdaline()
{
CDC *pDC=GetDC();//Get device pointer
int xa=100,ya=300,xb=300,yb=200,c=RGB(255,0,0);//Defines the ends of a line, the line color red
int x,y;//Define variables x,y
float dx,dy,k;//Define incremental dx, dy, and slope k
dx=(float)(xb-xa),dy=(float)(yb-ya);//Incremental difference between two endpoints of a line
k=dy/dx;//Slope
y=ya;//Assign the first point ordinate to y
//k-value judgment
if(abs(k)<1)
{
for(x=xa;x<=xb;x++)//Step from left to right of x
{
y=y+k;//y plus k units
}
}
if(abs(k)>=1)
{
for(y=ya;y<=yb;y++)
{
x=x+1/k;//x plus 1/k units
}
}
ReleaseDC(pDC);//Pointer release
}

```

(2) Midpoint drawing algorithm:

```/////////////////////////////////////////////////////////////////////////////
//Midpoint Line Generation Algorithm
/////////////////////////////////////////////////////////////////////////////
void CLiHuchenView::OnMidpointline()
{
CDC *pDC=GetDC();//Get device pointer
int xa=300,ya=200,xb=450,yb=300,c=RGB(0,255,0);//Defines the ends of a line with a green line color
float a,b,d1,d2,d,x,y;//Defines the values d, increment d1, d2, variable x, y for the coefficients a,b, and the midpoint to the straight line
a=ya-yb;b=xb-xa;d=2*a+b;//Calculate a, b, d
d1=2*a;d2=2*(a+b);////Calculate increments d1, d2
while(x<xb)
{
//Judgment d
if(d<0)
{
x++,y++,d+=d2;
}
else
{
x++,d+=d1;
}
}
ReleaseDC(pDC);//Pointer release
}

```

(3) Bresenham algorithm:

```/////////////////////////////////////////////////////////////////////////////
//Bresenham Line Generation Algorithm
/////////////////////////////////////////////////////////////////////////////
void CLiHuchenView::OnBresenhamline()
{
CDC *pDC=GetDC();//Get device pointer
int x1=100,y1=200,x2=300,y2=100,c=RGB(0,0,255);//Defines the ends of a line, straight line color blue
int i,s1,s2,interchange;//Define Variables
float x,y,deltax,deltay,f,temp;//Define variables x, y; incremental dx, dy
deltax=abs(x2-x1);deltay=abs(y2-y1);//Incremental difference between two endpoints of a line
if(x2-x1>=0){ s1=1;}else {s1=-1;}//Judging s1 Forward and Negative
if(y2-y1>=0) {s2=1;}else {s2=-1;}//Judging s2 forward or negative
//Judging the size of dy and dx
if(deltay>deltax)
{
//Exchange dy and dx
temp=deltax;
deltax=deltay;
deltay=temp;
interchange=1;//Set Exchange
}
else {interchange=0;}
f=2*deltay-deltax;//Calculating the Initial Error Value
for(i=1;i<=deltax;i++)
{
if(f>=0)
{
if(interchange==1) x+=s1;
else y+=s2;
f=f-2*deltax;
}
else
{
if(interchange==1)  y+=s2;
else x+=s1;
f=f+2*deltay;
}
}
ReleaseDC(pDC);//Pointer release
}

```

## 2.7 EXPERIMENTAL EXPERIMENTS DISPLAY Red: Numeric Differential Method (DDA)
Green: Midpoint Line Drawing Algorithm
Blue: Bresenham algorithm

Posted by Scifa on Fri, 12 Jun 2020 19:49:33 -0700