tasting on MonoGame
[tastes.git] / Platformer2D / Platformer2D.Core / Game / Circle.cs
blob908ebdb4933451da136a29dd723bdf5588596f0a
1 #region File Description
2 //-----------------------------------------------------------------------------
3 // Circle.cs
4 //
5 // Microsoft XNA Community Game Platform
6 // Copyright (C) Microsoft Corporation. All rights reserved.
7 //-----------------------------------------------------------------------------
8 #endregion
10 using System;
11 using Microsoft.Xna.Framework;
13 namespace Platformer2D
15 /// <summary>
16 /// Represents a 2D circle.
17 /// </summary>
18 struct Circle
20 /// <summary>
21 /// Center position of the circle.
22 /// </summary>
23 public Vector2 Center;
25 /// <summary>
26 /// Radius of the circle.
27 /// </summary>
28 public float Radius;
30 /// <summary>
31 /// Constructs a new circle.
32 /// </summary>
33 public Circle(Vector2 position, float radius)
35 Center = position;
36 Radius = radius;
39 /// <summary>
40 /// Determines if a circle intersects a rectangle.
41 /// </summary>
42 /// <returns>True if the circle and rectangle overlap. False otherwise.</returns>
43 public bool Intersects(Rectangle rectangle)
45 Vector2 v = new Vector2(MathHelper.Clamp(Center.X, rectangle.Left, rectangle.Right),
46 MathHelper.Clamp(Center.Y, rectangle.Top, rectangle.Bottom));
48 Vector2 direction = Center - v;
49 float distanceSquared = direction.LengthSquared();
51 return ((distanceSquared > 0) && (distanceSquared < Radius * Radius));