### Join Our Email Alerts-Subscribe Important Note:Login & Check Your Email Inbox and Activate Confirmation Link

Enter Your Email :

## Computer Graphics Lab Codings-BRESENHAM’S LINE ALGORITHM

Aim:

To write a C++ Program to draw a line using Bresenhams algorithm.

Algorithm:

1. Start the program .

2. Initialize the variables.

3. Call the initgraph() function

4. Get the values for left and right end points for drawing a line..

5. Calculate the distance to be traveled by the line.

6. Put the pixels in the given color to draw the line.

7. Display the output.

8. stop the program

PROGRAM

void main()

{

int gd=DETECT,gm,x0,y0,x1,y1,p,dx,dy,i,x2,y2,ec;

ec=graphresult();

if(ec!=grOk)

{

cout<<"Graphics error:%s\n",grapherrormsg(ec);

cout<<"press any key to halt";

getch();

exit(1);

}

initgraph(&gd,&gm,"");

cout<<"enter left end pt";

cin>>x0>>y0;

cout<<"Enter right end pts";

cin>>x1>>y1;

cleardevice();

dx=abs(x1-x0);

dy=(y1-y0);

if(dx
{

y2=dx;

dx=dy;

dy=y2;

x2=x0;

y2=y0;

}

else

{

x2=y0;

y2=x0;

} p=2*dx*dy;

putpixel(x2,y2,RED);

for(i=0;i<=x0;i++)

{

if(p<0)

{

y2=y2+1;

p=p+2*dy;

}

else

{

x1++;

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

}

putpixel(x1,y1,RED);

}

setbkcolor(GREEN);

getch();

return;

}

### Technicalsymposium.com-Download All Study Notes PDF ## Technicalsymposium.com-Free Live Support-Chat with Our Experts  