Degree.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. [SerializeField]
  16. readonly float value;
  17. /// <summary>
  18. /// Creates a new degree value.
  19. /// </summary>
  20. /// <param name="value">Value in degrees.</param>
  21. public Degree(float value = 0.0f)
  22. {
  23. this.value = value;
  24. }
  25. /// <summary>
  26. /// Creates a new degree value.
  27. /// </summary>
  28. /// <param name="r">Value in radians.</param>
  29. public Degree(Radian r)
  30. {
  31. this.value = r.Degrees;
  32. }
  33. /// <summary>
  34. /// Converts a undefined angle value to a degree value.
  35. /// </summary>
  36. /// <param name="value">Value in degrees.</param>
  37. /// <returns>Degree object wrapping the value.</returns>
  38. public static implicit operator Degree(float value)
  39. {
  40. return new Degree(value);
  41. }
  42. /// <summary>
  43. /// Converts a radian angle value to a degree value.
  44. /// </summary>
  45. /// <param name="r">Value in radians.</param>
  46. /// <returns>Degree object wrapping the value.</returns>
  47. public static implicit operator Degree(Radian r)
  48. {
  49. return new Degree(r.Degrees);
  50. }
  51. /// <summary>
  52. /// Converts a degree value to a regular floating point value.
  53. /// </summary>
  54. /// <param name="d">Degree value to convert.</param>
  55. /// <returns>Value in degrees as floating point type.</returns>
  56. public static explicit operator float(Degree d)
  57. {
  58. return d.value;
  59. }
  60. /// <summary>
  61. /// Returns the value in degrees as a floating point type.
  62. /// </summary>
  63. public float Degrees
  64. {
  65. get { return value; }
  66. }
  67. /// <summary>
  68. /// Returns the value in radians as a floating point type.
  69. /// </summary>
  70. public float Radians
  71. {
  72. get { return value*MathEx.Deg2Rad; }
  73. }
  74. public static Degree operator+(Degree a)
  75. {
  76. return a;
  77. }
  78. public static Degree operator+(Degree a, Degree b)
  79. {
  80. return new Degree(a.value + b.value);
  81. }
  82. public static Degree operator+(Degree a, Radian r)
  83. {
  84. return new Degree (a.value + r.Degrees);
  85. }
  86. public static Degree operator-(Degree a)
  87. {
  88. return new Degree(-a.value);
  89. }
  90. public static Degree operator-(Degree a, Degree b)
  91. {
  92. return new Degree(a.value - b.value);
  93. }
  94. public static Degree operator-(Degree a, Radian r)
  95. {
  96. return new Degree (a.value - r.Degrees);
  97. }
  98. public static Degree operator*(Degree a, float s)
  99. {
  100. return new Degree(a.value * s);
  101. }
  102. public static Degree operator*(Degree a, Degree b)
  103. {
  104. return new Degree(a.value * b.value);
  105. }
  106. public static Degree operator/(Degree a, float s)
  107. {
  108. return new Degree(a.value / s);
  109. }
  110. public static Degree operator /(Degree a, Degree b)
  111. {
  112. return new Degree(a.value / b.value);
  113. }
  114. public static bool operator<(Degree a, Degree b)
  115. {
  116. return a.value < b.value;
  117. }
  118. public static bool operator>(Degree a, Degree b)
  119. {
  120. return a.value > b.value;
  121. }
  122. public static bool operator<=(Degree a, Degree b)
  123. {
  124. return a.value <= b.value;
  125. }
  126. public static bool operator>=(Degree a, Degree b)
  127. {
  128. return a.value >= b.value;
  129. }
  130. public static bool operator==(Degree a, Degree b)
  131. {
  132. return a.value == b.value;
  133. }
  134. public static bool operator!=(Degree a, Degree b)
  135. {
  136. return a.value != b.value;
  137. }
  138. /// <inheritdoc/>
  139. public override bool Equals(object other)
  140. {
  141. if (!(other is Degree))
  142. return false;
  143. Degree degree = (Degree)other;
  144. if (value.Equals(degree.value))
  145. return true;
  146. return false;
  147. }
  148. /// <inheritdoc/>
  149. public override int GetHashCode()
  150. {
  151. return value.GetHashCode();
  152. }
  153. /// <inheritdoc/>
  154. public override string ToString()
  155. {
  156. return value.ToString();
  157. }
  158. };
  159. }