Quadrangle.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. namespace bbiwarg.Utility
  10. {
  11. class Quadrangle
  12. {
  13. public Vector2D[] Vertices { get { return new Vector2D[4] { BottomLeft, TopLeft, TopRight, BottomRight }; } }
  14. public Vector2D BottomLeft { get; private set; }
  15. public Vector2D TopLeft { get; private set; }
  16. public Vector2D TopRight { get; private set; }
  17. public Vector2D BottomRight { get; private set; }
  18. public float Area {get; private set;}
  19. public Quadrangle(Vector2D bottomLeft, Vector2D topLeft, Vector2D topRight, Vector2D bottomRight)
  20. {
  21. BottomLeft = bottomLeft;
  22. TopLeft = topLeft;
  23. TopRight = topRight;
  24. BottomRight = bottomRight;
  25. Contour<PointF> points = new Contour<PointF>(new MemStorage());
  26. points.Push(BottomLeft);
  27. points.Push(TopLeft);
  28. points.Push(TopRight);
  29. points.Push(BottomRight);
  30. Area = (float) points.Area;
  31. }
  32. public Vector2D getRelativePosition(Vector2D p, Hand.HandSide side)
  33. {
  34. Vector2D a, b, c, d;
  35. if (side == Hand.HandSide.Left)
  36. {
  37. a = BottomLeft;
  38. b = TopLeft;
  39. c = TopRight;
  40. d = BottomRight;
  41. }
  42. else
  43. {
  44. a = BottomRight;
  45. b = TopRight;
  46. c = TopLeft;
  47. d = BottomLeft;
  48. }
  49. float C = (a.Y - p.Y) * (d.X - p.X) - (a.X - p.X) * (d.Y - p.Y);
  50. 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);
  51. float A = (b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y);
  52. float D = B * B - 4 * A * C;
  53. float u = (-B - (float)Math.Sqrt(D)) / (2 * A);
  54. float p1x = a.X + (b.X - a.X) * u;
  55. float p2x = d.X + (c.X - d.X) * u;
  56. float px = p.X;
  57. float v = (px - p1x) / (p2x - p1x);
  58. return new Vector2D(v, u);
  59. }
  60. }
  61. }