Beispiel #1
0
        public static Texture2D CircleWireFrame(Circle circle)
        {
            if (GraphicsDevice == null)
            {
                throw new MissingFieldException("Must set the GraphicsDevice field on WireFrames before any wire frames can be generated.");
            }

            int diameter = circle.Radius * 2;

            Color[] data = new Color[diameter * diameter];

            double delta = Math.Atan((double)1 / circle.Radius);

            for (double t = 0; t < 2 * Math.PI + delta; t += delta)
            {
                double y     = circle.Radius * (1 - Math.Sin(t));
                int    y_int = (int)AxisMath.Clamp(y, 0, diameter);
                double x     = circle.Radius * (1 + Math.Cos(t));
                int    x_int = (int)AxisMath.Clamp(x, 0, diameter);

                int i = y_int * diameter + x_int;

                data[i] = Color;
            }

            Texture2D result = new Texture2D(GraphicsDevice, diameter, diameter);

            result.SetData(data);
            return(result);
        }
Beispiel #2
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);
        }