Program to Simulate DDA ALGORITHM FOR DRAWING LINE.
AIM:
To write a C program for draw a line using DDA line Algorithm.
ALGORITHM:
1. Start the program.
2. Enter the starting and ending point of the line.
3. Call the initgraph() function.
4. Invoke the function draw, and calculate the absolute value of dx and dy and check if abs(dx)>abs(dy).
5. If true assign step size as abs(dx), or else assign as abs(dy).
6. Calculate the x in c, y in c and plot the start point use the loop, until k is less than or equal to step size.
7. Calculate the x and y for each steps and plot the corresponding pixels and let the line be displayed.
8. Stop the graphics driver.
9. Stop the program.
Program:
- C
- C++
- Java
- Python
- C#
- PHP
- Javascript
/*-----------------------------------------------------------------------------------------
Cyclic Redundancy Check(CRC) Error detection for noisy channel Program Using C++
-----------------------------------------------------------------------------------------*/
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <ctype.h>
# include <math.h>
# include <stdlib.h>
void draw(int x1,int y1,int x2,int y2);
int main(void)
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
printf("\n Enter the x and y value for starting point:\n");
scanf("%d%d",&x1,&y1);
printf("\n Enter the x and y value for ending point:\n");
scanf("%d%d",&x2,&y2);
clrscr();
initgraph(&gdriver,&gmode,"E:\\TC\\BGI\\");
draw(x1,y1,x2,y2);
getch();
return 0;
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k,step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(abs(x),abs(y),111);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(abs(x),abs(y),111);
}
}
|
Output
Enter the x and y value for starting point: 50 60
Enter the x and y value for ending point: 70 90
RESULT:
Thus the program in C to draw the line by using DDA algorithm was written and executed successfully.