Degree.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. /// <summary>
  9. /// Wrapper class which indicates a given angle value is in degrees. Degree values are interchangeable with radian
  10. /// values, and conversions will be done automatically between them.
  11. /// </summary>
  12. [StructLayout(LayoutKind.Sequential), SerializeObject]
  13. public struct Degree // Note: Must match C++ class Degree
  14. {
  15. readonly float value;
  16. /// <summary>
  17. /// Creates a new degree value.
  18. /// </summary>
  19. /// <param name="value">Value in degrees.</param>
  20. public Degree(float value = 0.0f)
  21. {
  22. this.value = value;
  23. }
  24. /// <summary>
  25. /// Creates a new degree value.
  26. /// </summary>
  27. /// <param name="r">Value in radians.</param>
  28. public Degree(Radian r)
  29. {
  30. this.value = r.Degrees;
  31. }
  32. /// <summary>
  33. /// Converts a undefined angle value to a degree value.
  34. /// </summary>
  35. /// <param name="value">Value in degrees.</param>
  36. /// <returns>Degree object wrapping the value.</returns>
  37. public static implicit operator Degree(float value)
  38. {
  39. return new Degree(value);
  40. }
  41. /// <summary>
  42. /// Converts a radian angle value to a degree value.
  43. /// </summary>
  44. /// <param name="r">Value in radians.</param>
  45. /// <returns>Degree object wrapping the value.</returns>
  46. public static implicit operator Degree(Radian r)
  47. {
  48. return new Degree(r.Degrees);
  49. }
  50. /// <summary>
  51. /// Converts a degree value to a regular floating point value.
  52. /// </summary>
  53. /// <param name="d">Degree value to convert.</param>
  54. /// <returns>Value in degrees as floating point type.</returns>
  55. public static explicit operator float(Degree d)
  56. {
  57. return d.value;
  58. }
  59. /// <summary>
  60. /// Returns the value in degrees as a floating point type.
  61. /// </summary>
  62. public float Degrees
  63. {
  64. get { return value; }
  65. }
  66. /// <summary>
  67. /// Returns the value in radians as a floating point type.
  68. /// </summary>
  69. public float Radians
  70. {
  71. get { return value*MathEx.Deg2Rad; }
  72. }
  73. public static Degree operator+(Degree a)
  74. {
  75. return a;
  76. }
  77. public static Degree operator+(Degree a, Degree b)
  78. {
  79. return new Degree(a.value + b.value);
  80. }
  81. public static Degree operator+(Degree a, Radian r)
  82. {
  83. return new Degree (a.value + r.Degrees);
  84. }
  85. public static Degree operator-(Degree a)
  86. {
  87. return new Degree(-a.value);
  88. }
  89. public static Degree operator-(Degree a, Degree b)
  90. {
  91. return new Degree(a.value - b.value);
  92. }
  93. public static Degree operator-(Degree a, Radian r)
  94. {
  95. return new Degree (a.value - r.Degrees);
  96. }
  97. public static Degree operator*(Degree a, float s)
  98. {
  99. return new Degree(a.value * s);
  100. }
  101. public static Degree operator*(Degree a, Degree b)
  102. {
  103. return new Degree(a.value * b.value);
  104. }
  105. public static Degree operator/(Degree a, float s)
  106. {
  107. return new Degree(a.value / s);
  108. }
  109. public static Degree operator /(Degree a, Degree b)
  110. {
  111. return new Degree(a.value / b.value);
  112. }
  113. public static bool operator<(Degree a, Degree b)
  114. {
  115. return a.value < b.value;
  116. }
  117. public static bool operator>(Degree a, Degree b)
  118. {
  119. return a.value > b.value;
  120. }
  121. public static bool operator<=(Degree a, Degree b)
  122. {
  123. return a.value <= b.value;
  124. }
  125. public static bool operator>=(Degree a, Degree b)
  126. {
  127. return a.value >= b.value;
  128. }
  129. public static bool operator==(Degree a, Degree b)
  130. {
  131. return a.value == b.value;
  132. }
  133. public static bool operator!=(Degree a, Degree b)
  134. {
  135. return a.value != b.value;
  136. }
  137. /// <inheritdoc/>
  138. public override bool Equals(object other)
  139. {
  140. if (!(other is Degree))
  141. return false;
  142. Degree degree = (Degree)other;
  143. if (value.Equals(degree.value))
  144. return true;
  145. return false;
  146. }
  147. /// <inheritdoc/>
  148. public override int GetHashCode()
  149. {
  150. return value.GetHashCode();
  151. }
  152. /// <inheritdoc/>
  153. public override string ToString()
  154. {
  155. return value.ToString();
  156. }
  157. };
  158. }