如何检查2个WPF路径(应用了MatrixTransform)是否重叠

时间:2012-01-07 14:25:55

标签: wpf multi-touch overlap

在我的WPF多点触控应用程序中,我想检查路径是否与另一个路径重叠。

我该怎么做?我在互联网上找到了不同的解决方案,但由于需求的组合,所以没有一种解决方案:

  • 2不规则形状的路径(因此边界不起作用:没有矩形)
  • 在路径上应用MatrixTransform(由于操纵事件)
  • 路径可以旋转和缩放(使用MatrixTransform)
  • RenderTransformOrigin为0.5,0.5

我该怎么做?

示例:在此示例中,蓝色和红色星形重叠,红色和绿色不重叠。

<Window x:Class="WPFTest.OtherWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="OtherWindow" Height="300" Width="300" >
<Canvas >
    <Path x:Name="path1" Data="M17,0 L23,11 34,13 27,21 28,32 18,24 8,30 9,19 0,11 13,11 z" Fill="#99FF0000" Stroke="Black" RenderTransformOrigin="0.5,0.5">
        <Path.RenderTransform>
            <MatrixTransform Matrix="1,0,0,1,50,50"/>
        </Path.RenderTransform>
    </Path>
    <Path x:Name="path2" Data="M17,0 L23,11 34,13 27,21 28,32 18,24 8,30 9,19 0,11 13,11 z" Fill="#990000FF" Stroke="Black" RenderTransformOrigin="0.5,0.5">
        <Path.RenderTransform>
            <MatrixTransform Matrix="0.777817459305202,0.777817459305202,-0.777817459305202,0.777817459305202,35,45"/>
        </Path.RenderTransform>
    </Path>
    <Path x:Name="path3" Data="M17,0 L23,11 34,13 27,21 28,32 18,24 8,30 9,19 0,11 13,11 z" Fill="#9900FF00" Stroke="Black" RenderTransformOrigin="0.5,0.5">
        <Path.RenderTransform>
            <MatrixTransform Matrix="0.777817459305202,0.777817459305202,-0.777817459305202,0.777817459305202,82,55"/>
        </Path.RenderTransform>
    </Path>
</Canvas>

如何从后面的代码中查看?

谢谢,

吉姆

1 个答案:

答案 0 :(得分:0)

如果可以接受,您可以使用Path.Data的变换几何而不是(渲染 - )变换路径(当然缺点是它们的笔划不会缩放)。 一旦使用了这样的变换几何,就可以通过Geometry.FillContainsWithDetail确定它们的相互交集,它为你提供了交集类型的枚举值。

您的XAML可能如下所示。请注意,因为几何体不具有变换原点,所以我修改了几何体,使坐标原点位于其中心。我还将结果偏移量添加到MatrixTransform偏移值。

<Canvas>
    <Path Name="redStar" Fill="#99FF0000" Stroke="Black">
        <Path.Data>
            <PathGeometry Figures="M0,-16 L6,-5 17,-3 10,5 11,16 1,8 -9,14 -8,3 -17,-5 -4,-5 z">
                <PathGeometry.Transform>
                    <MatrixTransform Matrix="1,0,0,1,67,66"/>
                </PathGeometry.Transform>
            </PathGeometry>
        </Path.Data>
    </Path>
    <Path Name="blueStar" Fill="#990000FF" Stroke="Black">
        <Path.Data>
            <PathGeometry Figures="M0,-16 L6,-5 17,-3 10,5 11,16 1,8 -9,14 -8,3 -17,-5 -4,-5 z">
                <PathGeometry.Transform>
                    <MatrixTransform Matrix="0.777817459305202,0.777817459305202,-0.777817459305202,0.777817459305202,52,61"/>
                </PathGeometry.Transform>
            </PathGeometry>
        </Path.Data>
    </Path>
    <Path Name="greenStar" Fill="#9900FF00" Stroke="Black">
        <Path.Data>
            <PathGeometry Figures="M0,-16 L6,-5 17,-3 10,5 11,16 1,8 -9,14 -8,3 -17,-5 -4,-5 z">
                <PathGeometry.Transform>
                    <MatrixTransform Matrix="0.777817459305202,0.777817459305202,-0.777817459305202,0.777817459305202,99,71"/>
                </PathGeometry.Transform>
            </PathGeometry>
        </Path.Data>
    </Path>
</Canvas>

以下代码将确定交叉点:

Geometry redGeometry = redStar.Data;
Geometry blueGeometry = blueStar.Data;
Geometry greenGeometry = greenStar.Data;

Trace.TraceInformation("red/blue intersection: {0}", redGeometry.FillContainsWithDetail(blueGeometry));
Trace.TraceInformation("blue/green intersection: {0}", blueGeometry.FillContainsWithDetail(greenGeometry));
Trace.TraceInformation("green/red intersection: {0}", greenGeometry.FillContainsWithDetail(redGeometry));

redGeometry.Transform = new MatrixTransform(1, 0, 0, 1, 70, 90);

Trace.TraceInformation("red/blue intersection: {0}", redGeometry.FillContainsWithDetail(blueGeometry));