Sei sulla pagina 1di 3

4

EXPERIMENT:-2.
AIM:- TO STUDY ABOUT DDA LINE ALGORITHM.
PROGRAME:-
// C program for DDA line generation
#include<stdio.h>
#include<graphics.h>

//Function for finding absolute value


int abs (int n)
{
return ( (n>0) ? n : ( n * (-1)));
}

//DDA Function for line generation


void DDA(int X0, int Y0, int X1, int Y1)
{
char a[10],b[10];
sprintf(a,"(%d,%d)",X0,Y0);
outtextxy(X0-10,Y0-20,a);
sprintf(b,"(%d,%d)",X1,Y1);
outtextxy(X1+8,Y1+10,b);
// calculate dx & dy
int dx = X1 - X0;
int dy = Y1 - Y0;
// calculate steps required for generating pixels
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

// calculate increment in x & y for each steps


float Xinc = dx / (float) steps;
float Yinc = dy / (float) steps;

// Put pixel for each step


float X = X0;
float Y = Y0;
settextstyle(1,0,2);
outtext("DEEPANSHU RANJAN");
moveto(0,20);
if(dx<=dy)
{
if(dx<dy){
outtext("LINE IS DRAWING FOR m>1");
printf("\nLINE IS DRAWING FOR m>1");
}
else{
outtext("LINE IS DRAWING FOR m=1");
printf("\nLINE IS DRAWING FOR m=1");
DEEPANSHU RANJAN
17CE011
5
}
}
else{
outtext("LINE IS DRAWING FOR m<1");
printf("\nLINE IS DRAWING FOR m<1");
}
for (int i = 0; i <= steps; i++)
{
putpixel (X,Y,WHITE); // put pixel at (X,Y)
X += Xinc; // increment in x at each step
Y += Yinc; // increment in y at each step
delay(100); // for visualization of line-
// generation step by step
}
}

// Driver program
int main()
{
int gd = DETECT, gm;
int x0,y0,x1,y1;
// Initialize graphics function
initgraph (&gd, &gm, "");
printf("enter starting point\nx coordinate : ");
scanf("%d" ,&x0);
printf("\n ycoordinate : ");
scanf("%d" ,&y0);
printf("enter ending point\nx coordinate : ");
scanf("%d" ,&x1);
printf("\n ycoordinate : ");
scanf("%d" ,&y1);
DDA(x0,y0,x1,y1);
getch();
}
OUTPUT:- for m>1

DEEPANSHU RANJAN
17CE011
6
For m<1

For m=1

For horizontal line

For vertical line:

DEEPANSHU RANJAN
17CE011

Potrebbero piacerti anche