WPF Paths Collision detection?

流过昼夜 提交于 2021-02-07 04:11:00

问题


I have two hexagon shapes 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>

I need to detect collision between two hexagons

case 1 (collision) enter image description here

case 2 (collision) enter image description here

case 3 (No collision) enter image description here


回答1:


You should be able to get the intersection of two Paths by the FillContainsWithDetail method of their geometries:

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

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


来源:https://stackoverflow.com/questions/15602400/wpf-paths-collision-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!