Example #1
0
        static public RectI2 GetSlice(this RectI2 item, int amount, RectSide side)
        {
            switch (side)
            {
            case RectSide.Left: return(item.GetLeftSlice(amount));

            case RectSide.Right: return(item.GetRightSlice(amount));

            case RectSide.Bottom: return(item.GetBottomSlice(amount));

            case RectSide.Top: return(item.GetTopSlice(amount));
            }

            throw new UnaccountedBranchException("side", side);
        }
Example #2
0
        static public float GetSide(this RectF2 item, RectSide side)
        {
            switch (side)
            {
            case RectSide.Left: return(item.GetLeft());

            case RectSide.Right: return(item.GetRight());

            case RectSide.Bottom: return(item.GetBottom());

            case RectSide.Top: return(item.GetTop());
            }

            throw new UnaccountedBranchException("side", side);
        }
Example #3
0
        static public Rect GetExtrusion(this Rect item, float amount, RectSide side)
        {
            switch (side)
            {
            case RectSide.Left: return(item.GetLeftExtrusion(amount));

            case RectSide.Right: return(item.GetRightExtrusion(amount));

            case RectSide.Bottom: return(item.GetBottomExtrusion(amount));

            case RectSide.Top: return(item.GetTopExtrusion(amount));
            }

            throw new UnaccountedBranchException("side", side);
        }
Example #4
0
        static public void SplitBySideOffset(this RectI2 item, int offset, RectSide side, out RectI2 center, out RectI2 edge)
        {
            switch (side)
            {
            case RectSide.Left: item.SplitByXLeftOffset(offset, out edge, out center); return;

            case RectSide.Right: item.SplitByXRightOffset(offset, out center, out edge); return;

            case RectSide.Bottom: item.SplitByYBottomOffset(offset, out edge, out center); return;

            case RectSide.Top: item.SplitByYTopOffset(offset, out center, out edge); return;
            }

            throw new UnaccountedBranchException("side", side);
        }
Example #5
0
        static public Rect GetEnlarged(this Rect item, float amount, RectSide side)
        {
            switch (side)
            {
            case RectSide.Left: return(item.GetEnlargedLeft(amount));

            case RectSide.Right: return(item.GetEnlargedRight(amount));

            case RectSide.Bottom: return(item.GetEnlargedBottom(amount));

            case RectSide.Top: return(item.GetEnlargedTop(amount));
            }

            throw new UnaccountedBranchException("side", side);
        }
Example #6
0
        static public RectI2 ShrinkwrapSide(this Texture2D item, RectI2 rect, RectSide side)
        {
            RectI2 center;
            RectI2 edge;

            do
            {
                rect.SplitBySideOffset(1, side, out center, out edge);

                if (item.IsSolid(edge))
                {
                    return(rect);
                }

                rect = center;
            }while (center.IsCollapsed() == false);

            return(center);
        }
        /// <summary>
        /// Author: Napoleon.
        /// Tested and works.
        /// Only apply this to non-rotated rectangles.
        /// Touching rectangles don't count in this function
        /// </summary>
        /// <param name="movingRect">Usually the player or moving object.</param>
        /// <param name="staticRect">Usually the wall or something. The collided sides are the sides from this rectangle</param>
        /// <returns></returns>
        /// <example>
        ///  if ((collidedSides & Collision.RectSide.Top) == Collision.RectSide.Top)
        ///    // Top side was hit, do something here
        /// </example>
        public static RectSide CollidedSidesNoTouch(this FRect movingRect, FRect staticRect)
        {
            RectSide result = RectSide.None;

            if (!movingRect.Intersects(staticRect))
            {
                return(result); // No intersection at all
            }
            if (movingRect == staticRect)
            {
                return(RectSide.Equal);
            }

            // Left
            if (movingRect.Right > staticRect.Left && movingRect.Left < staticRect.Left)
            {
                result = (result | RectSide.Left);
            }

            // Top
            if (movingRect.Bottom > staticRect.Top && movingRect.Top < staticRect.Top)
            {
                result = (result | RectSide.Top);
            }

            // Right
            if (movingRect.Left < staticRect.Right && movingRect.Right > staticRect.Right)
            {
                result = (result | RectSide.Right);
            }

            // Bottom
            if (movingRect.Top < staticRect.Bottom && movingRect.Bottom > staticRect.Bottom)
            {
                result = (result | RectSide.Bottom);
            }

            if (result == RectSide.None)
            {
                return(RectSide.Inside); // moving rectangle is inside the static rectangle
            }
            return(result);
        }
Example #8
0
        /// <summary>
        /// Calculates intersection point between rectangle and line connecting center of rectangle
        /// with target point
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="target"></param>
        /// <param name="rs"></param>
        /// <returns></returns>
        public static PointF GetConnectorPoint(RectangleF rect, PointF target, out RectSide rs)
        {
            PointF center = new PointF(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
            SizeF  vector = new SizeF(target.X - center.X, target.Y - center.Y);
            PointF border = new PointF();

            rs = RectSide.A;
            bool b;

            if (vector.Width == 0 && vector.Height == 0)
            {
                return(center);
            }
            b = Math.Abs(vector.Width / rect.Width) > Math.Abs(vector.Height / rect.Height);

            rs = b ? (vector.Width < 0 ? RectSide.C : RectSide.A)
                : (vector.Height < 0 ? RectSide.D : RectSide.B);

            switch (rs)
            {
            case RectSide.A:
                border.X = rect.Right;
                border.Y = center.Y + vector.Height / vector.Width * (rect.Width / 2);
                break;

            case RectSide.B:
                border.X = center.X + vector.Width / vector.Height * (rect.Height / 2);
                border.Y = rect.Bottom;
                break;

            case RectSide.C:
                border.X = rect.Left;
                border.Y = center.Y - vector.Height / vector.Width * (rect.Width / 2);
                break;

            case RectSide.D:
                border.X = center.X - vector.Width / vector.Height * (rect.Height / 2);
                border.Y = rect.Top;
                break;
            }

            return(border);
        }
Example #9
0
        internal static double GetSideAxisValue(Rect rect, RectSide side)
        {
            switch (side)
            {
            case RectSide.Left:
                return(rect.Left);

            case RectSide.Top:
                return(rect.Top);

            case RectSide.Right:
                return(rect.Right);

            case RectSide.Bottom:
                return(rect.Bottom);

            default:
                throw new ArgumentOutOfRangeException(nameof(side), side, null);
            }
        }
Example #10
0
        void DrawLine(Graphics g, Pen pen,
                      RectangleF rect, RectSide side)
        {
            float x1 = 0, y1 = 0, x2 = 0, y2 = 0;

            if (pen != null)
            {
                switch (side)
                {
                case RectSide.Top:
                    x1 = rect.Left;
                    x2 = rect.Right;
                    y1 = y2 = rect.Top + pen.Width / 2;
                    break;

                case RectSide.Right:
                    x1 = x2 = rect.Right - pen.Width / 2;
                    y1 = rect.Top;
                    y2 = rect.Bottom;
                    break;

                case RectSide.Bottom:
                    x1 = rect.Left;
                    x2 = rect.Right;
                    y1 = y2 = rect.Bottom - pen.Width / 2;
                    break;

                case RectSide.Left:
                    x1 = x2 = rect.Left + pen.Width / 2;
                    y1 = rect.Top;
                    y2 = rect.Bottom;
                    break;
                }
                g.DrawLine(pen, x1, y1, x2, y2);
            }
        }
Example #11
0
        /// <summary>
        /// check if this is a simple rect
        /// </summary>
        /// <param name="vxs"></param>
        /// <returns></returns>
        public static bool EvaluateRectClip(VertexStore vxs, out RectangleF clipRect)
        {
            float x0 = 0, y0 = 0;
            float x1 = 0, y1 = 0;
            float x2 = 0, y2 = 0;
            float x3 = 0, y3 = 0;
            float x4 = 0, y4 = 0;

            clipRect = new RectangleF();

            int sideCount = 0;

            int j = vxs.Count;

            for (int i = 0; i < j; ++i)
            {
                VertexCmd cmd = vxs.GetVertex(i, out double x, out double y);
                switch (cmd)
                {
                default: return(false);

                case VertexCmd.NoMore:
                    if (i > 6)
                    {
                        return(false);
                    }
                    break;

                case VertexCmd.Close:
                    if (i > 5)
                    {
                        return(false);
                    }
                    break;

                case VertexCmd.LineTo:
                {
                    switch (i)
                    {
                    case 1:
                        x1 = (float)x;
                        y1 = (float)y;
                        sideCount++;
                        break;

                    case 2:
                        x2 = (float)x;
                        y2 = (float)y;
                        sideCount++;
                        break;

                    case 3:
                        x3 = (float)x;
                        y3 = (float)y;
                        sideCount++;
                        break;

                    case 4:
                        x4 = (float)x;
                        y4 = (float)y;
                        sideCount++;
                        break;
                    }
                }
                break;

                case VertexCmd.MoveTo:
                {
                    if (i == 0)
                    {
                        x0 = (float)x;
                        y0 = (float)y;
                    }
                    else
                    {
                        return(false);
                    }
                }
                break;
                }
            }

            if (sideCount == 4)
            {
                RectSide s0 = FindRectSide(x0, y0, x1, y1);
                if (s0 == RectSide.None)
                {
                    return(false);
                }
                //
                RectSide s1 = FindRectSide(x1, y1, x2, y2);
                if (s1 == RectSide.None || s0 == s1)
                {
                    return(false);
                }
                //
                RectSide s2 = FindRectSide(x2, y2, x3, y3);
                if (s2 == RectSide.None || s1 == s2)
                {
                    return(false);
                }
                //
                RectSide s3 = FindRectSide(x3, y3, x4, y4);
                if (s3 == RectSide.None || s2 == s3)
                {
                    return(false);
                }
                //
                if (x4 == x0 && y4 == y0)
                {
                    if (s0 == RectSide.Horizontal)
                    {
                        clipRect = new RectangleF(x0, y0, x1 - x0, y3 - y0);
                    }
                    else
                    {
                        clipRect = new RectangleF(x0, y0, x3 - x0, y3 - y0);
                    }

                    return(true);
                }
            }
            return(false);
        }
 public static bool IsNotSet(this RectSide sides, RectSide flags)
 {
     return((sides & (~flags)) == 0);
 }
Example #13
0
 private ArtboardRectSnapLine CreateSnapLine(RectSide rectSide)
 {
     return(new ArtboardRectSnapLine(rectSide, this));
 }
Example #14
0
 public ArtboardRectSnapLine(RectSide side, ArtboardSnapRectSource source) : base(source)
 {
     Side = side;
 }
 public static RectSide Set(this RectSide sides, RectSide flags)
 {
     return(sides | flags);
 }
		/// <summary>
		/// Checks whether the object leaves a rectangle.
		/// </summary>
		/// <param name="side">Rectangle side.</param>
		/// <param name="rect">Rectangle.</param>
		/// <param name="absolute">Flag indicating whether rectangle coordinates are screen-bound or playfield-bound.</param>
		/// <returns></returns>
		public bool LeavesRect(RectSide side, Rectangle rect, bool absolute)
		{
			var box = GetBoundingBox(absolute);

			return (side.HasFlag(RectSide.Top) && leavesRectFromTop(rect, box))
				|| (side.HasFlag(RectSide.Left) && leavesRectFromLeft(rect, box))
				|| (side.HasFlag(RectSide.Right) && leavesRectFromRight(rect, box))
				|| (side.HasFlag(RectSide.Bottom) && leavesRectFromBottom(rect, box));
		}
		/// <summary>
		/// Checks if the object has just left the playfield.
		/// The object must leave it naturally using it's momentum.
		/// </summary>
		/// <param name="side">Side of the playfield.</param>
		/// <returns></returns>
		public bool LeavesPlayfield(RectSide side)
		{
			return LeavesRect(side, GameStoryBoard.CurrentScene.LevelRect, true);
		}
		/// <summary>
		/// Checks if the object has just left the screen.
		/// The object must leave it naturally using it's momentum.
		/// </summary>
		/// <param name="side">Side of the screen.</param>
		/// <returns></returns>
		public bool LeavesScreen(RectSide side)
		{
			return LeavesRect(side, GameCore.ScreenRect, false);
		}
 public static bool IsSet(this RectSide sides, RectSide flags)
 {
     return((sides & flags) == flags);
 }
Example #20
0
 void DrawLine(Graphics g, Pen pen, 
     RectangleF rect, RectSide side)
 {
     float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
     if (pen != null)
     {
         switch (side)
         {
             case RectSide.Top:
                 x1 = rect.Left;
                 x2 = rect.Right;
                 y1 = y2 = rect.Top + pen.Width / 2;
                 break;
             case RectSide.Right:
                 x1 = x2 = rect.Right - pen.Width / 2;
                 y1 = rect.Top;
                 y2 = rect.Bottom;
                 break;
             case RectSide.Bottom:
                 x1 = rect.Left;
                 x2 = rect.Right;
                 y1 = y2 = rect.Bottom - pen.Width / 2;
                 break;
             case RectSide.Left:
                 x1 = x2 = rect.Left + pen.Width / 2;
                 y1 = rect.Top;
                 y2 = rect.Bottom;
                 break;
         }
         g.DrawLine (pen, x1, y1, x2, y2);
     }
 }
 public static RectSide Clear(this RectSide sides, RectSide flags)
 {
     return(sides & (~flags));
 }
		/// <summary>
		/// Checks if the object has just entered the screen.
		/// The object must enter it naturally using it's momentum.
		/// </summary>
		/// <param name="side">Side of the screen.</param>
		/// <returns></returns>
		public bool EntersScreen(RectSide side)
		{
			return EntersRect(side, GameCore.ScreenRect, false);
		}
 public float GetCornerWorldPos(RectSide corner)
 {
     return(cornerWorldPos[(int)corner]);
 }