2011-03-15 54 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

相關問題