PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/Utilities/Math/LinearSegment.cs

#
C# | 110 lines | 62 code | 10 blank | 38 comment | 5 complexity | a3995b926c117a7ee2042ef287ba6ea1 MD5 | raw file
  1using Delta.Utilities.Datatypes;
  2using Delta.Utilities.Helpers;
  3
  4namespace Delta.Utilities.Math
  5{
  6	/// <summary>
  7	/// The linear segment class that exposes the functionality of getting a
  8	/// linearly interpolated value from between Point "Start" and "End".
  9	/// Only used for the Graph class.
 10	/// </summary>
 11	public struct LinearSegment
 12	{
 13		#region Constants
 14		/// <summary>
 15		/// Zero segment with Point.Zero as Start and End points, used to mark
 16		/// unused segments.
 17		/// </summary>
 18		public static readonly LinearSegment Zero =
 19			new LinearSegment(Point.Zero, Point.Zero);
 20		#endregion
 21
 22		#region Start (Public)
 23		/// <summary>
 24		/// Start
 25		/// </summary>
 26		public Point Start;
 27		#endregion
 28
 29		#region End (Public)
 30		/// <summary>
 31		/// End
 32		/// </summary>
 33		public Point End;
 34		#endregion
 35
 36		#region Constructors
 37		/// <summary>
 38		/// Create linear segment
 39		/// </summary>
 40		public LinearSegment(Point setStart, Point setEnd)
 41		{
 42			Start = setStart;
 43			End = setEnd;
 44		}
 45		#endregion
 46
 47		#region op_Equality (Operator)
 48		/// <summary>
 49		/// Operator to check for equality
 50		/// </summary>
 51		/// <param name="a">First value of the comparison.</param>
 52		/// <param name="b">Second value of the comparison.</param>
 53		public static bool operator ==(LinearSegment a, LinearSegment b)
 54		{
 55			return b.Start == a.Start &&
 56			       b.End == a.End;
 57		}
 58		#endregion
 59
 60		#region op_Inequality (Operator)
 61		/// <summary>
 62		/// Operator to check for inequality
 63		/// </summary>
 64		/// <param name="a">First value of the comparison.</param>
 65		/// <param name="b">Second value of the comparison.</param>
 66		public static bool operator !=(LinearSegment a, LinearSegment b)
 67		{
 68			return b.Start != a.Start ||
 69			       b.End != a.End;
 70		}
 71		#endregion
 72
 73		#region GetValue (Public)
 74		/// <summary>
 75		/// Get value in segment at position "at" on the x-axis.
 76		/// </summary>
 77		public float GetValue(float at)
 78		{
 79			float remainingPart = at - Start.X;
 80			float percentage =
 81				MathHelper.Min(remainingPart / (End.X - Start.X), 1.0f);
 82
 83			return Start.Y + (End.Y - Start.Y) * percentage;
 84		}
 85		#endregion
 86
 87		#region GetHashCode (Public)
 88		/// <summary>
 89		/// GetHashCode
 90		/// </summary>
 91		public override int GetHashCode()
 92		{
 93			return Start.GetHashCode() ^ End.GetHashCode();
 94		}
 95		#endregion
 96
 97		#region Equals (Public)
 98		/// <summary>
 99		/// Equals
100		/// </summary>
101		/// <param name="obj">Object</param>
102		public override bool Equals(object obj)
103		{
104			return (obj is LinearSegment)
105			       	? this == (LinearSegment)obj
106			       	: base.Equals(obj);
107		}
108		#endregion
109	}
110}