-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinedda.cpp
70 lines (60 loc) · 1.35 KB
/
linedda.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
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Plotting Lines with DDA line algorithm and Bresenham's method */
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
int round(float x)
{
return int(x+0.5);
}
void DDA(int x1,int y1, int x2, int y2,int ym, int xm)
{
float m ;
if(x2==x1){}
else{ m = float((y2-y1))/float((x2-x1));
}
if(abs(m)<=1)
{ float y =y1;
for(int x=x1;x<=x2;x++)
{
putpixel(x,ym-round(y),WHITE);
y+=m;
}
}
else
{
float x =x1;
if(y2>y1)
{
for(int y=y1;y<=y2;y++)
{
putpixel(round(x),ym-y,WHITE);
x+=1/m;
}
}
else if( y2<y1)
{
for(int y=y1;y>=y2;y--)
{
putpixel(-round(x),ym-y,WHITE);
x+=1/m;
cout<<x<<" "<<y<<"\n";
}
}
}
}
int main()
{
int x1,y1,x2,y2;
cout<<"Line Drawing Algorithms";
cout<<"\nEnter coordinates";
cin>>x1>>y1>>x2>>y2;
int gd = DETECT,gm;
cin>>ch;
initgraph(&gd,&gm,"");
int xm = getmaxx();
int ym = getmaxy();
DDA(x1,y1,x2,y2);
getch();
closegraph();
}