using System; using UnityEngine; namespace Framework.Geometry.Lines { /// Line segment that connects two points together public struct Segment3 { /// Point the line segment begins at public Vector3 From; /// Point the line segment ends at public Vector3 To; /// Initializes a new line segment between the provided points /// X coordinate the line segment will begin at /// Y coordinate the line segment will begin at /// Z coordinate the line segment will begin at /// X coordinate the line segment will end at /// Y coordinate the line segment will end at /// Z coordinate the line segment will end at public Segment3(float fromX, float fromY, float fromZ, float toX, float toY, float toZ) { this.From = new Vector3(fromX, fromY, fromZ); this.To = new Vector3(toX, toY, toZ); } /// Initializes a new line segment between the provided points /// Point the line segment will begin at /// Point the line segment will end at public Segment3(Vector3 from, Vector3 to) { this.From = from; this.To = to; } /// Length of the line segment public float Length { get { return (this.To - this.From).magnitude; } } /// Squared length of the line segment (faster to calculate than Length) public float SquaredLength { get { return (this.To - this.From).sqrMagnitude; } } /// /// Determines whether this instance is identical to another instance /// /// Other instance of compare against /// True if both instances are identical public override bool Equals(object otherObject) { if(!(otherObject is Segment3)) { return false; } Segment3 other = (Segment3)otherObject; return ( (this.From == other.From) && (this.To == other.To) ); } /// Returns a hash code for the instance /// The instance's hash code public override int GetHashCode() { return this.From.GetHashCode() ^ this.To.GetHashCode(); } /// Interpolates along a line segment /// Point at which the line segment begins /// Point at which the line segment ends /// Interpolation time /// The interpolated point public static Vector3 Interpolate(Vector3 from, Vector3 to, float t) { return (to - from) * t + from; } } } // namespace Framework.Geometry.Lines