Advertisement
smc_gamer

Vector2 RightTriangle.GetIntersectionDepth(Rectangle)

Sep 20th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1.         public Vector2 GetIntersectionDepth(Rectangle _rectB)
  2.         {
  3.             //make sure rects atleast intersect
  4.  
  5.             if (!Bounds.Intersects(_rectB))
  6.             {
  7.                 return Vector2.Zero;
  8.             }
  9.  
  10.             Vector2 topLeft = new Vector2(_rectB.Left, _rectB.Top);
  11.             Vector2 topRight = new Vector2(_rectB.Right, _rectB.Top);
  12.             Vector2 bottomLeft = new Vector2(_rectB.Left, _rectB.Bottom);
  13.             Vector2 bottomRight = new Vector2(_rectB.Right, _rectB.Bottom);
  14.  
  15.             float left = Left;
  16.             float right = Right;
  17.             float top = Top;
  18.             float bottom = Bottom;
  19.             //left
  20.             //upside down
  21.             if (VertDirection == -1)
  22.             {
  23.                 if (topLeft.X >= left &&
  24.                     topLeft.X <= right &&
  25.                     topLeft.Y >= top &&
  26.                     topLeft.Y <= bottom)
  27.                 {
  28.                     float y = topLeft.X * Slope + B;
  29.                     if (topLeft.Y <= y)
  30.                     {
  31.                         return new Vector2(0, y - topLeft.Y);
  32.                     }
  33.                 }
  34.             }
  35.             else
  36.             {
  37.                 if (bottomLeft.X >= left &&
  38.                    bottomLeft.X <= right &&
  39.                    bottomLeft.Y >= top &&
  40.                    bottomLeft.Y <= bottom)
  41.                 {
  42.                     float y = bottomLeft.X * Slope + B;
  43.                     if (bottomLeft.Y >= y)
  44.                     {
  45.                         return new Vector2(0, y - bottomLeft.Y);
  46.                     }
  47.                 }
  48.             }
  49.             //  return 0;
  50.             //test other side
  51.             //right side
  52.             //upside down
  53.             if (VertDirection == -1)
  54.             {
  55.                 //  if (HorzDirection = 1)
  56.                 //   {
  57.                 if (topRight.X >= left &&
  58.                     topRight.X <= right &&
  59.                     topRight.Y >= top &&
  60.                    topRight.Y <= bottom)
  61.                 {
  62.                     float y = topRight.X * Slope + B;
  63.                     if (topRight.Y <= y)
  64.                     {
  65.                         return new Vector2(0, y - topRight.Y);
  66.                     }
  67.                 }
  68.             }
  69.             else
  70.             {
  71.                 if (bottomRight.X >= left &&
  72.                    bottomRight.X <= right &&
  73.                    bottomRight.Y >= top &&
  74.                    bottomRight.Y <= bottom)
  75.                 {
  76.                     float y = bottomRight.X * Slope + B;
  77.                     if (bottomRight.Y >= y)
  78.                     {
  79.                         return new Vector2(0, y - bottomRight.Y);
  80.                     }
  81.                 }
  82.             }
  83.             return Vector2.Zero;
  84.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement