using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using bbiwarg.Recognition.HandRecognition;
using Emgu.CV;
using Emgu.CV.Structure;
namespace bbiwarg.Utility
{
///
/// Class to represent a quadrangle in 2 dimensions, a quagrangle is a geomatric shape composed of 4 arbitrary points.
///
public class Quadrangle
{
///
/// point in the top left corner
///
public Vector2D TopLeft { get; private set; }
///
/// point in the top right corner
///
public Vector2D TopRight { get; private set; }
///
/// point in the bottom right corner
///
public Vector2D BottomRight { get; private set; }
///
/// point in the bottom left corner
///
public Vector2D BottomLeft { get; private set; }
///
/// all 4 points of the quadrangle in clockwise order, begining at the top left corner
///
public Vector2D[] Corners { get { return new Vector2D[4] { TopLeft, TopRight, BottomRight, BottomLeft }; } }
///
/// Standard constructor of quadrangle, which sets the 4 points
///
/// top left point
/// top right point
/// bottom right point
/// bottom left point
public Quadrangle(Vector2D topLeft, Vector2D topRight, Vector2D bottomRight, Vector2D bottomLeft)
{
TopLeft = topLeft;
TopRight = topRight;
BottomRight = bottomRight;
BottomLeft = bottomLeft;
}
///
/// Computes the relative position of a point inside the quadrangle, iff the point is inside the output varies between (0,0) and (1,1), else it is smaller or greater
///
/// the point which relative position should be computed
/// a point between (0,0) and (1,1), iff the point is inside the quadrangle
public Vector2D getRelativePosition(Vector2D p)
{
Vector2D a, b, c, d;
a = TopLeft;
b = TopRight;
c = BottomRight;
d = BottomLeft;
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(u, v);
}
///
/// Checks whether a point is inside the quadrangle or not, a tolerance can be set, which allows that the decision is based on a quadrangle which is tolerance times greater/smaller than the original quadrangle
///
/// the point
/// the tolerance value
/// true iff the point is inside the quadrangle(plus tolerance)
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);
}
}
}