91 lines
3.9 KiB
C#
91 lines
3.9 KiB
C#
|
namespace GeometriCS
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Geometric extents of a finitely large entity. Double precision.
|
|||
|
/// </summary>
|
|||
|
public struct Extents2d
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// The point with the lowest Y and X.
|
|||
|
/// </summary>
|
|||
|
public Vector2d MinPoint { get; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// The point with the highest Y and X.
|
|||
|
/// </summary>
|
|||
|
public Vector2d MaxPoint { get; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Take two points and create Extents from their bounding box.
|
|||
|
/// </summary>
|
|||
|
/// <param name="pt1">First point.</param>
|
|||
|
/// <param name="pt2">Second point.</param>
|
|||
|
/// <returns>Extents of the bounding box of the two points.</returns>
|
|||
|
public static Extents2d FromTwoPoints(Vector2d pt1, Vector2d pt2)
|
|||
|
{
|
|||
|
// Find the smallest and largest x and y values.
|
|||
|
double minx = pt1.X < pt2.X ? pt1.X : pt2.X;
|
|||
|
double miny = pt1.Y < pt2.Y ? pt1.Y : pt2.Y;
|
|||
|
double maxx = pt1.X > pt2.X ? pt1.X : pt2.X;
|
|||
|
double maxy = pt1.Y > pt2.Y ? pt1.Y : pt2.Y;
|
|||
|
|
|||
|
// Create new points from them and create the extents.
|
|||
|
return new Extents2d(new Vector2d(minx, miny),
|
|||
|
new Vector2d(maxx, maxy));
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Is there an overlap between the bounding boxes of these two extents?
|
|||
|
/// </summary>
|
|||
|
/// <param name="other"></param>
|
|||
|
/// <returns><c>true</c> if there is an overlap, otherwise <c>false</c>.</returns>
|
|||
|
public bool Overlaps(Extents2d other)
|
|||
|
{
|
|||
|
// If at least one of my points are in others X band, or at least one of others points are in my X band
|
|||
|
// check whether at least one of my points are in others Y band, or at least one of the others points
|
|||
|
// are in my Y band. If both conditions are met, there is an overlap.
|
|||
|
return (MinPoint.X.IsInRange(other.MinPoint.X, other.MaxPoint.X) ||
|
|||
|
MaxPoint.X.IsInRange(other.MinPoint.X, other.MaxPoint.X) ||
|
|||
|
other.MinPoint.X.IsInRange(MinPoint.X, MaxPoint.X) ||
|
|||
|
other.MaxPoint.X.IsInRange(MinPoint.X, MaxPoint.X) ) && (
|
|||
|
MinPoint.Y.IsInRange(other.MinPoint.Y, other.MaxPoint.Y) ||
|
|||
|
MaxPoint.Y.IsInRange(other.MinPoint.Y, other.MaxPoint.Y) ||
|
|||
|
other.MinPoint.Y.IsInRange(MinPoint.Y, MaxPoint.Y) ||
|
|||
|
other.MaxPoint.Y.IsInRange(MinPoint.Y, MaxPoint.Y));
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Does the bounding box contain the point.
|
|||
|
/// </summary>
|
|||
|
/// <param name="point">Point to test.</param>
|
|||
|
/// <returns><c>true</c> if the point is inside of the bounding box, otherwise <c>false</c>.</returns>
|
|||
|
public bool Contains(Vector2d point)
|
|||
|
{
|
|||
|
return point.X.IsInRange(MinPoint.X, MaxPoint.X) && point.Y.IsInRange(MinPoint.Y, MaxPoint.Y);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Constructor for the Extents2d.
|
|||
|
/// </summary>
|
|||
|
/// <param name="minPoint">The point with the lowest X and Y.</param>
|
|||
|
/// <param name="maxPoint">The point with the greatest X and Y.</param>
|
|||
|
/// <exception cref="ArgumentException">
|
|||
|
/// The maxpoint is not greater in both dimensions than the minpoint.
|
|||
|
/// </exception>
|
|||
|
public Extents2d(Vector2d minPoint, Vector2d maxPoint)
|
|||
|
{
|
|||
|
/// Check validity
|
|||
|
if((minPoint.X > maxPoint.X && !Utils.DoubleEquals(minPoint.X, maxPoint.X)) ||
|
|||
|
(minPoint.Y > maxPoint.Y && !Utils.DoubleEquals(minPoint.Y, maxPoint.Y)))
|
|||
|
{
|
|||
|
throw new ArgumentException($"The minpoint {minPoint} has to " +
|
|||
|
$"have lesser both X and Y than the maxpoint {maxPoint}.");
|
|||
|
}
|
|||
|
|
|||
|
// Assign the properties.
|
|||
|
MinPoint = minPoint;
|
|||
|
MaxPoint = maxPoint;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|