Job Saarnee

# Program For drawing Dashed Line Using DDA Line Drawing Algorithm in C

DAA is a Line Drawing Algorithm that is used to Draw Line on the Basis of Value of Slop that m which is equal to dy/dx.
In this particular we are preparing one of the variation of DDA Algorithm where we are going to learn how to draw dashed line using DDA.

Let See the Algorithm for Drawing Dashed Line Using DDA Line Drawing Algorithm

Step 1: First read the two ends Point of line that is (x1, y1) and (x2, y2).
Step 2: Find out the Value of dy and dx using following formula
dy = y2 – y1
dx = x2 – x1
Step 3: Now we are going to compare the value of dx and dy in order to find out that m <= 1 or                 m > 1

if (dx > dy)
Step = dx
else
Step = dy

Step 4: Now we have to decide how much we have to increase in x direction or in y direction
IncX = dx / Step
IncY = dy / Step

Step 5: Now we are going to plot First Pixel of Line
x = x1
y = y1
Now Put Pixel at (x , y)
Make i = 1
Make m=0;

Step 6: While  i is less than equal to Step
// here is the code for drawing dashed line

if  m is less than 4
x = x + IncX
y = y + IncY
Now Put Pixel at (x , y)
m=m+1

otherwise
x = x + IncX
y = y + IncY
m=0

i = i +1 // This is for while Loop

Step 7: Stop

Here is Program for Drawing Line Using DDA Line Drawing Algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
int x1,y1,x2,y2,dx,dy,step,i=0,m=0;
float incx,incy;
/* request auto detection */
int gdriver = DETECT, gmode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

// Here is Step 1 of Algorithm

printf(“enter the starting coordinate of line”);
scanf(“%d %d”, &x1,&y1);
printf(“enter the End coordinate of line”);
scanf(“%d %d”, &x2,&y2);

// Here is Step 2 of Algorithm
dx=x2-x1;
dy=y2-y1;

// Here is Step 3 of Algorithm
if(dx>dy)
{
step=dx;
}
else
{
step=dy;
}

// Here is Step 4 of Algorithm
incx=dx/step;
incy=dy/step;

// Here is Step 5 of Algorithm
putpixel(x1,y1,15);
i=1;

// Here is Step 6 of Algorithm
while(i<=step)
{
if(m < 4)
{
x1=x1+incx;
y1=y1+incy;
m=m+1;
putpixel(x1,y1,15);

}

else
{
x1=x1+incx;
y1=y1+incy;
m=0;
}
i=i+1;
}
/* clean up */
getch();
closegraph();
return 0;
}

Shopping Cart

You cannot copy content of this page