Skip to content

Commit

Permalink
Optimize the code according to: ssloy/tinyrenderer#28.
Browse files Browse the repository at this point in the history
  • Loading branch information
zfengyan committed May 10, 2022
1 parent b48c66b commit f6ac2c6
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down

0 comments on commit f6ac2c6

Please sign in to comment.