Line Drawing using DDA Algorithm with patterns such as simple,dash,dash-dot,thick
Algorithm for DDA(Digital Differential Algorithm)
Algorithm for DDA(Digital Differential Algorithm)
- Read the lines and points (x1,y1) and (x2,y2) such that they are not equal.(If they are equal then plot those points and exit)
- D(x)= | x2-x1 and | D( y)=| y2-y1 |
- Find out the length (if D(x) >= D(y), then length equal to D(x)).
- D(x)=(x2-x1/length), D(x)=(x2-x1/length)
- x=x1+0.5(sign(D(x))) y=y1+0.5(sign(D(y))
- (Here sign function make algorithm work in all quadrants, if it returns -1, 0, 1 depending on whether its argument is less than zero, equal to zero or greater than zero)
- Plot int x,int y;
- i=1;
- while(i<=length)
- {
- x=x+D(x); y=y+D(y);
- plot int x,int y;
- i++;
- }
- STOP.
//program for DDA
#include <stdio .h="">
#include <dos .h="">
#include <graphics .h="">
void lineDDA(int, int, int, int);
void main()
{
int x1, y1, xn, yn;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc\\bgi");
printf("Enter the starting coordinates of line: ");
scanf("%d %d", &x1, &y1);
printf("Enter the ending coordinates of line: ");
scanf("%d %d", &xn, &yn);
lineDDA(x1, y1, xn, yn);
getch();
}
void lineDDA(int x1, int y1, int xn, int yn)
{
int dx, dy, m, i;
m = (yn-y1)/(xn-x1);
for (i=x1; i<=xn; i++)
{
if (m <= 1)
{
dx = 1;
dy = m * dx;
}
else
{
dy = 1;
dx = dy / m;
}
x1 = x1 + dx;
y1 = y1 + dy;
putpixel(x1, y1, RED);
delay(20);
}
}
</graphics></dos></stdio>
Output:
Comments
Post a Comment