namespace GeometriCS
{
///
/// A segment of a two-dimensional line with double precision.
///
public class LineSegment2d : IBounded2d
{
///
/// Start point of the line.
///
public Vector2d StartPoint { get; set; }
///
/// End point of the line.
///
public Vector2d EndPoint { get; set;}
// Consider pre-calculating this in point setters later on,
// once we have group intersection support and can gauge performance effects.
public Extents2d Extents => Extents2d.FromTwoPoints(StartPoint, EndPoint);
///
/// A constructor for a 2d line segment.
///
/// Point at which the line starts.
/// Point at which the line ends.
public LineSegment2d(Vector2d startPoint, Vector2d endPoint)
{
StartPoint = startPoint;
EndPoint = endPoint;
}
///
/// String representation of the line segment.
///
/// (stPt) -- (endPt)
public override string ToString()
{
return $"({StartPoint} -- {EndPoint})";
}
///
/// Do the two objects represent the same line segment?
///
/// Object to compare.
///
/// If this lines start point is equal to the other lines end point,
/// and this lines end point is equal to the other lines start point, the lines will be considered equal.
///
/// true if the lines have the same start and end points. Otherwise false
public override bool Equals(object? obj)
{
if (obj is LineSegment2d asLnSgmnt)
{
return this == asLnSgmnt;
}
else
{
return false;
}
}
public override int GetHashCode()
{
return HashCode.Combine(StartPoint, EndPoint);
}
///
/// Deep copy the line.
///
/// Deep copy of the line.
public object Clone()
{
return new LineSegment2d(StartPoint, EndPoint);
}
///
/// Are the line segments equal?
///
/// First line segment.
/// Second line segment.
///
/// If this lines start point is equal to the other lines end point,
/// and this lines end point is equal to the other lines start point, the lines will be considered equal.
///
/// true if the lines have the same start and end points. Otherwise false
public static bool operator ==(LineSegment2d a, LineSegment2d b)
{
return (a.StartPoint == b.StartPoint && a.EndPoint == b.EndPoint) ||
(a.StartPoint == b.EndPoint && a.EndPoint == b.StartPoint);
}
///
/// Are the line segments different?
///
/// First line segment.
/// Second line segment.
/// true If the line segments start or end points are different from the other ones. false If both are same, refer to for details.
public static bool operator !=(LineSegment2d a, LineSegment2d b)
{
return !(a == b);
}
}
}