Skip to content

Commit

Permalink
Additional Circle Test, Additional Circle Intersects
Browse files Browse the repository at this point in the history
  • Loading branch information
killerrin committed Jan 17, 2015
1 parent c297a49 commit d936dac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions MonoGame.Framework/Circle.cs
Expand Up @@ -364,6 +364,32 @@ public bool Intersects(Rectangle value)
return (distanceOfCorners <= (Radius * Radius));
}

/// <summary>
/// Gets whether or not a specified <see cref="Rectangle"/> intersects with this <see cref="Circle"/>.
/// </summary>
/// <param name="value">Other <see cref="Rectangle"/>.</param>
/// <param name="result"><c>true</c> if other <see cref="Rectangle"/> intersects with this <see cref="Circle"/>; <c>false</c> otherwise. As an output parameter.</param>
public void Intersects(ref Rectangle value, out bool result)
{
Vector2 distance = new Vector2(Math.Abs(Center.X - value.X), Math.Abs(Center.Y - value.Y));

if (distance.X > (value.Width / 2.0f + Radius))
result = false;
if (distance.Y > (value.Height / 2.0f + Radius))
result = false;

if (distance.X <= (value.Width / 2.0f))
result = true;
if (distance.Y <= (value.Height / 2.0f))
result = true;


float distanceOfCorners = ((distance.X - value.Width / 2.0f) * (distance.X - value.Width / 2.0f) +
(distance.Y - value.Height / 2.0f) * (distance.Y - value.Height / 2.0f));

result = (distanceOfCorners <= (Radius * Radius));
}

/*
/// <summary>
/// Creates a new <see cref="Circle"/> that contains overlapping region of two other circles.
Expand Down
30 changes: 30 additions & 0 deletions Test/Framework/CircleTest.cs
Expand Up @@ -171,6 +171,36 @@ public void ContainsCircle()
Assert.AreEqual(false, circle.Contains(circ4));
}

[Test]
public void IntersectionTest()
{
var circle = new Circle(new Vector2(200.0f, 300.0f), 100.0f);

var circ1 = new Circle(new Vector2(350.0f, 300.0f), 100.0f);
var circ2 = new Circle(new Vector2(400.0f, 300.0f), 100.0f);

var rect1 = new Rectangle(250, 300, 100, 100);
var rect2 = new Rectangle(400, 300, 100, 100);

bool result;

circle.Intersects(ref circ1, out result);
Assert.AreEqual(true, result);
circle.Intersects(ref circ2, out result);
Assert.AreEqual(false, result);

circle.Intersects(ref rect1, out result);
Assert.AreEqual(true, result);
circle.Intersects(ref rect2, out result);
Assert.AreEqual(false, result);

Assert.AreEqual(true, circle.Intersects(circ1));
Assert.AreEqual(false, circle.Intersects(circ2));

Assert.AreEqual(true, circle.Intersects(rect1));
Assert.AreEqual(false, circle.Intersects(rect2));
}

[Test]
public void Inflate()
{
Expand Down

2 comments on commit d936dac

@mgbot
Copy link
Member

@mgbot mgbot commented on d936dac Jan 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity MonoGame :: Develop (Win) Build 3.3.0.1904 is now running

@mgbot
Copy link
Member

@mgbot mgbot commented on d936dac Jan 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity MonoGame :: Develop (Win) Build 3.3.0.1904 outcome was FAILURE
Summary: Tests failed: 1 (1 new), passed: 375, ignored: 6 Build time: 00:10:08

Failed tests

MonoGameTests.exe: MonoGame.Tests.Framework.CircleTest.ToStringTest: Test(s) failed.   Expected string length 35 but was 33. Strings differ at index 9.
  Expected: "{Center:{{X:200 Y:300}} Radius:100}"
  But was:  "{Center:{X:200 Y:300} Radius:100}"

Please sign in to comment.