## Line Segment

/// <summary> /// Represents a line between two points in 3-dimensional space. /// </summary> public struct LineSegment3D : IEquatable<LineSegment3D> { #region Instance Members #region Fields & Properties #region Public public Vector3 Point1; public Vector3 Point2; #endregion #endregion #region Constructors #region Public /// <summary> /// Creates a new line between two points in 3-dimensional space. /// </summary> /// <param name="point1">First point.</param> /// <param name="point2">Second point.</param> public LineSegment3D(Vector3 point1, Vector3 point2) { this.Point1 = point1; this.Point2 = point2; } /// <summary> /// Creates a new line between two points in 3-dimensional space. /// </summary> /// <param name="ray"> /// Ray from which to take point of origin and direction. /// </param> /// <param name="length">Length of the line segment.</param> public LineSegment3D(Ray ray, float length) { this.Point1 = ray.Position; this.Point2 = (Vector3.Normalize(ray.Direction) * length) + ray.Position; } #endregion #endregion #region Methods #region Public #region Compare To /// <summary> /// Compares LineSegments to determine if they are between the same two /// points, and whether those two points are in the same order. /// </summary> /// <param name="other">The other LineSegment to compare.</param> /// <returns> /// ComparisonResults.Equal if the points are identical. /// ComparisonResults.Reverse if the points are equal but reversed. /// ComparisonResults.NotEqual if one or both points are not equal. /// </returns> public ComparisonResults CompareTo(LineSegment3D other) { if (this.Point1 == other.Point1) { if (this.Point2 == other.Point2) return ComparisonResults.Equal; else return ComparisonResults.NotEqual; } else if (this.Point1 == other.Point2) { if (this.Point2 == other.Point1) return ComparisonResults.Reverse; else return ComparisonResults.NotEqual; } return ComparisonResults.NotEqual; } #endregion #endregion #endregion #region IEquatable<LineSegment> Members /// <summary> /// Compares LineSegments to determine if they are between the same two /// points. /// </summary> /// <param name="other">The other LineSegment to compare.</param> /// <returns> /// true if CompareTo returns Equal or Reverse, false otherwise. /// </returns> public bool Equals(LineSegment3D other) { return this.CompareTo(other) != ComparisonResults.NotEqual; } #endregion #endregion #region Static Members #region Methods #region Public /// <summary> /// Swaps points in a LineSegment. /// </summary> /// <param name="segment"> /// Original LineSegment whose points should be swapped. /// </param> /// <returns> /// A new LineSegment with the same points as the original, but swapped. /// </returns> public static LineSegment3D Reverse(LineSegment3D segment) { return new LineSegment3D(segment.Point2, segment.Point1); } #endregion #endregion #endregion #region Types public enum ComparisonResults { /// <summary> /// The points match exactly. /// </summary> Equal, /// <summary> /// The points are a match, but in reverse order. /// </summary> Reverse, /// <summary> /// One or both points does not match. /// </summary> NotEqual, } #endregion }