方法FillContainsWithDetail无法按预期工作

时间:2018-05-17 17:36:53

标签: c# wpf intersection

当第一个形状没有触及另一个形状时,FillContainsWithDetail方法应该返回Empty,它必须返回FullyContains。但是当我做一些测试时,我发现它不是它返回的......

这是触摸或不触摸形状时返回的方法。

public string CheckIfInside(Shape shpPlayer, Shape shpObject)
{
    Geometry geo1 = shpPlayer.RenderedGeometry;
    Geometry geo2 = shpObject.RenderedGeometry;

    string s = geo1.FillContainsWithDetail(geo2).ToString();
    return s;
}

上述方法的返回

    [0] "Empty" 
    [1] "Empty" 
    [2] "Intersects"    
    [3] "Intersects"    
    [4] "Intersects"    
    [5] "Empty" 
    [6] "Empty" 
    [7] "Intersects"    
    [8] "Intersects"    
    [9] "FullyInside"   
    [10]    "Empty" 
    [11]    "Empty" 
    [12]    "FullyInside"   
    [13]    "Empty" 
    [14]    "Empty" 
    [15]    "FullyInside"   
    [16]    "Empty" 
    [17]    "Empty" 
    [18]    "FullyInside"   
    [19]    "Empty" 
    [20]    "Empty" 
    [21]    "FullyInside"   
    [22]    "Empty" 
    [23]    "Empty" 
    [24]    "FullyInside"   
    [25]    "Empty" 
    [26]    "Empty" 
    [27]    "FullyInside"   
    [28]    "Empty" 
    [29]    "Empty" 
    [30]    "FullyInside"   
    [31]    "Empty" 
    [32]    "Empty" 
    [33]    "FullyInside"   
    [34]    "Empty" 
    [35]    "Empty" 
    [36]    "FullyInside"   
    [37]    "Empty" 
    [38]    "Empty" 
    [39]    "FullyInside"   
    [40]    "Empty" 
    [41]    "Empty" 
    [42]    "FullyInside"   
    [43]    "Empty" 
    [44]    "Empty" 
    [45]    "FullyInside"   
    [46]    "Empty" 
    [47]    "Empty" 
    [48]    "FullyInside"   
    [49]    "Empty" 
    [50]    "Empty" 

Screenshot of the game(播放器[shpPlayer / geo1]是红色的小立方体,形状是树木和湖泊)

感谢您的帮助:D

1 个答案:

答案 0 :(得分:0)

我终于找到了答案。问题是我使用RenderedGeometry来获取Geometry,但它并没有真正返回Geomerty ......至少不是我想要的。所以,如果这可以帮到你,我就会采用自己的方法来做到这一点。

private Geometry ConvertToGeometry(Shape s)
    {
        if (s.GetType() == new Rectangle().GetType())
        {
            return new RectangleGeometry(new Rect(new Point(s.Margin.Left, s.Margin.Top), new Point(s.Margin.Left + s.Width, s.Margin.Top + s.Height)));
        }
        if (s.GetType() == new Ellipse().GetType())
        {
            return new EllipseGeometry(new Point(s.Width / 2 + s.Margin.Left, s.Height / 2 + s.Margin.Top), s.Width / 2, s.Height / 2);
        }
        if (s.GetType() == new Polygon().GetType())
        {
            Polygon p = (Polygon)s;
            List<PathSegment> ps = new List<PathSegment>();
            for (int i = 1; i < p.Points.Count; i++)
            {
                ps.Add(new LineSegment(p.Points[i], true));
            }
            PathGeometry pg = new PathGeometry(new PathFigure[] { new PathFigure(p.Points[0], ps, true) });
            return pg;
        }
        return null;
    }

此方法适用于Shapes中的Rectangle,Ellipse和Polygon。 感谢所有帮助过我的人:D