-
Notifications
You must be signed in to change notification settings - Fork 0
/
dda_single_line.c
66 lines (53 loc) · 1.29 KB
/
dda_single_line.c
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
58
59
60
61
62
63
64
65
66
// Implemented using Turbo C
// Steps to enable Graphics Library in Turbo C.
// Option -> Linkers -> Libraries -> Graphics Library
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int Sign(int diff)
{
if(diff < 0)
return (-1);
else
return (1);
}
int DDA(float x1, float y1, float x2, float y2)
{
int i=1;
float dx, dy, x, y, xinc, yinc, length;
dx = x2 - x1;
dy = y2 - y1;
if(abs(dx) >= abs(dy))
length = abs(dx);
else
length = abs(dy);
xinc = dx/length;
yinc = dy/length;
x = x1 + (0.5)*Sign(xinc);
y = y1 + (0.5)*Sign(yinc);
while(i<=length)
{
// Plot(Integer(x),Integer(y))
putpixel((int)x, (int)y,14);
x = x + xinc;
y = y + yinc;
i = i + 1;
}
return 0;
}
int main()
{
int gd=DETECT, gm;
float x1, y1, x2, y2;
initgraph(&gd, &gm, "C:\\TC\\BGI");
printf(" ********** DDA Line Plotting Program ********\n");
printf(" DDA Line ALGORITHM\n");
printf("Enter values of Start and End points (x1,y1) and (x2,y2):\n");
scanf("%f %f %f %f", &x1, &y1, &x2, &y2); // Eg: Put values 100 100 400 400
printf("-------------------------------------------------------------");
DDA(x1, y1, x2, y2);
getch(); // If this line is giving error remove it.
closegraph();
return 0;
}