差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| cgfs:filled_triangles [2024/06/13 01:23] – freemikan | cgfs:filled_triangles [2025/12/12 15:59] (現在) – Replace an overused back_inserter freemikan | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | < | + | ====== DrawPixelだけで三角形を描く (2) 塗り潰す ====== |
| + | |||
| + | {{: | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | <file cpp> | ||
| #include < | #include < | ||
| + | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| 行 9: | 行 16: | ||
| #include " | #include " | ||
| - | int const Cw = 600; | + | int const Cw = 600; // Canvas width |
| - | int const Ch = 600; | + | int const Ch = 600; // Canvas height |
| using vec2 = vmath:: | using vec2 = vmath:: | ||
| 行 26: | 行 33: | ||
| } | } | ||
| - | | + | |
| - | | + | |
| for (int i = i0; i <= i1; ++i) { | for (int i = i0; i <= i1; ++i) { | ||
| - | *out++ = d; | + | *out = d; |
| + | ++out; | ||
| d += a; | d += a; | ||
| } | } | ||
| 行 36: | 行 44: | ||
| void DrawLine(vec2 const &p0, vec2 const &p1, Color const &color) { | void DrawLine(vec2 const &p0, vec2 const &p1, Color const &color) { | ||
| + | using std::swap; | ||
| + | |||
| auto x0 = p0.x; | auto x0 = p0.x; | ||
| auto y0 = p0.y; | auto y0 = p0.y; | ||
| 行 43: | 行 53: | ||
| if (std:: | if (std:: | ||
| if (x0 > x1) { | if (x0 > x1) { | ||
| - | | + | swap(x0, x1); |
| - | | + | swap(y0, y1); |
| } | } | ||
| - | | + | |
| - | | + | |
| - | | + | std:: |
| Interpolate(i0, | Interpolate(i0, | ||
| 行 57: | 行 67: | ||
| } else { | } else { | ||
| if (y0 > y1) { | if (y0 > y1) { | ||
| - | | + | swap(y0, y1); |
| - | | + | swap(x0, x1); |
| } | } | ||
| - | | + | |
| - | | + | |
| - | | + | std:: |
| Interpolate(i0, | Interpolate(i0, | ||
| 行 85: | 行 95: | ||
| vec2 const &p2, | vec2 const &p2, | ||
| Color const &color) { | Color const &color) { | ||
| + | using std::swap; | ||
| + | |||
| vec2 P0{p0}, P1{p1}, P2{p2}; | vec2 P0{p0}, P1{p1}, P2{p2}; | ||
| if (P1.y < P0.y) { | if (P1.y < P0.y) { | ||
| - | | + | swap(P1, P0); |
| } | } | ||
| if (P2.y < P0.y) { | if (P2.y < P0.y) { | ||
| - | | + | swap(P2, P0); |
| } | } | ||
| if (P2.y < P1.y) { | if (P2.y < P1.y) { | ||
| - | | + | swap(P2, P1); |
| } | } | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | std:: |
| - | | + | std:: |
| - | | + | std:: |
| Interpolate(i0, | Interpolate(i0, | ||
| 行 107: | 行 119: | ||
| Interpolate(i0, | Interpolate(i0, | ||
| + | // remove an overlapping point at x01.back and x12.front | ||
| x01.pop_back(); | x01.pop_back(); | ||
| - | assert(x012.size() == x02.size()); | ||
| - | | + | std:: |
| - | | + | std::merge(x01.begin(), |
| - | | + | |
| + | | ||
| auto &x_left = x012; | auto &x_left = x012; | ||
| auto & | auto & | ||
| auto m = x012.size() / 2; | auto m = x012.size() / 2; | ||
| - | |||
| if (x02[m] < x012[m]) { | if (x02[m] < x012[m]) { | ||
| - | | + | swap(x_left, |
| } | } | ||
| for (int y = i0; y < i2; ++y) { | for (int y = i0; y < i2; ++y) { | ||
| - | | + | |
| - | | + | |
| for (int x = x_l; x <= x_r; ++x) { | for (int x = x_l; x <= x_r; ++x) { | ||
| PutPixel(x, y, color); | PutPixel(x, y, color); | ||
| 行 153: | 行 165: | ||
| } | } | ||
| } | } | ||
| - | </codeprism> | + | </file> |
