WPF路径碰撞检测?

时间:2013-03-24 18:35:37

标签: c# wpf xaml collision-detection collision

我有两个六边形 Hex1 and Hex2

<Path Stroke="Black" HorizontalAlignment="Left" VerticalAlignment="Top" Fill="Red" StrokeThickness="1" Name="Hex1" Canvas.Left="31.343" Canvas.Top="26.866" Height="163.687" Stretch="Fill" Width="159.134" >
            <Path.Data>
                <PathGeometry >
                    <PathGeometry.Figures>
                        <PathFigureCollection >
                            <PathFigure StartPoint="43,0">
                                <PathFigure.Segments>
                                    <PathSegmentCollection >
                                        <PolyLineSegment Points="43,0"/>
                                        <PolyLineSegment Points="86,25"/>
                                        <PolyLineSegment Points="86,75"/>
                                        <PolyLineSegment Points="43,100"/>
                                        <PolyLineSegment Points="0,75"/>
                                        <PolyLineSegment Points="0,25"/>
                                        <PolyLineSegment Points="43,0"/>
                                    </PathSegmentCollection>
                                </PathFigure.Segments>
                            </PathFigure>
                        </PathFigureCollection>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>
        <Path Stroke="Black" HorizontalAlignment="Left" VerticalAlignment="Top" Fill="Aqua" StrokeThickness="1" Name="Hex2" Canvas.Left="455.224" Canvas.Top="210.448" Height="163.687" Stretch="Fill" Width="159.134" >
            <Path.Data>
                <PathGeometry >
                    <PathGeometry.Figures>
                        <PathFigureCollection >
                            <PathFigure StartPoint="43,0">
                                <PathFigure.Segments>
                                    <PathSegmentCollection >
                                        <PolyLineSegment Points="43,0"/>
                                        <PolyLineSegment Points="86,25"/>
                                        <PolyLineSegment Points="86,75"/>
                                        <PolyLineSegment Points="43,100"/>
                                        <PolyLineSegment Points="0,75"/>
                                        <PolyLineSegment Points="0,25"/>
                                        <PolyLineSegment Points="43,0"/>
                                    </PathSegmentCollection>
                                </PathFigure.Segments>
                            </PathFigure>
                        </PathFigureCollection>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>

我需要检测两个六边形之间的碰撞

案例1(碰撞) enter image description here

案例2(碰撞) enter image description here

案例3(无碰撞) enter image description here

1 个答案:

答案 0 :(得分:6)

您应该能够通过其几何的FillContainsWithDetail方法获得两个路径的交集:

var intersectionDetail = path1.Data.FillContainsWithDetail(path2.Data);

if (intersectionDetail != IntersectionDetail.NotCalculated &&
    intersectionDetail != IntersectionDetail.Empty)
{
    // collision
}