用CImg中的图案填充形状

时间:2011-10-08 01:50:17

标签: polygon flood-fill cimg

我希望能够绘制形状并用各种填充图案(对角线,点画点等)填充它们。

CImg库包含用于为大纲绘制具有任意线条图案的各种形状的功能。但是我没有看到关于填充模式的任何内容。

我认为这可以通过使用按位或数学运算符将模式屏蔽到实体图像上来完成,但我希望看到执行此操作的具体代码。

1 个答案:

答案 0 :(得分:0)

是的,多边形填充图案可以通过首先以纯色绘制多边形,然后使用带有预先加载的黑白图案图像的& =运算符来实现。

// preload several pattern images (black and white versions of the desired fill patterns)
CImg<unsigned char> *fillPatternImages[ NumFillPatterns ] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
fillPatternImages[ 0 ] = new CImg<unsigned char>( "256x256_bw_dotted_fill.png" );
... etc. for all patterns you want to use

// create an empty image
CImg<unsigned char> image( 256, 256, 1, 4, 0 );

// draw the polygon (or in the case of my code, any number of polygons) on the image in a solid color
if ( nShapeType == SHPT_POLYGON && fillPattern != FILL_PATTERN_NONE )
{
  for( int i = 0 ; i < nShapeCount ; i++ )
  {
    SHPObject *psShape;
    psShape = SHPReadObject( hSHP, panHits[ i ] );

    for ( int part = 0 ; part < psShape->nParts ; part++ )
    {
      int numPoints;
      if ( part < ( psShape->nParts - 1 ) )
      {
        numPoints = psShape->panPartStart[ part + 1 ] - psShape->panPartStart[ part ];
      }
      else
      {
        numPoints = psShape->nVertices - psShape->panPartStart[ part ];
      }
      CImg<int> pointImage( numPoints, 2, 1, 1, 0 );
      int s = psShape->panPartStart[ part ];
      for ( int p = 0 ; p < numPoints ; p++ )
      {
        int screenX;
        int screenY;
        GetTileXYFromMercatorLonLat( (float)psShape->padfX[ s + p ], (float)psShape->padfY[ s + p ], x, y, z, &screenX, &screenY );
          pointImage( p, 0 ) = screenX;
          pointImage( p, 1 ) = screenY;
      }
      image.draw_polygon( pointImage, fillColor );
    }

    SHPDestroyObject( psShape );
  }
}

// to achieve a non-solid pattern, & the image with a pre-loaded pattern image
if ( fillPattern > -1 )
{
  image &= *fillPatternImages[ fillPattern ];
}