Quadrangle.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. class Quadrangle
  13. {
  14. public Vector2D TopLeft { get; private set; }
  15. public Vector2D TopRight { get; private set; }
  16. public Vector2D BottomRight { get; private set; }
  17. public Vector2D BottomLeft { get; private set; }
  18. public Quadrangle(Vector2D topLeft, Vector2D topRight, Vector2D bottomRight, Vector2D bottomLeft)
  19. {
  20. TopLeft = topLeft;
  21. TopRight = topRight;
  22. BottomRight = bottomRight;
  23. BottomLeft = bottomLeft;
  24. }
  25. public Vector2D getRelativePosition(Vector2D p)
  26. {
  27. Vector2D a, b, c, d;
  28. a = TopLeft;
  29. b = TopRight;
  30. c = BottomRight;
  31. d = BottomLeft;
  32. float C = (a.Y - p.Y) * (d.X - p.X) - (a.X - p.X) * (d.Y - p.Y);
  33. 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);
  34. float A = (b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y);
  35. float D = B * B - 4 * A * C;
  36. float u = (-B - (float)Math.Sqrt(D)) / (2 * A);
  37. float p1x = a.X + (b.X - a.X) * u;
  38. float p2x = d.X + (c.X - d.X) * u;
  39. float px = p.X;
  40. float v = (px - p1x) / (p2x - p1x);
  41. return new Vector2D(u, v);
  42. }
  43. public bool isInside(Vector2D point)
  44. {
  45. Vector2D relativePos = getRelativePosition(point);
  46. return (relativePos.X >= 0 && relativePos.X <= 1.0 && relativePos.Y >= 0 && relativePos.Y <= 1.0);
  47. }
  48. }
  49. }