using System; using UnityEngine; namespace Framework.Geometry.Lines { /// Line segment that connects two points together public struct Segment2 { /// Point the line segment begins at public Vector2 From; /// Point the line segment ends at public Vector2 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 /// X coordinate the line segment will end at /// Y coordinate the line segment will end at public Segment2(float fromX, float fromY, float toX, float toY) { this.From = new Vector2(fromX, fromY); this.To = new Vector2(toX, toY); } /// Initializes a new line segment between the provided points /// Point the line segment will begin at /// Point the line segment will end at public Segment2(Vector2 from, Vector2 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 Segment2)) { return false; } Segment2 other = (Segment2)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 Vector2 Interpolate(Vector2 from, Vector2 to, float t) { return (to - from) * t + from; } } } // namespace Framework.Geometry.Lines