Graphics32用阴影图案填充多边形

时间:2017-07-26 05:52:09

标签: delphi vcl delphi-xe4 graphics32

我正在尝试将delphi XE4应用程序转换为使用Graphics32库进行绘制而不是标准的delphi绘图方法。

我做的一件事是绘制一个包含带有斜交叉线图案的小椭圆的图标。图标应如下所示:

enter image description here

以下是我使用标准TCanvas绘图方法的方法:

ACanvas.Brush.Color := shape.pcolor;
ACanvas.Brush.Style := bsdiagCross;
ACanvas.Ellipse(-13, -9, 13, 9);

我可以使用Graphics32绘制椭圆,执行以下操作:

var    
  Polygon : TArrayOfFloatPoint;   
begin    
  Polygon := Ellipse(0, 0, 13, 9);
  PolylineFS(Bitmap, Polygon, pcolor, True, UAVPenWidth);

但有没有一种简单的方法来复制对角交叉影线图案?我假设我可以使用TBitmapPolygonFiller类,但这是使用位图填充。请注意,如果相关,此图形将显示在TPositionedLayer事件处理程序中的OnPaint

2 个答案:

答案 0 :(得分:2)

到目前为止,Graphics32中没有直接的模式支持,但是有很多方法可以创建类似你想要使用的模式。

这是使用样本多边形填充的一种解决方案:

首先,您需要为阴影图案编写一个采样器类。有几种方法可以构建这样的采样器。您可以在下面找到一个非常简单的:

type
  THatchedPatternSampler = class(TCustomSampler)
  public
    function GetSampleInt(X, Y: Integer): TColor32; override;
  end;

function THatchedPatternSampler.GetSampleInt(X, Y: Integer): TColor32;
begin
  Result := 0;
  if ((X - Y) mod 8 = 0) or ((X + Y) mod 8 = 0) then
    Result := clRed32
end;

你只需要在这里覆盖一个方法(GetSampleInt),所有其他方法都可以在祖先类中使用。

现在它有点卷入。为了使用该样本,您必须将它用于TSamplerFiller的顶部,如下所示:

Sampler := THatchedPatternSampler.Create;
Filler := TSamplerFiller.Create(Sampler);

一旦有了这个填充物,就可以在PolygonFS甚至PolylineFS中使用它。

最后代码可能如下所示:

var
  Polygon: TArrayOfFloatPoint;
  Sampler: THatchedPatternSampler;
  Filler: TSamplerFiller;
begin
  Polygon := Ellipse(128, 128, 120, 100);
  Sampler := THatchedPatternSampler.Create;
  try
    Filler := TSamplerFiller.Create(Sampler);
    try
      PolygonFS(PaintBox32.Buffer, Polygon, Filler);
    finally
      Filler.Free;
    end;
      finally
    Sampler.Free;
  end;

  PolylineFS(PaintBox32.Buffer, Polygon, clRed32, True, 1);
end;

这将在位图的中心(这里:TPaintBox32实例的缓冲区)绘制一个相当大的椭圆,并用阴影采样器代码填充它。最后使用PolylineFS函数绘制实线轮廓。

从性能角度来看,这并不是每个像素调用GetSampleInt的最快方法。但是,最容易理解会发生什么。

为了更快的替代方案,您应该直接使用填充物。您可以直接从TCustomPolygonFiller派生,如下所示:

type
  THatchedPatternFiller = class(TCustomPolygonFiller)
  private
    procedure FillLine(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  protected
    function GetFillLine: TFillLineEvent; override;
  end;

方法GetFillLine变得如此简单:

function THatchedPatternFiller.GetFillLine: TFillLineEvent;
begin
  Result := FillLine;
end;

然而,FillLine方法会更加复杂,如下所示:

procedure THatchedPatternFiller.FillLine(Dst: PColor32; DstX, DstY,
  Length: Integer; AlphaValues: PColor32);
var
  X: Integer;
begin
  for X := DstX to DstX + Length do
  begin
    if ((X - DstY) mod 8 = 0) or ((X + DstY) mod 8 = 0) then
      Dst^ :=clRed32
    else
      Dst^ := 0;

    Inc(Dst);
  end;
end;

由于DstY保持不变,您还可以重构代码以提高性能。或者你可以使用汇编程序(SSE)加速代码,但我想这对于这样一个简单的函数来说会有点过分。

答案 1 :(得分:-1)

我尝试了上述自定义填充,但结果出乎意料。 U形多边形的两条腿之间的区域被填充。对于我正在做的事情的任何投入,将不胜感激。

procedure TForm1.Button1Click(Sender: TObject);
var Filler2: THatchedPatternFiller;
    Polygon: TArrayOfFloatPoint;
begin
  polygon := [floatpoint(100, 10), floatpoint(200, 10), floatpoint(200, 400), floatpoint(300, 400), floatpoint(300, 10), floatpoint(400, 10), floatpoint(400, 500), floatpoint( 100, 500), floatpoint( 100, 10)]; // U shaped polygon
  filler2 := THatchedPatternFiller.Create;
  PolygonFS(PreviewImage.Bitmap, polygon, filler2);   // Wrong, red fill inside U share
  PolygonFS(PreviewImage.Bitmap, polygon, clGreen32); // Works fine, green fill
  Filler2.Free;
end;

Image with wrong fill, the red part should not be visible in the centre and on the right side

相关问题