• Direct2D (24) : 合并几何对象



    合并通过 ID2D1Geometry.CombineWithGeometry() 完成,但返回的是 ID2D1SimplifiedGeometrySink(ID2D1GeometrySink 的父对象);
    需要把 ID2D1SimplifiedGeometrySink 添加到 ID2D1PathGeometry 才能使用。

    uses Direct2D, D2D1;
    
    procedure TForm1.FormPaint(Sender: TObject);
    var
      cvs: TDirect2DCanvas;
      iEllipseGeometry1, iEllipseGeometry2: ID2D1EllipseGeometry;
      iGeometrySink: ID2D1GeometrySink;
      iPathGeometry1,iPathGeometry2,iPathGeometry3,iPathGeometry4: ID2D1PathGeometry;
      ptCenter: TD2DPoint2f;
      r: Single;
      matrix: TD2DMatrix3x2F;
    begin
      ptCenter := D2D1PointF(100, 100);
      r := 50;
      matrix := TD2DMatrix3x2F.Identity;
    
      {两个源对象}
      D2DFactory.CreateEllipseGeometry(D2D1Ellipse(ptCenter, r, r), iEllipseGeometry1);
      ptCenter.x := ptCenter.x + r;
      D2DFactory.CreateEllipseGeometry(D2D1Ellipse(ptCenter, r, r), iEllipseGeometry2);
    
      {UNION 运算}
      D2DFactory.CreatePathGeometry(iPathGeometry1);
      iPathGeometry1.Open(iGeometrySink);
        iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, D2D1_COMBINE_MODE_UNION, matrix, 0, iGeometrySink);
      iGeometrySink.Close;
    
      {INTERSECT 运算}
      D2DFactory.CreatePathGeometry(iPathGeometry2);
      iPathGeometry2.Open(iGeometrySink);
        iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, D2D1_COMBINE_MODE_INTERSECT, matrix, 0, iGeometrySink);
      iGeometrySink.Close;
    
      {XOR 运算}
      D2DFactory.CreatePathGeometry(iPathGeometry3);
      iPathGeometry3.Open(iGeometrySink);
        iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, D2D1_COMBINE_MODE_XOR, matrix, 0, iGeometrySink);
      iGeometrySink.Close;
    
      {EXCLUDE 运算}
      D2DFactory.CreatePathGeometry(iPathGeometry4);
      iPathGeometry4.Open(iGeometrySink);
        iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, D2D1_COMBINE_MODE_EXCLUDE, matrix, 0, iGeometrySink);
      iGeometrySink.Close;
    
      cvs := TDirect2DCanvas.Create(Canvas, ClientRect);
      cvs.BeginDraw;
      cvs.Pen.Color := clWebCornFlowerBlue;
      cvs.Brush.Color := clWebCornFlowerBlue;
      cvs.Brush.Handle.SetOpacity(0.75);
    
      cvs.FillGeometry(iPathGeometry1);
      cvs.DrawGeometry(iEllipseGeometry1);
      cvs.DrawGeometry(iEllipseGeometry2);
    
      cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(180, 0));
      cvs.FillGeometry(iPathGeometry2);
      cvs.DrawGeometry(iEllipseGeometry1);
      cvs.DrawGeometry(iEllipseGeometry2);
    
      cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(180*2, 0));
      cvs.FillGeometry(iPathGeometry3);
      cvs.DrawGeometry(iEllipseGeometry1);
      cvs.DrawGeometry(iEllipseGeometry2);
    
      cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(180*3, 0));
      cvs.FillGeometry(iPathGeometry4);
      cvs.DrawGeometry(iEllipseGeometry1);
      cvs.DrawGeometry(iEllipseGeometry2);
    
      cvs.EndDraw;
      cvs.Free;
    end;
    


    效果图:



    简化后的代码:

    uses Direct2D, D2D1;
    
    procedure TForm1.FormPaint(Sender: TObject);
    var
      cvs: TDirect2DCanvas;
      iEllipseGeometry1, iEllipseGeometry2: ID2D1EllipseGeometry;
      iGeometrySink: ID2D1GeometrySink;
      iPathGeometrys: array[0..3] of ID2D1PathGeometry;
      ptCenter: TD2DPoint2f;
      r: Single;
      i: Integer;
    begin
      ptCenter := D2D1PointF(100, 100);
      r := 50;
    
      D2DFactory.CreateEllipseGeometry(D2D1Ellipse(ptCenter, r, r), iEllipseGeometry1);
      ptCenter.x := ptCenter.x + r;
      D2DFactory.CreateEllipseGeometry(D2D1Ellipse(ptCenter, r, r), iEllipseGeometry2);
    
      for i := 0 to 3 do
      begin
        D2DFactory.CreatePathGeometry(iPathGeometrys[i]);
        iPathGeometrys[i].Open(iGeometrySink);
          iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, i, TD2DMatrix3x2F.Identity, 0, iGeometrySink);
        iGeometrySink.Close;
      end;
    
      cvs := TDirect2DCanvas.Create(Canvas, ClientRect);
      cvs.BeginDraw;
      cvs.Pen.Color := clWebCornFlowerBlue;
      cvs.Brush.Color := clWebCornFlowerBlue;
      cvs.Brush.Handle.SetOpacity(0.75);
    
      for i := 0 to 3 do
      begin
        cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(180*i, 0));
        cvs.FillGeometry(iPathGeometrys[i]);
        cvs.DrawGeometry(iEllipseGeometry1);
        cvs.DrawGeometry(iEllipseGeometry2);
      end;
    
      cvs.EndDraw;
      cvs.Free;
    end;
    


  • 相关阅读:
    andrax不为人知的秘密
    SecuritySRT console已经建立链接为什么还没有反应?
    必备软硬件
    关于mt的个人看法及优缺点
    hash是什么
    usb接口的缺点
    古董交换机欣赏
    博客圆低调的文章审核机制
    手机技术控不能买哪些手机,应该买什么手机
    两个以上vlan三台以上交换机配置好,网络不通,引发的思考
  • 原文地址:https://www.cnblogs.com/del/p/2007185.html
Copyright © 2020-2023  润新知