-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBresenhamLineAlgorithm.cpp
177 lines (159 loc) · 5.8 KB
/
BresenhamLineAlgorithm.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<stdio.h>
#include<graphics.h>
#include<math.h>
// required constants
#define X 640
#define Y 480
void lineBresenham(int,int,int,int,int); // function prototype to draw a straight line using Bresenham's algorithm
int main(void)
/*
Variable Description:-
x1- abscissa of the starting point
y1- ordinate of the starting point
x2- abscissa of the terminating point
y2- ordinate of the terminating point
Functions Description:-
Predefined:
initwindow()- to create the displaying window
circle()- to mark the end points
getch()- to hold the console till a button is pressed
User-defined:
lineBresenham()- to draw a straight line using Bresenham's algorithm
*/
{
int x1,y1,x2,y2; // declaring variables
// accept user inputs
printf("Enter the points: ");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
initwindow(X,Y); // creating window
// marking the two-end points with circles
circle(x1,y1,5);
circle(x2,y2,5);
lineBresenham(x1,y1,x2,y2,12); // function call to draw a straight line using Bresenham's algorithm
getch();
return 0;
} // end of main
// function definition to draw a straight line using Bresenham's algorithm
void lineBresenham(int x1,int y1,int x2,int y2,int color)
/* Variable Description:-
x1- abscissa of the starting point
y1- ordinate of the starting point
x2- abscissa of the terminating point
y2- ordinate of the terminating point
color- the color with which the line is to be drawn
dx- difference between x2 and x1
dy- difference between y2 and y1
(x,y) - pixel to plot
absDX- absolute values of dx
absDY- absolute values of dy
xInc- constant value that adds up to x after each iteration
yInc- constant value that adds up to y after each iteration
Functions Description:-
Predefined:
putpixel()- to draw a particular pixel with a particular color
delay()- to hold the present scenario for some time passed as an argument
Algorithm:-
We assume that slope is gentle (increment x, find y).
If dx<0 we assume it to be positive for calculation of decision varaible.
x= x+1 or x-1 according as dx>0 <left ro right) or dx<0 (right to left) respectively.
Initial decision parameter, d=2*dy-dx;
Now d=d+2(dy-dx), if d>0, NE pixel to plot, y=y+1
=d+2dx, if d<=0, E pixel to plot, y=y;
If slope is sharp/ steep, just interchange the roles of x and y and proceed same as above.
*/
{
int x,y,xInc,yInc,d,dx,dy,absDx,absDy; // declaring variables
// initializing variables and calculating some constants
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
absDx=abs(dx);
absDy=abs(dy);
if(x1==x2 && y1==y2) //same point
putpixel(x1,y1,color); // plot the pixel with color 'color'
else if(y1==y2) // horizontal line
{
// calculate xInc, y remains constant
if(x1<x2)
xInc=1;
else
xInc=-1;
while(x!=x2)
{
putpixel(x,y,color); // plot the pixel with color 'color'
x+=xInc; // change x
delay(1); // holding the present state for 1 milliseconds
}
putpixel(x,y,color); // plot the pixel with color 'color'
}
else if(x1==x2) // vertical
{
// calculate yInc, x remains constant
if(y1<y2)
yInc=1;
else
yInc=-1;
while(y!=y2)
{
putpixel(x,y,color); // plot the pixel with color 'color'
y=y+yInc; // change y
delay(1); // holding the present state for 1 milliseconds
}
putpixel(x,y,color);
}
else if(absDy<absDx) // gentle slope
{
// calculate xInc
if(x1<x2)
xInc=1;
else
xInc=-1;
// calculate yInc
if(y1<y2)
yInc=1;
else
yInc=-1;
d=2*absDy-absDx; // initial decision parameter
while(x!=x2)
{
if(d>0) // move to NE, y changes
{
d=d+2*absDy-2*absDx;
y+=yInc;
}
else // move to E pixel
d=d+2*absDy;
x+=xInc;
putpixel(x,y,color); // plot the pixel with color 'color'
delay(1); // holding the present state for 1 milliseconds
}
}
else // sharp
{
// calculate xInc
if(x1<x2)
xInc=1;
else
xInc=-1;
// calculate yInc
if(y1<y2)
yInc=1;
else
yInc=-1;
d=2*absDx-absDy; // initial decision parameter
while(y!=y2)
{
if(d>0) // x changes
{
d=d+2*absDx-2*absDy;
x+=xInc;
}
else // x remains same
d=d+2*absDx;
y+=yInc;
delay(1); // holding the present state for 1 milliseconds
putpixel(x,y,color); // plot the pixel with color 'color'
}
}
} // end of function