How do I detect intersections between a circle and any other circle in the same plane?

前端 未结 7 2242
走了就别回头了
走了就别回头了 2020-11-28 05:54

I\'m looking for an algorithm to detect if a circle intersects with any other circle in the same plane (given that there can be more than one circle in a plane).

One

7条回答
  •  醉梦人生
    2020-11-28 06:04

    XNA / C# solution

        class Circle
        {
            public Vector2 Center;
            public float Radius;
    
            public bool Intersects(Circle circle)
            {
                float distanceX = Center.X - circle.Center.X;
                float distanceY = Center.Y - circle.Center.Y;
                float radiusSum = circle.Radius + Radius;
                return distanceX * distanceX + distanceY * distanceY <= radiusSum * radiusSum;
            }
            public bool Contains(Circle circle)
            {
                if (circle.Radius > Radius)
                    return false;
                float distanceX = Center.X - circle.Center.X;
                float distanceY = Center.Y - circle.Center.Y;
                float radiusD = Radius - circle.Radius;
                return distanceX * distanceX + distanceY * distanceY <= radiusD * radiusD;
            }
        }

    Note that method Circle.Intersects() returns true even if one circle is within another (treats them as "filled" circles).

提交回复
热议问题