Is there a good way to check if segments overlap in a PathFigure in WPF?
I'm working on a control in WPF to draw area shapes using the different segment types (arc, bezier, line segment) and want to keep them from creating area shapes that are complex. That is to say shapes where the edges overlap.
I am working with a PathGeometry
generated by a converter, but after the converter is finished the XAML would look like the following XAML.
With no overlaps:
<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>
With overlaps (should fail the test):
<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"/>
开发者_JS百科 <LineSegment Point="0,60"/>
<LineSegment Point="250,200"/>
<QuadraticBezierSegment Point1="100,350" Point2="50,50"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
In the above case, the second and third line segments <LineSegment Point="0,60"/>
and <LineSegment Point="250,200"/>
overlap the last segment <QuadraticBezierSegment Point1="100,350" Point2="50,50"/>
.
Is there a method I am missing to test if a path intersects with itself at any point in WPF?
I guess what you could do is:
get a bounds rectangle for each PathFigure you're testing by calling:
Rect rect = new PathGeometry(new PathFigure[] { figure }).Bounds;
Then use rect.IntersectsWith method to check of rectangles do intersect.
Smth like this:
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>
the code above returns:
figure1 intersects with figure2
figure2 intersects with figure3
for this xaml
hope this helps, regards
精彩评论