Quadrangle.cs 1.6 KB

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