Quadrangle.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. namespace BBIWARG.Utility
  3. {
  4. /// <summary>
  5. /// Class to represent a quadrangle in 2 dimensions, a quadrangle is a geometric shape composed of 4 arbitrary points.
  6. /// </summary>
  7. public class Quadrangle
  8. {
  9. /// <summary>
  10. /// point in the bottom left corner
  11. /// </summary>
  12. public Vector2D BottomLeft { get; private set; }
  13. /// <summary>
  14. /// point in the bottom right corner
  15. /// </summary>
  16. public Vector2D BottomRight { get; private set; }
  17. /// <summary>
  18. /// all 4 points of the quadrangle in clockwise order, beginning at the top left corner
  19. /// </summary>
  20. public Vector2D[] Corners { get { return new Vector2D[4] { TopLeft, TopRight, BottomRight, BottomLeft }; } }
  21. /// <summary>
  22. /// point in the top left corner
  23. /// </summary>
  24. public Vector2D TopLeft { get; private set; }
  25. /// <summary>
  26. /// point in the top right corner
  27. /// </summary>
  28. public Vector2D TopRight { get; private set; }
  29. /// <summary>
  30. /// Standard constructor of quadrangle, which sets the 4 points.
  31. /// </summary>
  32. /// <param name="topLeft">top left point <see cref="topLeft"/></param>
  33. /// <param name="topRight">top right point <see cref="topRight"/></param>
  34. /// <param name="bottomRight">bottom right point <see cref="bottomRight"/></param>
  35. /// <param name="bottomLeft">bottom left point <see cref="bottomLeft"/></param>
  36. public Quadrangle(Vector2D topLeft, Vector2D topRight, Vector2D bottomRight, Vector2D bottomLeft)
  37. {
  38. TopLeft = topLeft;
  39. TopRight = topRight;
  40. BottomRight = bottomRight;
  41. BottomLeft = bottomLeft;
  42. }
  43. /// <summary>
  44. /// Computes the relative position of a point inside the quadrangle, iff the point is inside the output varies between (0,0) and (1,1), else it is smaller or greater.
  45. /// </summary>
  46. /// <param name="p">the point which relative position should be computed</param>
  47. /// <returns>a point between (0,0) and (1,1), iff the point is inside the quadrangle</returns>
  48. public Vector2D getRelativePosition(Vector2D p)
  49. {
  50. Vector2D a, b, c, d;
  51. a = TopLeft;
  52. b = TopRight;
  53. c = BottomRight;
  54. d = BottomLeft;
  55. float vc = (a.Y - p.Y) * (d.X - p.X) - (a.X - p.X) * (d.Y - p.Y);
  56. float vb = (a.Y - p.Y) * (c.X - d.X) + (b.Y - a.Y) * (d.X - p.X) - (a.X - p.X) * (c.Y - d.Y) - (b.X - a.X) * (d.Y - p.Y);
  57. float va = (b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y);
  58. float vd = vb * vb - 4 * va * vc;
  59. float u = (-vb - (float)Math.Sqrt(vd)) / (2 * va);
  60. float p1x = a.X + (b.X - a.X) * u;
  61. float p2x = d.X + (c.X - d.X) * u;
  62. float px = p.X;
  63. float v = (px - p1x) / (p2x - p1x);
  64. return new Vector2D(u, v);
  65. }
  66. /// <summary>
  67. /// Checks whether a point is inside the quadrangle or not, a tolerance can be set, which allows that the decision is based on a quadrangle which is tolerance times greater/smaller than the original quadrangle.
  68. /// </summary>
  69. /// <param name="point">the point</param>
  70. /// <param name="tolerance">the tolerance value</param>
  71. /// <returns>true iff the point is inside the quadrangle(plus tolerance)</returns>
  72. public bool isInside(Vector2D point, float tolerance = 0.0f)
  73. {
  74. Vector2D relativePos = getRelativePosition(point);
  75. float min = 0 - tolerance;
  76. float max = 1 + tolerance;
  77. return (relativePos.X >= min && relativePos.X <= max) && (relativePos.Y >= min && relativePos.Y <= max);
  78. }
  79. }
  80. }