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

は次のとおりです。

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

次に交差しない長方形のチェックするrect.IntersectsWithメソッドを使用します。

は、あなたが呼び出すことでテストしている各のPathFigureの境界矩形を取得します。このような

なめらか:

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> 

戻り上記コード:

図1.が図2.

図2.と交差が交差figure3

とこのことができます。このXAML

希望のため

、これは悪い解決策ではなく、複数の数字がある場合にのみ動作します

+0

について。私は複数のセグメントを持つ単一の図と一緒に作業しています。私はさらに質問を明確にしようとします。ありがとうございました。 –

関連する問題