using System.Numerics;
namespace ACE.Server.Entity
{
///
/// Represents a 2D line
///
public class Line2
{
///
/// The start point
///
public Vector2 Start;
///
/// The end point
///
public Vector2 End;
///
/// Default constructor
///
public Line2()
{
}
///
/// Constructs a line from 2 points
///
public Line2(Vector2 start, Vector2 end)
{
Start = start;
End = end;
}
///
/// Constructs a line from 2 points
///
public Line2(float startX, float startY, float endX, float endY)
{
Start = new Vector2(startX, startY);
End = new Vector2(endX, endY);
}
///
/// Constructs a 2D line from 3D points, dropping the Z-component
///
public Line2(Vector3 start, Vector3 end)
{
Start = new Vector2(start.X, start.Y);
End = new Vector2(end.X, end.Y);
}
///
/// Returns the determinant of a line at point
///
public static float Determinant(Line2 line, float x, float y)
{
return (line.End.X - line.Start.X) * (y - line.Start.Y) - (line.End.Y - line.Start.Y) * (x - line.Start.X);
}
public static float Determinant(Line2 line, Vector2 point)
{
return Determinant(line, point.X, point.Y);
}
public float Determinant(Vector2 point)
{
return Determinant(this, point);
}
///
/// Returns TRUE if point resides on a line
///
public static bool Collinear(Line2 line, Vector2 point)
{
return Determinant(line, point) == 0;
}
public static bool Collinear(Line2 line, float x, float y)
{
return Determinant(line, x, y) == 0;
}
public bool Collinear(float x, float y)
{
return Collinear(this, x, y);
}
public bool Collinear(Vector2 point)
{
return Collinear(this, point);
}
///
/// Returns TRUE if point is on left side of line
///
///
/// If we draw a horizontal line from left to right,
/// The left side is considered to be the top.
///
public static bool LeftSide(Line2 line, Vector2 point)
{
return Determinant(line, point) < 0;
}
public static bool LeftSide(Line2 line, float x, float y)
{
return Determinant(line, x, y) < 0;
}
public bool LeftSide(Vector2 point)
{
return LeftSide(this, point);
}
public bool LeftSide(float x, float y)
{
return LeftSide(this, x, y);
}
///
/// Returns TRUE if point is on right side of line
///
///
/// If we draw a horizontal line from left to right,
/// The right side is considered to be the bottom.
///
public static bool RightSide(Line2 line, Vector2 point)
{
return Determinant(line, point) > 0;
}
public static bool RightSide(Line2 line, float x, float y)
{
return Determinant(line, x, y) > 0;
}
public bool RightSide(Vector2 point)
{
return RightSide(this, point);
}
public bool RightSide(float x, float y)
{
return RightSide(this, x, y);
}
}
}