Zdeněk Borovec
a23ea94db4
Decided to revert to Vectors being structs after reconsidering the full implications.
104 lines
3.7 KiB
C#
104 lines
3.7 KiB
C#
namespace GeometriCS
|
|
{
|
|
/// <summary>
|
|
/// A segment of a two-dimensional line with double precision.
|
|
/// </summary>
|
|
public class LineSegment2d : IBounded2d
|
|
{
|
|
/// <summary>
|
|
/// Start point of the line.
|
|
/// </summary>
|
|
public Vector2d StartPoint { get; set; }
|
|
|
|
/// <summary>
|
|
/// End point of the line.
|
|
/// </summary>
|
|
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);
|
|
|
|
/// <summary>
|
|
/// A constructor for a 2d line segment.
|
|
/// </summary>
|
|
/// <param name="startPoint">Point at which the line starts.</param>
|
|
/// <param name="endPoint">Point at which the line ends.</param>
|
|
public LineSegment2d(Vector2d startPoint, Vector2d endPoint)
|
|
{
|
|
StartPoint = startPoint;
|
|
EndPoint = endPoint;
|
|
}
|
|
|
|
/// <summary>
|
|
/// String representation of the line segment.
|
|
/// </summary>
|
|
/// <returns>(stPt) -- (endPt)</returns>
|
|
public override string ToString()
|
|
{
|
|
return $"({StartPoint} -- {EndPoint})";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Do the two objects represent the same line segment?
|
|
/// </summary>
|
|
/// <param name="obj">Object to compare.</param>
|
|
/// <remarks>
|
|
/// 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.
|
|
/// </remarks>
|
|
/// <returns><c>true</c> if the lines have the same start and end points. Otherwise <c>false</c></returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deep copy the line.
|
|
/// </summary>
|
|
/// <returns>Deep copy of the line.</returns>
|
|
public object Clone()
|
|
{
|
|
return new LineSegment2d(StartPoint, EndPoint);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Are the line segments equal?
|
|
/// </summary>
|
|
/// <param name="a">First line segment.</param>
|
|
/// <param name="b">Second line segment.</param>
|
|
/// <remarks>
|
|
/// 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.
|
|
/// </remarks>
|
|
/// <returns><c>true</c> if the lines have the same start and end points. Otherwise <c>false</c></returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Are the line segments different?
|
|
/// </summary>
|
|
/// <param name="a">First line segment.</param>
|
|
/// <param name="b">Second line segment.</param>
|
|
/// <returns><c>true</c> If the line segments start or end points are different from the other ones. <c>false</c> If both are same, refer to <seealso cref="operator =="/> for details.</returns>
|
|
public static bool operator !=(LineSegment2d a, LineSegment2d b)
|
|
{
|
|
return !(a == b);
|
|
}
|
|
}
|
|
}
|