2011-03-15 51 views
1

我正在WPF中使用不同的段类型(弧,贝塞尔,线段)绘制区域形状,并希望阻止它们创建复杂的区域形状。这就是说边缘重叠的形状。有没有好的方法来检查段在WPF中的PathFigure中是否重叠?

我正在使用由转换器生成的PathGeometry,但在转换器完成后,XAML看起来像下面的XAML。

在没有重叠:

<Path x:Name="PolygonPath" Fill="Blue" Opacity="75"> 
     <Path.Data> 
      <PathGeometry> 
       <PathGeometry.Figures> 
        <PathFigure StartPoint="50,50" IsClosed="True" IsFilled="True"> 
         <PathFigure.Segments> 
          <QuadraticBezierSegment Point1="100,0" Point2="200,50"/> 
          <LineSegment Point="250,50"/> 
          <LineSegment Point="250,200"/> 
          <QuadraticBezierSegment Point1="100,350" Point2="50,50"/> 
         </PathFigure.Segments> 
        </PathFigure> 
       </PathGeometry.Figures> 
      </PathGeometry> 
     </Path.Data> 
    </Path> 

随着重叠(应使测试失败):

<Path x:Name="PolygonPath" Fill="Blue" Opacity="75"> 
     <Path.Data> 
      <PathGeometry> 
       <PathGeometry.Figures> 
        <PathFigure StartPoint="50,50" IsClosed="True" IsFilled="True"> 
         <PathFigure.Segments> 
          <QuadraticBezierSegment Point1="100,0" Point2="200,60"/> 
          <LineSegment Point="0,60"/> 
          <LineSegment Point="250,200"/> 
          <QuadraticBezierSegment Point1="100,350" Point2="50,50"/> 
         </PathFigure.Segments> 
        </PathFigure> 
       </PathGeometry.Figures> 
      </PathGeometry> 
     </Path.Data> 
    </Path> 

在上述情况下,第二和第三线段<LineSegment Point="0,60"/><LineSegment Point="250,200"/>重叠的最后一个段<QuadraticBezierSegment Point1="100,350" Point2="50,50"/>

有没有一种方法我缺少测试路径是否与WPF中的任何点相交?

回答

1

我想你可以做的是:

得到你测试每一个的PathFigure边界矩形致电:

Rect rect = new PathGeometry(new PathFigure[] { figure }).Bounds; 

然后用rect.IntersectsWith方法来检查矩形不相交。

平稳这样的:

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:

<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> 

上方返回的代码:

如图一与figure2

figure2相交相交与figure3

此XAML

希望这会有所帮助,至于

+0

这是个不错的解决方案,但如果有多个数字它仅适用。我正在与一个有多个细分市场的人物合作。我会试着进一步澄清我的问题。谢谢。 – 2011-03-16 13:58:30

相关问题