0% found this document useful (0 votes)
49 views

3.implement Following Line Drawing Algorithms (With Mouse) : DDA Algorithm

This document contains code to implement the Digital Differential Analyzer (DDA) line drawing algorithm in C++. It includes functions to get user input for the start and end points of a line, as well as the color. The DDA algorithm calculates the change in x and y, number of steps, and incremental changes per step to lay down pixels between the points. It uses a for loop to iterate through laying down pixels using the incremental changes until the end point is reached.

Uploaded by

Neha Chindaliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

3.implement Following Line Drawing Algorithms (With Mouse) : DDA Algorithm

This document contains code to implement the Digital Differential Analyzer (DDA) line drawing algorithm in C++. It includes functions to get user input for the start and end points of a line, as well as the color. The DDA algorithm calculates the change in x and y, number of steps, and incremental changes per step to lay down pixels between the points. It uses a for loop to iterate through laying down pixels using the incremental changes until the end point is reached.

Uploaded by

Neha Chindaliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

3.

Implement following Line drawing Algorithms (with mouse): DDA


Algorithm.
# include <graphics.h>
# include <math.h>
# include <conio.h>
voidDDALine(int x1,int y1,int x2,int y2,int iColor);
void main()
{
Int gDriver=DETECT,gMode,x1,x2,y1,y2,iColor;
initgraph(&gDriver,&gMode,"");
cleardevice();
printf("\n Enter x1 : ");
scanf("%d",&x1);
printf("\n Enter x2 : ");
scanf("%d",&x2);
printf("\n Enter y1 : ");
scanf("%d",&y1);
printf("\n Enter y2 : ");
scanf("%d",&y2);
printf("\n Enter color : ");
scanf("%d",&iColor);
cleardevice();
DDALine(320,1,320,480,12);
DDALine(1,240,640,240,12);
circle(320,240,2);
DDALine(320+x1,240-y1,320+x2,240-y2,iColor%16);
getch();
}
voidDDALine(int x1,int y1,int x2,int y2,int iColor)
{
floatdX,dY,iSteps;
floatxInc,yInc,iCount,x,y;
dX = x1 - x2;
dY = y1 - y2;
if (fabs(dX) >fabs(dY))
{
iSteps = fabs(dX);

}
else
{
iSteps = fabs(dY);
}
xInc = dX/iSteps;
yInc = dY/iSteps;
x = x1;
y = y1;
circle(x,y,1);
for (iCount=1; iCount<=iSteps; iCount++)
{
putpixel(floor(x),floor(y),iColor);
x -= xInc;
y -= yInc;
}
circle(x,y,1);
return 0;
}

You might also like