差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
cgfs:filled_triangles [2024/06/13 01:23] freemikancgfs:filled_triangles [2025/12/12 15:59] (現在) – Replace an overused back_inserter freemikan
行 1: 行 1:
-<codeprism lang=cpp el=true css=full>+====== DrawPixelだけで三角形を描く (2) 塗り潰す ====== 
 + 
 +{{:cgfs:filled_triangles-000.png?400|}} 
 + 
 +[[https://wiki.freemikan.com/doku.php?id=cgfs:vmath|vmath.h]] 
 + 
 +<file cpp>
 #include <raylib.h> #include <raylib.h>
  
 +#include <algorithm>
 #include <cassert> #include <cassert>
 #include <cmath> #include <cmath>
行 9: 行 16:
 #include "vmath.h" #include "vmath.h"
  
-int const Cw = 600; +int const Cw = 600; // Canvas width 
-int const Ch = 600;+int const Ch = 600; // Canvas height
  
 using vec2 = vmath::vec2<float>; using vec2 = vmath::vec2<float>;
行 26: 行 33:
     }     }
  
-    auto a = (d1 - d0) / (i1 - i0); +    float a = (d1 - d0) / (i1 - i0); 
-    auto d = d0;+    float d = d0;
  
     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::abs(x1 - x0) > std::abs(y1 - y0)) {     if (std::abs(x1 - x0) > std::abs(y1 - y0)) {
         if (x0 > x1) {         if (x0 > x1) {
-            std::swap(x0, x1); +            swap(x0, x1); 
-            std::swap(y0, y1);+            swap(y0, y1);
         }         }
-        auto i0 = static_cast<int>(std::floor(x0)); +        int i0 = static_cast<int>(std::floor(x0)); 
-        auto i1 = static_cast<int>(std::floor(x1)); +        int i1 = static_cast<int>(std::floor(x1)); 
-        auto ys = std::vector<float>(i1 - i0 + 1);+        std::vector<float> ys(i1 - i0 + 1);
  
         Interpolate(i0, y0, i1, y1, ys.begin());         Interpolate(i0, y0, i1, y1, ys.begin());
行 57: 行 67:
     } else {     } else {
         if (y0 > y1) {         if (y0 > y1) {
-            std::swap(y0, y1); +            swap(y0, y1); 
-            std::swap(x0, x1);+            swap(x0, x1);
         }         }
-        auto i0 = static_cast<int>(std::floor(y0)); +        int i0 = static_cast<int>(std::floor(y0)); 
-        auto i1 = static_cast<int>(std::floor(y1)); +        int i1 = static_cast<int>(std::floor(y1)); 
-        auto xs = std::vector<float>(i1 - i0 + 1);+        std::vector<float> xs(i1 - i0 + 1);
  
         Interpolate(i0, x0, i1, x1, xs.begin());         Interpolate(i0, x0, i1, x1, xs.begin());
行 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) {
-        std::swap(P1, P0);+        swap(P1, P0);
     }     }
     if (P2.y < P0.y) {     if (P2.y < P0.y) {
-        std::swap(P2, P0);+        swap(P2, P0);
     }     }
     if (P2.y < P1.y) {     if (P2.y < P1.y) {
-        std::swap(P2, P1);+        swap(P2, P1);
     }     }
  
-    auto i0 = static_cast<int>(std::floor(P0.y)); +    int i0 = static_cast<int>(std::floor(P0.y)); 
-    auto i1 = static_cast<int>(std::floor(P1.y)); +    int i1 = static_cast<int>(std::floor(P1.y)); 
-    auto i2 = static_cast<int>(std::floor(P2.y)); +    int i2 = static_cast<int>(std::floor(P2.y)); 
-    auto x01 = std::vector<float>(i1 - i0 + 1); +    std::vector<float> x01(i1 - i0 + 1); 
-    auto x12 = std::vector<float>(i2 - i1 + 1); +    std::vector<float> x12(i2 - i1 + 1); 
-    auto x02 = std::vector<float>(i2 - i0 + 1);+    std::vector<float> x02(i2 - i0 + 1);
  
     Interpolate(i0, P0.x, i1, P1.x, x01.begin());     Interpolate(i0, P0.x, i1, P1.x, x01.begin());
行 107: 行 119:
     Interpolate(i0, P0.x, i2, P2.x, x02.begin());     Interpolate(i0, P0.x, i2, P2.x, x02.begin());
  
 + // remove an overlapping point at x01.back and x12.front
     x01.pop_back();     x01.pop_back();
-    assert(x012.size() == x02.size()); 
  
-    auto x012 = std::vector<float>(x01.size() + x12.size()); +    std::vector<float> x012(x01.size() + x12.size()); 
-    auto corner = std::copy(x01.begin(), x01.end(), x012.begin()); +    std::merge(x01.begin(), x01.end(), x12.begin(), x12.end(), x012.begin()); 
-    std::copy(x12.begin(), x12.end(), corner);+ 
 +    assert(x012.size() == x02.size());
  
     auto &x_left = x012;     auto &x_left = x012;
     auto &x_right = x02;     auto &x_right = x02;
     auto m = x012.size() / 2;     auto m = x012.size() / 2;
- 
     if (x02[m] < x012[m]) {     if (x02[m] < x012[m]) {
-        std::swap(x_left, x_right);+        swap(x_left, x_right);
     }     }
  
     for (int y = i0; y < i2; ++y) {     for (int y = i0; y < i2; ++y) {
-        auto x_l = static_cast<int>(std::floor(x_left[y - i0])); +        int x_l = static_cast<int>(std::floor(x_left[y - i0])); 
-        auto x_r = static_cast<int>(std::floor(x_right[y - i0]));+        int x_r = static_cast<int>(std::floor(x_right[y - i0]));
         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>
  
文書の先頭へ