0

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

CImg 库包括用于绘制具有任意线条图案的各种形状的功能。但我没有看到任何关于填充模式的信息。

我认为这可能可以使用按位或数学运算符将图案掩码到实心图像上来完成,但我想看看具体的代码。

4

1 回答 1

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 ];
}
于 2011-10-21T23:02:50.613 回答