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

时间:2018-02-04 16:37:25

标签: c++ interpolation cimg

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

我现在所拥有的只是黑色的空白图像。

a_p.h

The result should look like this
enter image description here

1 个答案:

答案 0 :(得分:1)

这样的事情应该是好的......

#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