Quadrangle.cs 1.2 KB

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