Quadrangle.cs 1.9 KB

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