PageRenderTime 74ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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