diff --git a/src/Raster.cc b/src/Raster.cc index 9bc1c42..e35c11f 100644 --- a/src/Raster.cc +++ b/src/Raster.cc @@ -163,8 +163,16 @@ void Raster::drawSpan(const Span &span) { Rgba color; float length = span.xEnd - span.xStart; - for (int i = span.xStart; i <= span.xEnd; i++) { - color = colorLerp(span.color1, span.color2, (float) (i - span.xStart) / length); + + + int startX = tmax(span.xStart, 0); + int endX = tmin(span.xEnd, _width); + + float scale = (startX - span.xStart) / length; + + //优化3 x轴越界处理 + for (int i = startX; i <= endX; i++) { + color = colorLerp(span.color1, span.color2, (float) (i - startX) / length); setPixel(i, span.y, color); } } @@ -182,16 +190,20 @@ void Raster::drawEge(const Ege& e1, const Ege& e2) { float scale2 = 0; - //优化2 不画三角形坐标y以为的部分 -// int startY = tmin(e2.y2, _height); -// int endY = tmax(e2.y1, 0); -// scale2 = (startY - _height) / yOffset2; + //优化2 y轴越界处理 + int startY2 = tmin(e2.y2, _height); + int endY2 = tmax(e2.y1, 0); + scale2 = (e2.y2 - startY2) / yOffset2; + int startY1 = tmin(e1.y2, _height); + float s = (e1.y2 - startY1) / yOffset1; + scale1 = tmax(scale1, s); + //printf("") Rgba colorE1; Rgba colorE2; - for (int y = e2.y2; y > e2.y1; y--) { + for (int y = startY2; y > endY2; y--) { int x2; if (e2.x2 < e2.x1) { diff --git a/src/draw_triangle.cc b/src/draw_triangle.cc index a2b7274..6da94a5 100644 --- a/src/draw_triangle.cc +++ b/src/draw_triangle.cc @@ -72,17 +72,17 @@ void example3() { void example4() { - int2 p[3] = { - int2(50, 50), - int2(250, 250), - int2(300, 150) - }; - - // int2 p[3] = { - // int2(100, 150), - // int2(250, 250), - // int2(350, 50) - // }; +// int2 p[3] = { +// int2(-50, 50), +// int2(250, 800), +// int2(300, 150) +// }; + + int2 p[3] = { + int2(100, 150), + int2(250, 250), + int2(350, 50) + }; Ege e0(p[0].x, p[0].y, p[1].x, p[1].y, Rgba(255, 0, 0, 0), Rgba(0, 255, 0, 0)); Ege e1(p[1].x, p[1].y, p[2].x, p[2].y, Rgba(0, 255, 0, 0), Rgba(0, 0, 255, 0));