diff --git a/tinyrender2/render.go b/tinyrender2/render.go index 41c8dd1..220020b 100644 --- a/tinyrender2/render.go +++ b/tinyrender2/render.go @@ -60,15 +60,6 @@ func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) { v1, v2 = v2, v1 } - // The lesson says to always figure out what's on the left. - // As such, we always go from left to right - // While drawing the straight line, go the opposite direction if the - // longer side is on the right (we go from long side to short) - var sweep int = 1 - if v2.X > v1.X { - sweep = -1 - } - // Don't worry about speed for now... dy := float32(v2.Y-v0.Y) - 0.5 var v02step, v01step, v12step, xlong, xshort float32 @@ -88,8 +79,13 @@ func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) { } for y := v0.Y; y <= v2.Y; y++ { - // Draw a horizontal line from - for x := int(math.Floor(float64(xlong))); x <= int(math.Floor(float64(xshort))); x += sweep { + xleft := int(math.Floor(float64(xshort))) + xright := int(math.Floor(float64(xlong))) + if xleft > xright { + xleft, xright = xright, xleft + } + // Draw a horizontal line from elft to right + for x := xleft; x <= xright; x++ { fb.SetSafe(uint(x), uint(y), color) } xlong += v02step