diff --git a/src/main.cpp b/src/main.cpp index bd5714a..a8f0c65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,23 +15,32 @@ void line(int x0, int y0, int x1, int y1, TGAImage& image, TGAColor color) { std::swap(x0, x1); std::swap(y0, y1); } + int dx = x1 - x0; int dy = y1 - y0; int derror2 = std::abs(dy) * 2; int error2 = 0; int y = y0; - for (int x = x0; x <= x1; x++) { - if (steep) { + int y_increment = y1 > y0 ? 1 : -1; + + if (steep) { + for (int x = x0; x <= x1; ++x) { image.set(y, x, TGAColor(255, 1)); + error2 += derror2; + if (error2 > dx) { + y += y_increment; + error2 -= dx * 2; + } } - else { + } + else { + for (int x = x0; x <= x1; ++x) { image.set(x, y, TGAColor(255, 1)); - } - error2 += derror2; - - if (error2 > dx) { - y += (y1 > y0 ? 1 : -1); - error2 -= dx * 2; + error2 += derror2; + if (error2 > dx) { + y += y_increment; + error2 -= dx * 2; + } } } }