/Utilities/Math/LinearSegment.cs
C# | 110 lines | 62 code | 10 blank | 38 comment | 5 complexity | a3995b926c117a7ee2042ef287ba6ea1 MD5 | raw file
Possible License(s): Apache-2.0
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
-
- namespace Delta.Utilities.Math
- {
- /// <summary>
- /// The linear segment class that exposes the functionality of getting a
- /// linearly interpolated value from between Point "Start" and "End".
- /// Only used for the Graph class.
- /// </summary>
- public struct LinearSegment
- {
- #region Constants
- /// <summary>
- /// Zero segment with Point.Zero as Start and End points, used to mark
- /// unused segments.
- /// </summary>
- public static readonly LinearSegment Zero =
- new LinearSegment(Point.Zero, Point.Zero);
- #endregion
-
- #region Start (Public)
- /// <summary>
- /// Start
- /// </summary>
- public Point Start;
- #endregion
-
- #region End (Public)
- /// <summary>
- /// End
- /// </summary>
- public Point End;
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create linear segment
- /// </summary>
- public LinearSegment(Point setStart, Point setEnd)
- {
- Start = setStart;
- End = setEnd;
- }
- #endregion
-
- #region op_Equality (Operator)
- /// <summary>
- /// Operator to check for equality
- /// </summary>
- /// <param name="a">First value of the comparison.</param>
- /// <param name="b">Second value of the comparison.</param>
- public static bool operator ==(LinearSegment a, LinearSegment b)
- {
- return b.Start == a.Start &&
- b.End == a.End;
- }
- #endregion
-
- #region op_Inequality (Operator)
- /// <summary>
- /// Operator to check for inequality
- /// </summary>
- /// <param name="a">First value of the comparison.</param>
- /// <param name="b">Second value of the comparison.</param>
- public static bool operator !=(LinearSegment a, LinearSegment b)
- {
- return b.Start != a.Start ||
- b.End != a.End;
- }
- #endregion
-
- #region GetValue (Public)
- /// <summary>
- /// Get value in segment at position "at" on the x-axis.
- /// </summary>
- public float GetValue(float at)
- {
- float remainingPart = at - Start.X;
- float percentage =
- MathHelper.Min(remainingPart / (End.X - Start.X), 1.0f);
-
- return Start.Y + (End.Y - Start.Y) * percentage;
- }
- #endregion
-
- #region GetHashCode (Public)
- /// <summary>
- /// GetHashCode
- /// </summary>
- public override int GetHashCode()
- {
- return Start.GetHashCode() ^ End.GetHashCode();
- }
- #endregion
-
- #region Equals (Public)
- /// <summary>
- /// Equals
- /// </summary>
- /// <param name="obj">Object</param>
- public override bool Equals(object obj)
- {
- return (obj is LinearSegment)
- ? this == (LinearSegment)obj
- : base.Equals(obj);
- }
- #endregion
- }
- }