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

发布于 2024-10-22 00:58:12 字数 2284 浏览 4 评论 0原文

我正在 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,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="250,200"/> 与最后一段重叠

我是否缺少一种方法来测试路径是否在 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"/>
                            <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2024-10-29 00:58:12

我想你可以做的是:

通过调用为你正在测试的每个 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>

上面的代码返回:

图 1 与图 2 相交

图2与图3相交

对于这个xaml

希望这有帮助,问候

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文