C# 有没有一种好方法可以检查WPF中路径图中的线段是否重叠?
我正在使用WPF中的一个控件来使用不同的线段类型(圆弧、贝塞尔曲线、线段)绘制区域形状,并希望避免它们创建复杂的区域形状。也就是说边缘重叠的形状 我正在使用由转换器生成的C# 有没有一种好方法可以检查WPF中路径图中的线段是否重叠?,c#,wpf,path,geometry,intersection,C#,Wpf,Path,Geometry,Intersection,我正在使用WPF中的一个控件来使用不同的线段类型(圆弧、贝塞尔曲线、线段)绘制区域形状,并希望避免它们创建复杂的区域形状。也就是说边缘重叠的形状 我正在使用由转换器生成的PathGeometry,但是在转换器完成后,XAML看起来像下面的XAML 没有重叠: 重叠(如果测试失败): 在上述情况下,第二和第三线段和与最后一段重叠 是否有一种方法我没有测试路径是否在WPF中的任何点与自身相交?我想你可以做的是: 通过调用以下命令,为正在测试的每个PathFigure获取边界矩形: R
PathGeometry
,但是在转换器完成后,XAML看起来像下面的XAML
没有重叠:
重叠(如果测试失败):
在上述情况下,第二和第三线段
和
与最后一段
重叠
是否有一种方法我没有测试路径是否在WPF中的任何点与自身相交?我想你可以做的是: 通过调用以下命令,为正在测试的每个PathFigure获取边界矩形:
Rect rect = new PathGeometry(new PathFigure[] { figure }).Bounds;
然后用方法检查矩形是否相交
Smth是这样的:
Rect rect1 = new PathGeometry(new PathFigure[] { figure1 }).Bounds;
Rect rect2 = new PathGeometry(new PathFigure[] { figure2 }).Bounds;
Rect rect3 = new PathGeometry(new PathFigure[] { figure3 }).Bounds;
if (rect1.IntersectsWith(rect2))
Console.WriteLine("figure1 intersects with figure2");
if (rect1.IntersectsWith(rect3))
Console.WriteLine("figure1 intersects with figure3");
if (rect2.IntersectsWith(rect3))
Console.WriteLine("figure2 intersects with figure3");
xaml:
上面的代码返回:
图1与图2相交
图2与图3相交
对于这个xaml
希望这能有所帮助,因为这不是一个坏的解决方案,但只有在有多个数字的情况下才有效。我使用的是一个具有多个线段的图形。我会试着进一步澄清我的问题。非常感谢。
Rect rect = new PathGeometry(new PathFigure[] { figure }).Bounds;
Rect rect1 = new PathGeometry(new PathFigure[] { figure1 }).Bounds;
Rect rect2 = new PathGeometry(new PathFigure[] { figure2 }).Bounds;
Rect rect3 = new PathGeometry(new PathFigure[] { figure3 }).Bounds;
if (rect1.IntersectsWith(rect2))
Console.WriteLine("figure1 intersects with figure2");
if (rect1.IntersectsWith(rect3))
Console.WriteLine("figure1 intersects with figure3");
if (rect2.IntersectsWith(rect3))
Console.WriteLine("figure2 intersects with figure3");
<Canvas>
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="10,20" x:Name="figure1">
<PathFigure.Segments>
<LineSegment Point="100,130"/>
</PathFigure.Segments>
</PathFigure>
<PathFigure StartPoint="20,70" x:Name="figure2">
<PathFigure.Segments>
<LineSegment Point="200,70"/>
</PathFigure.Segments>
</PathFigure>
<PathFigure StartPoint="200,20" x:Name="figure3">
<PathFigure.Segments>
<LineSegment Point="130,130"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</Canvas>