如何检查圆是否位于凸多边形内部

Sav*_*ail 5 java geometry collision-detection

我想检查一个圆是否相交或位于凸多边形内。我找到了一种出色的方法来检测点是否在多边形内部(来自此处):

        public boolean insidePolygon(Vector2 [] vertices, Vector2 p)
        {
            int i, j;
            boolean c = false;
            int nvert = vertices.length;
            for (i = 0, j = nvert - 1; i < nvert; j = i++)
            {
                if (((vertices[i].Y > p.Y) != (vertices[j].Y > p.Y)) &&
                 (p.X < (vertices[j].X - vertices[i].X) * (p.Y - vertices[i].Y) / (vertices[j].Y - vertices[i].Y) + vertices[i].X))
                    c = !c;
            }
            return c;
        }
Run Code Online (Sandbox Code Playgroud)

这对于单个点来说非常有效,但是我们有什么方法可以修改它来检查给定半径的圆是否在多边形内部?我想这是可能的,因为圆实际上是一个点,但更大,但我仍然没有成功......

JTM*_*Mon 0

有很多方法可以用数学方法来实现,但我的方法是使用类Area。这可能不是性能方面最有效的方法,但速度足以满足我的需求,而且由于我不是数学奇才,所以没有数学部分是一个优点:)

public bool polygonContains circle (Shape poly, Shape circle){
    Area p = new Area(poly);
    if (!p.contains(circle.center)) return false;
    Area a = new Area(poly);        
    Area c = new Area(circle);
    a.add(c);
    //get the pathiterator of a and one for area p and compare the points they return
    // if there is a point in a not in p, it means circle jets out of polygon so return false.
    //if you care about the circle touching the inside of the polygon then 
    Area m = new Area(poly);
    Area s = m.substract(c);
    //repeat the pathiterator comparison for p and s , if there is a point in s not found in a
    //it means the circle touched the edge of the polygon return the value you see fit.
    return true;
}
Run Code Online (Sandbox Code Playgroud)