123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- namespace BBIWARG.Utility
- {
-
-
-
- public class Quadrangle
- {
-
-
-
- public Vector2D BottomLeft { get; private set; }
-
-
-
- public Vector2D BottomRight { get; private set; }
-
-
-
- public Vector2D[] Corners { get { return new Vector2D[4] { TopLeft, TopRight, BottomRight, BottomLeft }; } }
-
-
-
- public Vector2D TopLeft { get; private set; }
-
-
-
- public Vector2D TopRight { get; private set; }
-
-
-
-
-
-
-
- public Quadrangle(Vector2D topLeft, Vector2D topRight, Vector2D bottomRight, Vector2D bottomLeft)
- {
- TopLeft = topLeft;
- TopRight = topRight;
- BottomRight = bottomRight;
- BottomLeft = bottomLeft;
- }
-
-
-
-
-
- public Vector2D getRelativePosition(Vector2D p)
- {
- Vector2D a, b, c, d;
- a = TopLeft;
- b = TopRight;
- c = BottomRight;
- d = BottomLeft;
- float vc = (a.Y - p.Y) * (d.X - p.X) - (a.X - p.X) * (d.Y - p.Y);
- float vb = (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 va = (b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y);
- float vd = vb * vb - 4 * va * vc;
- float u = (-vb - (float)Math.Sqrt(vd)) / (2 * va);
- 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(u, v);
- }
-
-
-
-
-
-
- public bool isInside(Vector2D point, float tolerance = 0.0f)
- {
- Vector2D relativePos = getRelativePosition(point);
- float min = 0 - tolerance;
- float max = 1 + tolerance;
- return (relativePos.X >= min && relativePos.X <= max) && (relativePos.Y >= min && relativePos.Y <= max);
- }
- }
- }
|