GeometriCS/structs/LineSegment3.cs
Zdeněk Borovec a23ea94db4 Line intersections
Decided to revert to Vectors being structs after reconsidering the full implications.
2024-04-19 00:42:56 +02:00

100 lines
3.5 KiB
C#

namespace GeometriCS
{
/// <summary>
/// A segment of a line going through the R cubed space.
/// </summary>
public class LineSegment3d
{
/// <summary>
/// Point at which the line segment starts.
/// </summary>
public Vector3d StartPoint { get; set; }
/// <summary>
/// Point at which the line segment ends.
/// </summary>
public Vector3d EndPoint { get; set; }
/// <summary>
/// A constructor for a segment of a line going throught the R cubed space.
/// </summary>
/// <param name="startPoint">Start point of the line.</param>
/// <param name="endPoint">End point of the line.</param>
public LineSegment3d(Vector3d startPoint, Vector3d 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 LineSegment3d 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 LineSegment3d(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 ==(LineSegment3d a, LineSegment3d 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 !=(LineSegment3d a, LineSegment3d b)
{
return !(a == b);
}
}
}