c++ - 使用CImg绘制三角形并使用插值填充颜色

标签 c++ interpolation cimg

我必须使用 CImg 构建图像并使用中点算法绘制三角形,然后使用线性插值法为边缘着色。我很难理解 CImg 库的语法。

我现在只有黑色空白图像。

#include <iostream>
#include "CImg/CImg.h"

using namespace cimg_library;
using namespace std;

int main() {
  //Create an empty image
  CImg<unsigned char> triangle(255, 255, 1, 3, 0);

  //Display and save filtered image
  CImgDisplay disp(triangle); 
  while (!disp.is_closed())
      disp.wait();   
  triangle.save("triangleInterpolation.bmp");  
}

The result should look like this
enter image description here

最佳答案

这样的东西应该不错...

#include <iostream>
#include <cstdlib>
#define cimg_display 0
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main() {
   // Create 256x256 with radial gradient
   CImg<unsigned char> image(256,256,1,3,0);
   cimg_forXYC(image,x,y,c) {image(x,y,0,c) = 255-(unsigned char)hypot((float)(128-x),(float)(128-y)); }

   // Now overlay a partially opaque 2D interpolated filled triangle for fun
   unsigned char cyan[]    = { 0, 255, 255 };
   unsigned char magenta[] = { 255, 0, 255 };
   unsigned char yellow[]  = { 255, 255, 0 };

   const float opacity=0.6;
   image.draw_triangle(
      10,10,       // 1st vertex x,y
      245,10,      // 2nd vertex x,y
      245,245,     // 3rd vertex x,y 
      cyan,        // 1st vertex colour
      magenta,     // 2nd vertex colour
      yellow,      // 3rd vertex colour
      opacity);

   // Save result image as NetPBM PNM - no libraries required
   image.save_pnm("result.pnm");
}

enter image description here

关于c++ - 使用CImg绘制三角形并使用插值填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48610464/

相关文章:

c++ - 如何从 C++ 中的内部 union 访问类成员

python - 在 numpy 数组中插入 NaN 值

c++ - 在 CImg 中设置 alpha 的默认值

c++ - 如何使用组成排序键的位域而不落入UB?

c++ - 从 std::ranges 算法中获取投影值

c++ - 如何用 C 或 C++ 编写只有 GET 和 POST 方法的 http 协议(protocol)客户端

r - Hadoop中缺少时间序列数据

Angular Directive(指令)和插值不适用于动态 html 代码

c++ - 在 Windows 中全局存储 C++ 头文件

c++ - Cimg 随机抛出 CImgIOException