Bresenham’s line algorithm is an algorithm which determines which order to form a close approximation to a straight line between two given points. Write a C program for determining pixel activation list between two given points in order to draw line segment using bresenham’s Line drawing algorithm?

**Aim: **To implement Bresenham’s line drawing algorithm for drawing a line segment between two

given endpoints A (x1, y2) and B(x2, y2).

### Description:

**Basic Concept:**

- Move across the
*x *axis in unit intervals and at each step choose between two different *y *coordinates

- For example, from position (2, 3) we have to choose between (3, 3) and (3, 4). We would like the point that is closer to the original line
- So we have to take decision to choose next So next pixels are selected based on the value of decision parameter p. The equations are given in below algorithm.

### Algorithm:

BRESENHAM’S LINE DRAWING ALGORITHM

- Input the two line end-points, storing the left end-point in (
*x*_{0}, y_{0})
- Plot the point (
*x*_{0}, y_{0})
- Calculate the constants Δ
*x*, Δ*y*, 2Δ*y*, and (2Δ*y - *2Δ*x*) and get the first value for the decision parameter as:

* p *= 2 D *y *- D *x*

- At each
*x*_{k} along the line, starting at *k = 0*, perform the following If *p*_{k} < 0, the next point to plot is *(x*_{k}+1, y_{k} ) and:

* p **k *+ 1 =*p *+ 2 D *y*

Otherwise, the next point to plot is (*x*_{k}+1, y_{k}+1) and:

* p **k *+ 1 =*p k *+ 2 D *y *- 2 D *x*

*5. Repeat step 4 (Δx – 1) times*

**NOTE: The **algorithm and derivation above assumes slopes are less than 1. For other slopes we need to adjust the algorithm slight

### Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int x,y,x1,y1,x2,y2,p,dx,dy;

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("\nEnter the x-coordinate of the first point ::"); scanf("%d",&x1);

printf("\nEnter the y-coordinate of the first point ::"); scanf("%d",&y1);

printf("\nEnter the x-coordinate of the second point ::"); scanf("%d",&x2);

printf("\nEnter the y-coordinate of the second point ::"); scanf("%d",&y2);

x=x1; y=y1; dx=x2-x1; dy=y2-y1;

putpixel(x,y,2);

p=(2*dy-dx); while(x<=x2)

{

if(p<0)

{

}

else

{

x=x+1; p=p+2*dy;

x=x+1; y=y+1;

p=p+(2*dy)-(2*dx);

}

putpixel(x,y,7);

}

getch(); closegraph();

}

### Output: