using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace bbiwarg.Utility { class Quadrangle { public Quadrangle(Vector2D[] vertices) { Vertices = vertices; } public Vector2D[] Vertices { private set; get; } public Vector2D getRelativePosition(Vector2D p) { Vector2D a = Vertices[0]; Vector2D b = Vertices[1]; Vector2D c = Vertices[2]; Vector2D d = Vertices[3]; float C = (a.Y - p.Y) * (d.X - p.X) - (a.X - p.X) * (d.Y - p.Y); 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); float A = (b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y); float D = B * B - 4 * A * C; float u = (-B - (float) Math.Sqrt(D)) / (2 * A); float p1x = a.X + (b.X - a.X) * u; float p2x = d.X + (c.X - d.X) * u; float px = p.X; float v = (px - p1x) / (p2x - p1x); return new Vector2D(v, u); } } }