-
Notifications
You must be signed in to change notification settings - Fork 0
/
1. DDA Line Drawing Algorithm.CPP
57 lines (44 loc) · 1.47 KB
/
1. DDA Line Drawing Algorithm.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/********************************************************************************
TITLE : PROGRAM TO IMPLEMENT DDA LINE ALGORITHM
********************************************************************************/
#include <stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
int main()
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i = 1;
int gdriver=DETECT,gmode; //Detects the graphics drivers automatically
initgraph(&gdriver,&gmode,""); //Initialize to graphics mode
printf("Enter the value of x1,y1:\n"); //First co-ordinate of the line
scanf("%f %f",&x1,&y1);
printf("Enter the value of x2,y2:\n"); //Second co-ordinate of the line
scanf("%f %f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(dx>=dy) //Decides whether to move along X-direction
step=dx;
else //Or Y-direction
step=dy;
dx=dx/step;
dy=dy/step;
x=x1; //Calculates the increment value for x
y=y1; //Calculates the increment value for y
//To plot the quadrants
line(0,240,639,240);
line(320,0,320,479);
outtextxy(295,243,"0,0");
setcolor(WHITE);
while(i<=step) //To plot the line
{
putpixel(x+320,240-y,12); //Plots the points
x=x+dx; //Incrementing x
y=y+dy; //Incrementing y
i=i+1;
}
getch(); //Pauses the Output Console until a key is pressed
closegraph(); //Closes the graphics mode
return 0;
}