我正在使用 CombinedGeometry
将几个矩形合并在一起。这是我使用的代码。
它有点冗长,但它的要点是这里将两个矩形组合成一个几何图形,并将最终的几何图形传递给 Path.Data
。
现在,当我合并这些时,如下图所示,当两个矩形相互连接时,连接处相当难看。我想知道是否有办法让它看起来更漂亮一些,如红色标记所示?
<Canvas Width="1000" Height="1000">
<Path Stroke="Black" StrokeThickness="1" Fill="Transparent" x:Name="MyPath" >
</Path>
</Canvas>
public class MyRect
{
public double X1 { get; set; }
public double Y1 { get; set; }
public double X2 { get; set; }
public double Y2 { get; set; }
public double X3 { get; set; }
public double Y3 { get; set; }
public double X4 { get; set; }
public double Y4 { get; set; }
}
var cg = new CombinedGeometry { GeometryCombineMode = GeometryCombineMode.Union };
var rect1 = new MyRect { X1 = 500, Y1 = 120, X2 = 1000, Y2 = 120, X3 = 1000, Y3 = 80, X4 = 500, Y4 = 80 };
var rect2 = new MyRect { X1 = 480, Y1 = 100, X2 = 480, Y2 = 200, X3 = 520, Y3 = 200, X4 = 520, Y4 = 100 };
var list = new List<MyRect>
{
rect1, rect2
};
var geometry = new PathGeometry();
var myRec = list[0];
var la = new LineSegment { Point = new Point(myRec.X2, myRec.Y2) };
var lb = new LineSegment { Point = new Point(myRec.X3, myRec.Y3) };
var lc = new LineSegment { Point = new Point(myRec.X4, myRec.Y4) };
var pathFigure = new PathFigure
{
StartPoint = new Point(myRec.X1, myRec.Y1),
Segments = { la, lb, lc }
};
var path = new PathGeometry { Figures = { pathFigure } };
cg.Geometry1 = path;
for (var i = 1; i < list.Count; i++)
{
myRec = list[i];
la = new LineSegment { Point = new Point(myRec.X2, myRec.Y2) };
lb = new LineSegment { Point = new Point(myRec.X3, myRec.Y3) };
lc = new LineSegment { Point = new Point(myRec.X4, myRec.Y4) };
pathFigure = new PathFigure
{
StartPoint = new Point(myRec.X1, myRec.Y1),
Segments = { la, lb, lc }
};
path = new PathGeometry { Figures = { pathFigure } };
cg.Geometry2 = path;
geometry = cg.GetFlattenedPathGeometry();
cg.Geometry1 = geometry;
}
MyPath.Data = geometry;
最佳答案
走组合路径并合并边对,其中两条边都比某个 X 短?
X 可以设置为略低于任何矩形边的最小长度,因此您不会改变原始特征,只需清理图片中的小瑕疵。
取决于您的源数据以及您是否有任何不平凡的案例。
关于c# - 使用 CombinedGeometry 在 wpf 中连接两个多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23927610/