-
Notifications
You must be signed in to change notification settings - Fork 0
/
dda_n_lines.c
85 lines (67 loc) · 1.6 KB
/
dda_n_lines.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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);
// else
// return 0;
}
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, 3);
x = x + xinc;
y = y + yinc;
i = i + 1;
}
return 0;
}
int main()
{
int gd=DETECT,gm,i=1,n;
float x1,y1,x2,y2;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf(" ********** Line Plotting Program ********\n");
printf(" DDA Line ALGORITHM\n");
printf("Enter Number of Lines"); //Eg: 6
scanf("%d",&n);
// To create a star put no of lines = 6 and enter follwoing values of (x1,y1) and (x2,y2)
// 200 400 250 300
// 250 300 300 400
// 200 400 300 400
// 200 325 300 325
// 300 325 250 425
// 200 325 250 425
for(i=1;i<=n;i++)
{
printf("Enter values of Start and End points (x1,y1) and (x2,y2):\n");
scanf("%f %f %f %f",&x1,&y1,&x2,&y2);
DDA(x1,y1,x2,y2);
}
printf("---------------------------------------------------------");
getch(); // If this line is giving error remove it.
closegraph();
return 0;
}