public void TestCirclesIntersects() { //create a few circles Circle circle1 = new Circle { Center = new Point(0, 0), Radius = 2 }; Circle circle2 = new Circle { Center = new Point(0, 0), Radius = 3 }; Circle circle3 = new Circle { Center = new Point(6, 0), Radius = 3 }; Circle circle4 = new Circle { Center = new Point(1, 1), Radius = 3 }; //non-Intersectsing circles Assert.IsFalse(circle1.Intersects(circle3)); Assert.IsFalse(circle3.Intersects(circle1)); Assert.IsFalse(circle1.Intersects(null)); //touching circles Assert.IsTrue(circle2.Intersects(circle3)); Assert.IsTrue(circle3.Intersects(circle2)); //Intersectsing circles Assert.IsTrue(circle1.Intersects(circle2)); Assert.IsTrue(circle2.Intersects(circle1)); Assert.IsTrue(circle2.Intersects(circle4)); Assert.IsTrue(circle4.Intersects(circle2)); Assert.IsTrue(circle1.Intersects(circle4)); Assert.IsTrue(circle4.Intersects(circle1)); Assert.IsTrue(circle3.Intersects(circle4)); Assert.IsTrue(circle4.Intersects(circle3)); }
public bool Intersects(Circle circle) { //two circles intersect if the distance between the 2 centers is less than the sum of the 2 radii if (circle != null) { double centerDistance = Math.Sqrt(Math.Pow(circle.Center.X - Center.X, 2) + Math.Pow(circle.Center.Y - Center.Y, 2)); if (centerDistance <= circle.Radius + Radius) return true; } return false; }