Skip to content

Commit 5e66337

Browse files
committed
Simple speed up of Lesson 1 ssloy/tinyrenderer#28
1 parent ccdf77b commit 5e66337

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

main.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,31 @@ void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color)
2222
int derror2 = std::abs(dy) * 2;
2323
int error2 = 0;
2424
int y = y0;
25-
for (int x=x0; x <= x1; x++)
25+
const int yinc = (y1 > y0? 1 : -1);
26+
if (steep)
2627
{
27-
if (steep)
28+
for (int x=x0; x <= x1; x++)
2829
{
2930
image.set(y, x, color);
31+
error2 += derror2;
32+
if (error2 > dx)
33+
{
34+
y += yinc;
35+
error2 -= dx * 2;
36+
}
3037
}
31-
else
38+
}
39+
else
40+
{
41+
for (int x=x0; x <= x1; x++)
3242
{
3343
image.set(x, y, color);
34-
}
35-
error2 += derror2;
36-
if (error2 > dx)
37-
{
38-
y += (y1 > y0? 1 : -1);
39-
error2 -= dx * 2;
44+
error2 += derror2;
45+
if (error2 > dx)
46+
{
47+
y += yinc;
48+
error2 -= dx * 2;
49+
}
4050
}
4151
}
4252
}

0 commit comments

Comments
 (0)