Advertisement
smc_gamer

SML - Right Triangle Class

Sep 20th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7.  
  8. ///From TheShyGuy
  9. ///--this is my class i used for sloped tiles a while ago and i just put it in here
  10. ///--to use for the intersection depth
  11. namespace SmlEngine
  12. {
  13.     public class RightTriangle
  14.     {
  15.         public Vector2 Origin;
  16.  
  17.         Vector2 p90;
  18.         Vector2 p2;
  19.         Vector2 p3;
  20.         /// <summary>
  21.         /// The point tangent to the 90 degree angle (position of bottom-right corner?)
  22.         /// </summary>
  23.         public Vector2 P1_90
  24.         {
  25.             get { return p90 + Origin; }
  26.             set
  27.             {
  28.                 p90 = value;
  29.             }
  30.         }
  31.         public Vector2 P2
  32.         {
  33.             get { return p2 + Origin; }
  34.             set { p2 = value; }
  35.         }
  36.         public Vector2 P3
  37.         {
  38.             get { return p3 + Origin; }
  39.             set { p3 = value; }
  40.         }
  41.  
  42.         public Rectangle Bounds
  43.         {
  44.             get { return new Rectangle((int)Origin.X, (int)Origin.Y, (int)Width, (int)Height); }
  45.         }
  46.         public float Left { get { return Math.Min(P3.X, Math.Min(P1_90.X, P2.X)); } }
  47.         public float Bottom { get { return Math.Max(P3.Y, Math.Max(P1_90.Y, P2.Y)); } }
  48.         public float Right { get { return Math.Max(P3.X, Math.Max(P1_90.X, P2.X)); } }
  49.         public float Top { get { return Math.Min(P3.Y, Math.Min(P1_90.Y, P2.Y)); } }
  50.  
  51.         public float Width
  52.         {
  53.             get { return Right - Left; }
  54.         }
  55.         public float Height
  56.         {
  57.             get { return Bottom - Top; }
  58.         }
  59.  
  60.         /// <summary>
  61.         /// 1 = right
  62.         /// -1 = left
  63.         /// </summary>
  64.         public int HorzDirection
  65.         {
  66.             get
  67.             {
  68.                 if (P1_90.X < Right)
  69.                     return 1;
  70.                 else//X > Left
  71.                     return -1;
  72.             }
  73.         }
  74.         /// <summary>
  75.         /// 1 = upwards
  76.         /// -1 = upsideDown
  77.         /// </summary>
  78.         public int VertDirection
  79.         {
  80.             get
  81.             {
  82.                 //inverse?
  83.                 if (P1_90.Y >= Top)
  84.                     return 1;//1
  85.                 else//Y > Bottom
  86.                     return -1;//-1
  87.             }
  88.         }
  89.         public float B
  90.         {
  91.             get
  92.             {
  93.                 //Vector2 diffV = p3 - p2;
  94.                 return P3.Y - Slope * P3.X;
  95.  
  96.             }//coulda used p3, as long as its a point on the slope,nvm
  97.         }
  98.  
  99.         public float Slope
  100.         {
  101.             get
  102.             {
  103.                 return (Gradient.Rise / Gradient.Run);
  104.             }
  105.         }
  106.  
  107.         public int Direction
  108.         {
  109.             get { return HorzDirection * VertDirection; }
  110.         }
  111.         public Gradient Gradient
  112.         {
  113.             get
  114.             {
  115.                 if (Direction == 1)
  116.                 {
  117.                     return new Gradient(Height, Width, false);
  118.                 }
  119.                 else if (Direction == -1)
  120.                 {
  121.                     return new Gradient(Height, Width, true);
  122.                 }
  123.                 else
  124.                 {
  125.                     return new Gradient(Height, Width);
  126.                 }
  127.             }
  128.         }
  129.  
  130.         public bool Inverted
  131.         {
  132.             get
  133.             {
  134.                 return Direction == -1;
  135.             }
  136.         }
  137.  
  138.  
  139.         public RightTriangle(Vector2 p_90, Vector2 p2, Vector2 p3)
  140.         {
  141.             SetPoints(p_90, p2, p3);
  142.         }
  143.  
  144.         public RightTriangle(float originX, float originY, float width, float height)
  145.         {
  146.             SetBounds(new Rectangle((int)originX, (int)originY, (int)width, (int)height));
  147.         }
  148.  
  149.         public RightTriangle(int originX, int originY, int width, int height, RTDirection direction)
  150.         {
  151.             SetBounds(new Rectangle(originX, originY, width, height), direction);
  152.         }
  153.  
  154.         /// <summary>
  155.         /// Sets the triangles points so that p1 == the point of the 90 degree angle
  156.         /// p2 and p3 are the points adj to the hyp
  157.         /// </summary>
  158.         /// <param name="p_90">The 90 degree corner of the triangle.</param>
  159.         /// <param name="p2">The point on the same column as the 90 degree angle.</param>
  160.         /// <param name="p3">The third point, across from point 2.</param>
  161.         public void SetPoints(Vector2 p_90, Vector2 p2, Vector2 p3)
  162.         {
  163.             P1_90 = p_90;
  164.             P2 = p2;
  165.             P3 = p3;
  166.         }
  167.         public void FlipDirection(Flip side)
  168.         {
  169.             List<Vector2> points = new List<Vector2>() { p90, p2, p3 };
  170.  
  171.             float top = Top - Origin.Y;
  172.             float left = Left - Origin.X;
  173.             float bottom = Bottom - Origin.Y;
  174.             float right = Right - Origin.X;
  175.  
  176.             Vector2 topLeft = new Vector2(left, top);
  177.             Vector2 topRight = new Vector2(right, top);
  178.             Vector2 bottomLeft = new Vector2(left, bottom);
  179.             Vector2 bottomRight = new Vector2(right, bottom);
  180.  
  181.             if (side == Flip.Horizontally)
  182.             {
  183.                 if (HorzDirection == 1 && VertDirection == -1)
  184.                 {
  185.                     p90 = bottomRight;
  186.                     p2 = bottomLeft;
  187.                     p3 = topRight;
  188.                 }
  189.                 else if (HorzDirection == 1 && VertDirection == 1)
  190.                 {
  191.                     p90 = topRight;
  192.                     p2 = topLeft;
  193.                     p3 = bottomRight;
  194.                 }
  195.                 else if (HorzDirection == -1 && VertDirection == -1)
  196.                 {
  197.                     p90 = bottomLeft;
  198.                     p2 = bottomRight;
  199.                     p3 = topLeft;
  200.                 }
  201.                 else if (HorzDirection == -1 && VertDirection == 1)
  202.                 {
  203.                     p90 = topLeft;
  204.                     p2 = topRight;
  205.                     p3 = bottomLeft;
  206.                 }
  207.             }
  208.             else if (side == Flip.Vertically)
  209.             {
  210.                 if (HorzDirection == 1 && VertDirection == 1)//-1
  211.                 {
  212.                     p90 = bottomLeft;
  213.                     p2 = bottomRight;
  214.                     p3 = topLeft;
  215.                 }
  216.                 else if (HorzDirection == 1 && VertDirection == -1)//1
  217.                 {
  218.                     p90 = topLeft;
  219.                     p2 = topRight;
  220.                     p3 = bottomLeft;
  221.                 }
  222.                 else if (HorzDirection == -1 && VertDirection == 1)//-1
  223.                 {
  224.                     //since vertDirection has y flipped taken into account...
  225.                     //p90 = topRight;vertDir is actually -1
  226.                     //p2 = topLeft;
  227.                     //p3 = bottomRight;
  228.                     p90 = bottomRight;
  229.                     p2 = bottomLeft;
  230.                     p3 = topRight;
  231.                 }
  232.                 else if (HorzDirection == -1 && VertDirection == -1)//actually 1
  233.                 {
  234.                     p90 = topRight;
  235.                     p2 = topLeft;
  236.                     p3 = bottomRight;
  237.                 }
  238.             }
  239.         }
  240.  
  241.         public void SetBounds(Rectangle bounds)
  242.         {
  243.             Origin = new Vector2(bounds.X, bounds.Y);
  244.  
  245.             // p90 and p2 are in the same column
  246.             //p90 are tangent to p2 and p3
  247.             //p2 and p3 connect thru hypotenuse
  248.             List<Vector2> points = new List<Vector2>() { P1_90, P2, P3 };
  249.             float bottom = Bottom;
  250.             float right = Right;
  251.             List<Vector2> bottomPoints = points.FindAll(p => p.Y == bottom);
  252.             List<Vector2> rightPoints = points.FindAll(p => p.X == right);
  253.  
  254.             float deltaX = (float)bounds.Width - Width;
  255.             float deltaY = (float)bounds.Height - Height;
  256.  
  257.             //move points
  258.  
  259.             foreach (Vector2 point in bottomPoints)
  260.             {
  261.                 if (P1_90 == point)
  262.                 {
  263.                     p90 = new Vector2(point.X, point.Y + deltaY) - Origin;
  264.                 }
  265.                 if (P2 == point)
  266.                 {
  267.                     p2 = new Vector2(point.X, point.Y + deltaY) - Origin;
  268.                 }
  269.                 if (P3 == point)
  270.                 {
  271.                     p3 = new Vector2(point.X, point.Y + deltaY) - Origin;
  272.                 }
  273.             }
  274.             foreach (Vector2 point in rightPoints)
  275.             {
  276.                 if (P1_90 == point)
  277.                 {
  278.                     p90 = new Vector2(point.X + deltaX, point.Y) - Origin;
  279.                 }
  280.                 if (P2 == point)
  281.                 {
  282.                     p2 = new Vector2(point.X + deltaX, point.Y) - Origin;
  283.                 }
  284.                 if (P3 == point)
  285.                 {
  286.                     p3 = new Vector2(point.X + deltaX, point.Y) - Origin;
  287.                 }
  288.             }
  289.         }
  290.  
  291.         public void SetBounds(Rectangle bounds, RTDirection direction)
  292.         {
  293.             Origin = new Vector2(bounds.X, bounds.Y);
  294.  
  295.             switch (direction)
  296.             {
  297.                 case RTDirection.TopLeft:
  298.                     p90 = Vector2.Zero;
  299.                     p2 = new Vector2(0, bounds.Height);
  300.                     p3 = new Vector2(bounds.Width, 0);
  301.                     break;
  302.                 case RTDirection.TopRight:
  303.                     p90 = new Vector2(bounds.Width, 0);
  304.                     p2 = Vector2.Zero;
  305.                     p3 = new Vector2(bounds.Width, bounds.Height);
  306.                     break;
  307.                 case RTDirection.BottomLeft:
  308.                     p90 = new Vector2(0, bounds.Height);
  309.                     p2 = new Vector2(bounds.Width, bounds.Height);
  310.                     p3 = Vector2.Zero;
  311.                     break;
  312.                 case RTDirection.BottomRight:
  313.                     p90 = new Vector2(bounds.Width, bounds.Height);
  314.                     p3 = new Vector2(bounds.Width, 0);
  315.                     p2 = new Vector2(0, bounds.Height);
  316.                     break;
  317.                 default:
  318.                     break;
  319.             }
  320.         }
  321.  
  322.         public bool PointWithinTriangle(Vector2 p, bool notin)
  323.         {
  324.             if (p.X < Left ||
  325.                 p.X > Right ||
  326.                 p.Y < Bottom ||
  327.                 p.Y > Top)
  328.                 return false;
  329.  
  330.             //Origin = Origin;
  331.             //P1_90 = P1_90;
  332.             //P2 = P2;
  333.             //P3 = P3;
  334.  
  335.             if (VertDirection == -1)
  336.             {
  337.                 if (HorzDirection == 1)
  338.                 {
  339.                     if (p.Y <= p.X * Slope + B)
  340.                     {
  341.                         return true;
  342.                     }
  343.                 }
  344.                 else
  345.                 {
  346.                     if (p.Y >= p.X * Slope + B)
  347.                     {
  348.                         return true;
  349.                     }
  350.                 }
  351.             }
  352.             else
  353.             {
  354.                 if (HorzDirection == 1)
  355.                 {
  356.                     if (p.Y >= p.X * Slope + B)
  357.                     {
  358.                         return true;
  359.                     }
  360.                 }
  361.                 else
  362.                 {
  363.                     if (p.Y <= p.X * Slope + B)
  364.                     {
  365.                         return true;
  366.                     }
  367.                 }
  368.             }
  369.             return false;
  370.         }
  371.  
  372.         public Vector2 GetIntersectionDepth(Rectangle _rectB)
  373.         {
  374.             //make sure rects atleast intersect
  375.  
  376.             if (!Bounds.Intersects(_rectB))
  377.             {
  378.                 return Vector2.Zero;
  379.             }
  380.  
  381.             Vector2 topLeft = new Vector2(_rectB.Left, _rectB.Top);
  382.             Vector2 topRight = new Vector2(_rectB.Right, _rectB.Top);
  383.             Vector2 bottomLeft = new Vector2(_rectB.Left, _rectB.Bottom);
  384.             Vector2 bottomRight = new Vector2(_rectB.Right, _rectB.Bottom);
  385.  
  386.             float left = Left;
  387.             float right = Right;
  388.             float top = Top;
  389.             float bottom = Bottom;
  390.             //left
  391.             //upside down
  392.             if (VertDirection == -1)
  393.             {
  394.                 if (topLeft.X >= left &&
  395.                     topLeft.X <= right &&
  396.                     topLeft.Y >= top &&
  397.                     topLeft.Y <= bottom)
  398.                 {
  399.                     float y = topLeft.X * Slope + B;
  400.                     if (topLeft.Y <= y)
  401.                     {
  402.                         return new Vector2(0, y - topLeft.Y);
  403.                     }
  404.                 }
  405.             }
  406.             else
  407.             {
  408.                 if (bottomLeft.X >= left &&
  409.                    bottomLeft.X <= right &&
  410.                    bottomLeft.Y >= top &&
  411.                    bottomLeft.Y <= bottom)
  412.                 {
  413.                     float y = bottomLeft.X * Slope + B;
  414.                     if (bottomLeft.Y >= y)
  415.                     {
  416.                         return new Vector2(0, y - bottomLeft.Y);
  417.                     }
  418.                 }
  419.             }
  420.             //  return 0;
  421.             //test other side
  422.             //right side
  423.             //upside down
  424.             if (VertDirection == -1)
  425.             {
  426.                 //  if (HorzDirection = 1)
  427.                 //   {
  428.                 if (topRight.X >= left &&
  429.                     topRight.X <= right &&
  430.                     topRight.Y >= top &&
  431.                    topRight.Y <= bottom)
  432.                 {
  433.                     float y = topRight.X * Slope + B;
  434.                     if (topRight.Y <= y)
  435.                     {
  436.                         return new Vector2(0, y - topRight.Y);
  437.                     }
  438.                 }
  439.             }
  440.             else
  441.             {
  442.                 if (bottomRight.X >= left &&
  443.                    bottomRight.X <= right &&
  444.                    bottomRight.Y >= top &&
  445.                    bottomRight.Y <= bottom)
  446.                 {
  447.                     float y = bottomRight.X * Slope + B;
  448.                     if (bottomRight.Y >= y)
  449.                     {
  450.                         return new Vector2(0, y - bottomRight.Y);
  451.                     }
  452.                 }
  453.             }
  454.             return Vector2.Zero;
  455.  
  456.         }
  457.  
  458.         public Vector2 GetPoint(float x)
  459.         {
  460.             //TODO: fix points when loaded, points should be the same as in the data
  461.             //but when checking for slopes and stuff, use origin + the point
  462.             x = MathHelper.Clamp(x, Left, Right);
  463.  
  464.             Vector2 point = new Vector2(x, x * Slope + B);
  465.  
  466.             return point;
  467.         }
  468.  
  469.         public override string ToString()
  470.         {
  471.             return string.Format(
  472.                 "[Origin: {10}; Point_90: {0}; P2: {1}; P3: {2}; " +
  473.                 "Width: {3}; Height: {4}; HorizontalDirection: {5}; " +
  474.                 "VerticalDirection: {6}; Direction: {7}; B: {8}; Slope: {9}]",
  475.                 p90, p2, p3, Width, Height, HorzDirection, VertDirection, Direction, B, Slope, Origin);
  476.         }
  477.     }
  478.  
  479.     public enum RTDirection
  480.     {
  481.         TopLeft,
  482.         TopRight,
  483.         BottomLeft,
  484.         BottomRight
  485.     }
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement