Degree.cs 5.1 KB

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