forked from koolkarthik97/Graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BresenhamLine.cpp
58 lines (56 loc) · 1.35 KB
/
BresenhamLine.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
#include<iostream>
#include<graphics.h>
using namespace std;
void bbline(int xo, int yo, int xi, int yi)
{
int dx=xi-xo;
int dy=yi-yo;
int d=2*dy-dx;
int incre=2*dy;
int incrne=2*(dy-dx);
int end;
int x,y;
if(xo>xi || yo>yi)
{
x=xi;
y=yi;
end=xo;
}
else
{
x=xo;
y=yo;
end=xi;
}
putpixel(x,y,5);
while(x<=end){
if(d<0)
{
d+=incre;
x++;
if(yo!=yi) //for horizontal line yo=yi
y--; //for neg slope and not horizontal line
}
else
{
d+=incrne;
x++;
if(yo!=yi)
y++;
}
putpixel(x,y,WHITE);
}
}
int main()
{
initwindow(500,600,"bresenham");
int xo,yo,xi,yi;
cout<<"enter first coordinates"<<endl;
cin>>xo>>yo;
cout<<"enter second coordinates"<<endl;
cin>>xi>>yi;
bbline(xo,yo,xi,yi);
delay(50000);
closegraph();
return 0;
}