Degree.cs 5.2 KB

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