-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBresenham_line_drawing_algorithm.cpp
123 lines (116 loc) · 1.9 KB
/
Bresenham_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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Coder: Fenil Milankumar Parmar
Information & Technology
Vishwakarma Goverment Engineering College
*/
#include<graphics.h>
#include<iostream>
#include<cmath>
using namespace std;
main()
{
float x1,x2,y2,y1;
int xp,yp;
cout<<"Enter x1= ";
cin>>x1;
cout<<"Enter y1= ";
cin>>y1;
cout<<"Enter X2= ";
cin>>x2;
cout<<"Enter y2= ";
cin>>y2;
if(x2<x1 && y2<y1)
{
int tmp1,tmp2;
tmp1=x1;
x1=x2;
x2=tmp1;
tmp2=y1;
y1=y2;
y2=tmp2;
cout<<"x1 ="<<x1<<" y1= "<<y1<<" x2= "<<x2<<" y2= "<<y2<<endl;
}
if(x1==x2 && y1==y2)
{
putpixel(x1,x2,15);
cout<<"This can not become line!";
return 0;
getch();
}
float m,x,y,dx,dy;
cout<<"x1="<<x1<<" y1= "<<y1<<" x2= "<<x2<<" y2= "<<y2;
m=(float)(y2-y1)/(x2-x1);
cout<<"m = "<<m;
xp=x1;
yp=y1;
dy=y2-y1;
dx=x2-x1;
initwindow(800,800,"BRESENHAM ALGORITHM");
int po=2*dy-dx;
if((x2-x1)>0 && (y2-y1)>0 )
{
if(m<=1)
{
while(xp!=x2 && yp!=y2)
{
if(po<0)
{
po=po+2*dy;
x=x1+1;
y=y1;
xp=round(x);
yp=round(y);
cout<<"x="<<xp<<"y="<<yp;
x1=x;
y1=y;
putpixel(xp,yp,15);
}
else if(po>=0)
{
po=po+2*dy-2*dx;
x=x1+1;
y=y1+1;
xp=round(x);
yp=round(y);
cout<<"x="<<xp<<"y="<<yp;
x1=x;
y1=y;
putpixel(xp,yp,15);
}
cout<<endl;
}
}
if(m>1)
{
while(xp!=x2 && yp!=y2)
{
if(po<0)
{
po=po+2*dx;
x=x1+1;
y=y1;
xp=round(x);
yp=round(y);
cout<<"x="<<xp<<"y="<<yp;
x1=x;
y1=y;
putpixel(xp,yp,15);
}
else if(po>=0)
{
po=po+2*dx-2*dy;
x=x1+1;
y=y1+1;
xp=round(x);
yp=round(y);
cout<<"x="<<xp<<"y="<<yp;
x1=x;
y1=y;
putpixel(xp,yp,15);
}
cout<<endl;
}
}
}
getch();
}