Quadrangle.cs 1.9 KB

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