有没有一种好方法可以检查WPF中PathFigure中的段是否重叠?

时间:2011-03-15 22:24:21

标签: c# wpf path geometry intersection

我正在使用WPF中的控件来使用不同的段类型(弧,贝塞尔曲线,线段)绘制区域形状,并希望防止它们创建复杂的区域形状。也就是说边缘重叠的形状。

我正在使用转换器生成的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,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>

在上述情况下,第二和第三个线段 <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"/>重叠。

我是否缺少一个方法来测试路径在WPF中的任何一点是否与自身相交?

1 个答案:

答案 0 :(得分:1)

我猜你能做的是:

通过调用:

为您正在测试的每个PathFigure获取一个bounds矩形
Rect rect = new PathGeometry(new PathFigure[] { figure }).Bounds;

然后使用rect.IntersectsWith方法检查矩形是否相交。

像这样的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:

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

上面的代码返回:

  

figure1与figure2相交

     

figure2与figure3相交

这个xaml

希望这有帮助,尊重

相关问题