Quadrangle.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 { private set; get; }
  12. public Quadrangle(Vector2D[] vertices)
  13. {
  14. Vertices = vertices;
  15. }
  16. public Vector2D getRelativePosition(Vector2D p)
  17. {
  18. Vector2D a = Vertices[0];
  19. Vector2D b = Vertices[1];
  20. Vector2D c = Vertices[2];
  21. Vector2D d = Vertices[3];
  22. float C = (a.Y - p.Y) * (d.X - p.X) - (a.X - p.X) * (d.Y - p.Y);
  23. 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);
  24. float A = (b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y);
  25. float D = B * B - 4 * A * C;
  26. float u = (-B - (float)Math.Sqrt(D)) / (2 * A);
  27. float p1x = a.X + (b.X - a.X) * u;
  28. float p2x = d.X + (c.X - d.X) * u;
  29. float px = p.X;
  30. float v = (px - p1x) / (p2x - p1x);
  31. return new Vector2D(v, u);
  32. }
  33. }
  34. }