예제 #1
0
        public void Circle_ContainsPoint()
        {
            var circle = new Circle(new Vector2(200.0f, 300.0f), 100.0f);

            var p1 = new Point(-1, -1);
            var p2 = new Point(110, 300);
            var p3 = new Point(200, 300);
            var p4 = new Point(290, 300);
            var p5 = new Point(400, 400);

            bool result;

            circle.Contains(ref p1, out result);
            Assert.AreEqual(false, result);
            circle.Contains(ref p2, out result);
            Assert.AreEqual(true, result);
            circle.Contains(ref p3, out result);
            Assert.AreEqual(true, result);
            circle.Contains(ref p4, out result);
            Assert.AreEqual(true, result);
            circle.Contains(ref p5, out result);
            Assert.AreEqual(false, result);

            Assert.AreEqual(false, circle.Contains(p1));
            Assert.AreEqual(true, circle.Contains(p2));
            Assert.AreEqual(true, circle.Contains(p3));
            Assert.AreEqual(true, circle.Contains(p4));
            Assert.AreEqual(false, circle.Contains(p5));
        }
예제 #2
0
        public void Circle_ContainsFloats()
        {
            var circle = new Circle(new Vector2(200.0f, 300.0f), 100.0f);

            float x1 = -1; float y1 = -1;
            float x2 = 110; float y2 = 300;
            float x3 = 200; float y3 = 300;
            float x4 = 290; float y4 = 300;
            float x5 = 400; float y5 = 400;

            Assert.AreEqual(false, circle.Contains(x1, y1));
            Assert.AreEqual(true, circle.Contains(x2, y2));
            Assert.AreEqual(true, circle.Contains(x3, y3));
            Assert.AreEqual(true, circle.Contains(x4, y4));
            Assert.AreEqual(false, circle.Contains(x5, y5));
        }
예제 #3
0
        public void Circle_ContainsCircle()
        {
            var circle = new Circle(new Vector2(200.0f, 300.0f), 100.0f);
            var circle1 = new Circle(new Vector2(199.0f, 299.0f), 100.0f);
            var circle2 = new Circle(new Vector2(200.0f, 300.0f), 25.0f);
            var circle3 = new Circle(new Vector2(200.0f, 300.0f), 100.0f);
            var circle4 = new Circle(new Vector2(201.0f, 301.0f), 100.0f);

            bool result;

            circle.Contains(ref circle1, out result);
            Assert.AreEqual(false, result);

            circle.Contains(ref circle2, out result);
            Assert.AreEqual(true, result);

            circle.Contains(ref circle3, out result);
            Assert.AreEqual(true, result);

            circle.Contains(ref circle4, out result);
            Assert.AreEqual(false, result);

            Assert.AreEqual(false, circle.Contains(circle1));
            Assert.AreEqual(true, circle.Contains(circle2));
            Assert.AreEqual(true, circle.Contains(circle3));
            Assert.AreEqual(false, circle.Contains(circle4));
        }
예제 #4
0
        public static bool Collides(Rectangle r, Circle c)
        {
            // check if the center of the circle is in the rectangle
            if (r.Contains(c.Center))
                return true;

            // check if the circle's "outer box" doesn't intersect the rectangle
            Rectangle outerBounds = new Rectangle(new Point(c.Left, c.Top), new Point(c.Radius * 2));
            if (!r.Intersects(outerBounds))
                return false;

            // check if the circle contains any of the rectangle's corners
            Point corner = new Point(r.Left, r.Top);
            if (c.Contains(corner))
                return true;
            corner.X = r.Right;
            if (c.Contains(corner))
                return true;
            corner.Y = r.Bottom;
            if (c.Contains(corner))
                return true;
            corner.X = r.Left;
            if (c.Contains(corner))
                return true;

            // check for a top/bottom rectangle edge collision
            if(AxisMath.Between(c.Center.X, r.Left, r.Right))
            {
                // check top edge
                if (c.Bottom >= r.Top)
                    return true;

                // check bottom edge
                if (c.Top <= r.Bottom)
                    return true;
            }

            // check for a left/right rectangle edge collision
            if(AxisMath.Between(c.Center.Y, r.Top, r.Bottom))
            {
                // check left edge
                if (c.Right >= r.Left)
                    return true;

                // check right edge
                if (c.Left <= r.Right)
                    return true;
            }

            // if all else fails, return false
            return false;
        }