Quadrangle.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using bbiwarg.Recognition.HandRecognition;
  8. using Emgu.CV;
  9. using Emgu.CV.Structure;
  10. namespace bbiwarg.Utility
  11. {
  12. /// <summary>
  13. /// Class to represent a quadrangle in 2 dimensions, a quagrangle is a geomatric shape composed of 4 arbitrary points.
  14. /// </summary>
  15. public class Quadrangle
  16. {
  17. /// <summary>
  18. /// point in the top left corner
  19. /// </summary>
  20. public Vector2D TopLeft { get; private set; }
  21. /// <summary>
  22. /// point in the top right corner
  23. /// </summary>
  24. public Vector2D TopRight { get; private set; }
  25. /// <summary>
  26. /// point in the bottom right corner
  27. /// </summary>
  28. public Vector2D BottomRight { get; private set; }
  29. /// <summary>
  30. /// point in the bottom left corner
  31. /// </summary>
  32. public Vector2D BottomLeft { get; private set; }
  33. /// <summary>
  34. /// all 4 points of the quadrangle in clockwise order, begining at the top left corner
  35. /// </summary>
  36. public Vector2D[] Corners { get { return new Vector2D[4] { TopLeft, TopRight, BottomRight, BottomLeft }; } }
  37. /// <summary>
  38. /// Standard constructor of quadrangle, which sets the 4 points
  39. /// </summary>
  40. /// <param name="topLeft">top left point <see cref="topLeft"/></param>
  41. /// <param name="topRight">top right point <see cref="topRight"/></param>
  42. /// <param name="bottomRight">bottom right point <see cref="bottomRight"/></param>
  43. /// <param name="bottomLeft">bottom left point <see cref="bottomLeft"/></param>
  44. public Quadrangle(Vector2D topLeft, Vector2D topRight, Vector2D bottomRight, Vector2D bottomLeft)
  45. {
  46. TopLeft = topLeft;
  47. TopRight = topRight;
  48. BottomRight = bottomRight;
  49. BottomLeft = bottomLeft;
  50. }
  51. /// <summary>
  52. /// 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
  53. /// </summary>
  54. /// <param name="p">the point which relative position should be computed</param>
  55. /// <returns>a point between (0,0) and (1,1), iff the point is inside the quadrangle</returns>
  56. public Vector2D getRelativePosition(Vector2D p)
  57. {
  58. Vector2D a, b, c, d;
  59. a = TopLeft;
  60. b = TopRight;
  61. c = BottomRight;
  62. d = BottomLeft;
  63. float C = (a.Y - p.Y) * (d.X - p.X) - (a.X - p.X) * (d.Y - p.Y);
  64. float B = (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);
  65. float A = (b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y);
  66. float D = B * B - 4 * A * C;
  67. float u = (-B - (float)Math.Sqrt(D)) / (2 * A);
  68. float p1x = a.X + (b.X - a.X) * u;
  69. float p2x = d.X + (c.X - d.X) * u;
  70. float px = p.X;
  71. float v = (px - p1x) / (p2x - p1x);
  72. return new Vector2D(u, v);
  73. }
  74. /// <summary>
  75. /// 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
  76. /// </summary>
  77. /// <param name="point">the point</param>
  78. /// <param name="tolerance">the tolerance value</param>
  79. /// <returns>true iff the point is inside the quadrangle(plus tolerance)</returns>
  80. public bool isInside(Vector2D point, float tolerance=0.0f)
  81. {
  82. Vector2D relativePos = getRelativePosition(point);
  83. float min = 0 - tolerance;
  84. float max = 1 + tolerance;
  85. return (relativePos.X >= min && relativePos.X <= max) && (relativePos.Y >= min && relativePos.Y <= max);
  86. }
  87. }
  88. }