-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine2D.cpp
More file actions
266 lines (250 loc) · 7.99 KB
/
Copy pathLine2D.cpp
File metadata and controls
266 lines (250 loc) · 7.99 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "Line2D.hpp"
namespace CGF
{
Line2D::Line2D(const Point2D &start, const Point2D &end)
{
mStartPoint = start; mEndPoint = end; mColor = Color();
}
Line2D::Line2D(const Point2D & start, const Point2D & end, const Color & clr)
{
mStartPoint = start; mEndPoint = end; mColor = clr;
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Routine for drawing a line from start position mStartPoint to end
position mEndPoint using the DDA algorithm
@param window
The window to draw to
@Use Use the following data members of class Line2D
mStartPoint: start position
mEndPoint: end position
mColor: RGBA color
Commented code below shows how to draw a horizontal line ...
*/
////////////////////////////////////////////////////////////////////////////////
void Line2D::DrawDDA(const Window & window) const
{
float x0 = mStartPoint.GetX();
float y0 = mStartPoint.GetY();
float x1 = mEndPoint.GetX();
float y1 = mEndPoint.GetY();
if (roundToInt(x1 - x0) == 0)
DrawDDALargeM(x0, y0, x1, y1, 0.F, window);
else
{
float m = (y1 - y0) / (x1 - x0);
float mAbs = ((m >= 0.F) ? m : -m);
if (mAbs > 1.0F)
DrawDDALargeM(x0, y0, x1, y1, 1.F / mAbs, window);
else
DrawDDAFracM(x0, y0, x1, y1, mAbs, window);
}
/*
// Example code to show the scan-conversion of a horizontal line
// from (50,100) to (500, 100)
Point2D start(50.f, 100.f); Point2D end(500.f, 100.f);
for (int x = static_cast<int>(start.GetX()); x <= static_cast<int>(end.GetX()); x++)
window.SetPixel(x, static_cast<int>(start.GetY()), mColor);
*/
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Subroutine for drawing a line from start position (x0, y0) to end
position (x1, y1), when |slope| <= 1.0, using the DDA algorithm
@param x0
x-Value of start position
@param y0
y-Value of start position
@param x1
x-Value of end position
@param y1
y-Value of end position
@param mAbs
absolute value of slope, m, where 0.0 < |m| <= 1.0
@param window
The window to draw to
@Use Use the following data members of class Line2D
mColor: RGBA color
*/
////////////////////////////////////////////////////////////////////////////////
void Line2D::DrawDDAFracM(float x0, float y0, float x1, float y1, float mAbs, const Window & window) const
{
int xSet = roundToInt(x0); // x of first pixel
float ySet = y0; // y of first pixel, still in float form
int dx = roundToInt(x1) - xSet; // x-distance from start to end pixel
int xStep = ((dx >= 0) ? 1 : -1); // step x by 1 for left-to-right lines
// step x by -1 for right-to-left lines
dx = ((dx >= 0) ? dx : -dx); // |dx| so that while loop can always subtract 1
float dy = y1 - y0; // y-distance from start to end pixel, still in float form
float yStep = ((dy >= 0.F) ? mAbs : -mAbs); // y increases or decreases by m for each step of x
window.SetPixel(xSet, roundToInt(ySet), mColor); // set the starting pixel
// we now know the change in y for every step in x, so iterate and plot!
while(dx--)
{
xSet += xStep;
ySet += yStep;
window.SetPixel(xSet, roundToInt(ySet), mColor);
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Subroutine for drawing a line from start position (x0, y0) to end
position (x1, y1), when |slope| > 1, using the DDA algorithm
@param x0
x-Value of start position
@param y0
y-Value of start position
@param x1
x-Value of end position
@param y1
y-Value of end position
@param mInv
inverse of absolute value of slope, m, where |m| > 1.0
@param window
The window to draw to
@Use Use the following data members of class Line2D
mColor: RGBA color
*/
////////////////////////////////////////////////////////////////////////////////
void Line2D::DrawDDALargeM(float x0, float y0, float x1, float y1, float mInv, const Window & window) const
{
// comments for these lines are similar to DrawDDAFracM above
float xSet = x0;
int ySet = roundToInt(y0);
int dy = roundToInt(y1) - ySet;
int yStep = ((dy >= 0) ? 1 : -1);
dy = ((dy >= 0) ? dy : -dy);
float dx = x1 - x0;
float xStep = ((dx >= 0.F) ? mInv : -mInv); // x increases or decreases by mInv for each step of y
window.SetPixel(roundToInt(xSet), ySet, mColor);
// we now know the change in x for every step in y, so iterate and plot!
while(dy--)
{
xSet += xStep;
ySet += yStep;
window.SetPixel(roundToInt(xSet), ySet, mColor);
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Routine for drawing a line from start position mStartPoint to end
position mEndPoint using the Bresenham/Midpoint algorithm
@param window
The window to draw to
@Use Use the following data members of class Line2D
mStartPoint: start position
mEndPoint: end position
mColor: RGBA color
*/
////////////////////////////////////////////////////////////////////////////////
void Line2D::DrawBresenham(const Window & window) const
{
// Since we are dealing with integer pixel values, and not
// any clipping, the x and y floats can be rounded to ints.
int x0 = roundToInt(mStartPoint.GetX());
int y0 = roundToInt(mStartPoint.GetY());
int x1 = roundToInt(mEndPoint.GetX());
int y1 = roundToInt(mEndPoint.GetY());
if ((x1 - x0) == 0)
DrawBresLargeM(x0, y0, x1, y1, window);
else
{
float m = ((float) (y1 - y0)) / ((float) (x1 - x0));
float mAbs = ((m >= 0.F) ? m : -m);
if (mAbs > 1.0F)
DrawBresLargeM(x0, y0, x1, y1, window);
else
DrawBresFracM(x0, y0, x1, y1, window);
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Subroutine for drawing a line from start position (x0, y0) to end
position (x1, y1), when |slope| <= 1.0, using the Bresenham algorithm
@param x0
x-Value of start position
@param y0
y-Value of start position
@param x1
x-Value of end position
@param y1
y-Value of end position
@param window
The window to draw to
@Use Use the following data members of class Line2D
mColor: RGBA color
*/
////////////////////////////////////////////////////////////////////////////////
void Line2D::DrawBresFracM(int x0, int y0, int x1, int y1, const Window & window) const
{
int dx = x1 - x0;
int xStep = ((dx >= 0) ? 1 : -1);
bool LToR = (xStep == 1);
bool RToL = (xStep == -1);
dx = ((dx >= 0) ? dx : -dx);
int dy = y1 - y0;
int yStep = ((dy >= 0) ? 1 : -1);
dy = ((dy >= 0) ? dy : -dy);
int dVal = (2 * dy) - dx;
int incrStraight = 2 * dy;
int incrDiagonal = 2 * (dy - dx);
window.SetPixel(x0, y0, mColor);
while(dx--)
{
x0 += xStep;
if ((LToR && dVal <= 0) || (RToL && dVal < 0))
dVal += incrStraight;
else
{
dVal += incrDiagonal;
y0 += yStep;
}
window.SetPixel(x0, y0, mColor);
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Subroutine for drawing a line from start position (x0, y0) to end
position (x1, y1), when |slope| > 1, using the Bresenham algorithm
@param x0
x-Value of start position
@param y0
y-Value of start position
@param x1
x-Value of end position
@param y1
y-Value of end position
@param window
The window to draw to
@Use Use the following data members of class Line2D
mColor: RGBA color
*/
////////////////////////////////////////////////////////////////////////////////
void Line2D::DrawBresLargeM(int x0, int y0, int x1, int y1, const Window & window) const
{
int dy = y1 - y0;
int yStep = ((dy >= 0) ? 1 : -1);
bool LToR = (yStep == 1);
bool RToL = (yStep == -1);
dy = ((dy >= 0) ? dy : -dy);
int dx = x1 - x0;
int xStep = ((dx >= 0) ? 1 : -1);
dx = ((dx >= 0) ? dx : -dx);
int dVal = (2 * dx) - dy;
int incrStraight = 2 * dx;
int incrDiagonal = 2 * (dx - dy);
window.SetPixel(x0, y0, mColor);
while(dy--)
{
y0 += yStep;
if ((LToR && dVal <= 0) || (RToL && dVal < 0))
dVal += incrStraight;
else
{
dVal += incrDiagonal;
x0 += xStep;
}
window.SetPixel(x0, y0, mColor);
}
}
} // namespace CGF