Anonymous user
Bitmap/Bresenham's line algorithm: Difference between revisions
Bitmap/Bresenham's line algorithm (view source)
Revision as of 16:10, 13 December 2010
, 13 years ago→{{header|C}}
m (→{{header|C}}) |
|||
Line 153:
=={{header|C}}==
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
<lang C>void line(int x0, int y0, int x1, int y1) {
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
int err = (dx>dy ? dx : -dy)/2, e2;
while(true){
if (e2 >-dx) { err -= dy; x0 += sx; }
}</lang>
▲ swap_uint(x0, y0);
▲ }
▲ if (x0 > x1) {
▲ int y = y0;
=={{header|C++}}==
|