C# 获取组合几何的点坐标
我使用下面的代码从组合几何中排除一个几何体 并获得最终几何图形,如下所示: 我想知道有没有办法得到这个最终结果的几何定义?类似点集合之类的东西,可以帮助我以后用不同的属性重新绘制,甚至可以将点坐标写入文件C# 获取组合几何的点坐标,c#,wpf,xaml,C#,Wpf,Xaml,我使用下面的代码从组合几何中排除一个几何体 并获得最终几何图形,如下所示: 我想知道有没有办法得到这个最终结果的几何定义?类似点集合之类的东西,可以帮助我以后用不同的属性重新绘制,甚至可以将点坐标写入文件 <Window x:Class="Combine.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas
<Window x:Class="Combine.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas Width="300" Height="300">
<Path Stroke="Black" StrokeThickness="1" Fill="Transparent">
<Path.Data>
<CombinedGeometry GeometryCombineMode="Exclude">
<CombinedGeometry.Geometry1>
<RectangleGeometry Rect="40,40, 200, 50" />
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<RectangleGeometry Rect="100,0, 50, 200" />
</CombinedGeometry.Geometry2>
</CombinedGeometry>
</Path.Data>
</Path>
</Canvas>
</Window>
您可以尝试使用
combinedGeometry.getFlattedPathGeometry()代码>方法。这将返回PathGeometry对象。然后,您可以将其转换为字符串并写入文件,或者运行PathGeometry中包含的图形并使用坐标
PathGeometry geometry = combinedGeometry.GetFlattenedPathGeometry();
Console.WriteLine(geometry.ToString());
foreach (PathFigure figure in geometry.Figures)
{
Console.WriteLine(figure.StartPoint);
foreach (PathSegment segment in figure.Segments)
{
foreach (Point point in ((PolyLineSegment)segment).Points)
{
Console.WriteLine(point);
}
}
}
您还可以使用PathGeometry.Combine()代替CombinedGeometry。生成的PathGeometry包含图形和线段。PS:您还可以从任何其他几何体创建PathGeometry。查看静态构造函数PathGeometry.CreateFromGeometry()。
PathGeometry结果=PathGeometry.Combine(g1、g2、GeometryCombineMode.Exclude、null)