PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/NerdSharp.Net_Studio/NerdSharp_UberNet/Science/Math/Utils/Trigonometry.cs

https://bitbucket.org/pastageek/scide-cad-scilife
C# | 269 lines | 235 code | 33 blank | 1 comment | 13 complexity | 1416e509f2dacb9199e7fc4d1ea01317 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Numerics;
  6. using System.Runtime.Serialization;
  7. using System.ServiceModel;
  8. using M = System.Math;
  9. using System.Windows;
  10. using System.Windows.Shapes;
  11. using OthM = NerdSharp.Net_Studio.NerdSharp_UberNet.Science.Math.Utils.Math;
  12. namespace NerdSharp.Net_Studio.NerdSharp_UberNet.Science.Math.Utils
  13. {
  14. public class Trigonometry
  15. {
  16. [OperationContract]
  17. public static double degreesToRadians(double degrees)
  18. {
  19. return degrees * (M.PI / 180d);
  20. }
  21. [OperationContract]
  22. public static double Hypotenous(double a, double b)
  23. {
  24. return M.Sqrt(M.Pow(a, 2) + M.Pow(b, 2));
  25. }
  26. [OperationContract]
  27. public static double radToDeg(double rad)
  28. {
  29. return rad * (180d / M.PI);
  30. }
  31. [OperationContract]
  32. public static Point ConvertToScreenPoint(Point cartesian, Point screenOrigin)
  33. {
  34. Point answer = new Point();
  35. if (cartesian.Y < 0)
  36. {
  37. answer.Y = M.Abs(cartesian.Y) + screenOrigin.Y;
  38. }
  39. else if (cartesian.Y >= 0)
  40. {
  41. answer.Y = screenOrigin.Y - cartesian.Y;
  42. }
  43. answer.X = screenOrigin.X + cartesian.X;
  44. return answer;
  45. }
  46. [OperationContract]
  47. public static double VerSine(double theta)
  48. {
  49. return 0;
  50. }
  51. [OperationContract]
  52. public static double csc(double theta)
  53. {
  54. return 1 / System.Math.Sin(theta);
  55. }
  56. [OperationContract]
  57. public static double sec(double theta)
  58. {
  59. return 1 / System.Math.Cos(theta);
  60. }
  61. [OperationContract]
  62. public static double cot(double theta)
  63. {
  64. return 1 / System.Math.Tan(theta);
  65. }
  66. [OperationContract]
  67. public static double csch(double theta)
  68. {
  69. return 1 / System.Math.Sinh(theta);
  70. }
  71. [OperationContract]
  72. public static double sech(double theta)
  73. {
  74. return 1 / System.Math.Cosh(theta);
  75. }
  76. [OperationContract]
  77. public static double coth(double theta)
  78. {
  79. return 1 / System.Math.Tanh(theta);
  80. }
  81. [OperationContract]
  82. public static double acsc(double theta)
  83. {
  84. return 1 / System.Math.Asin(theta);
  85. }
  86. [OperationContract]
  87. public static double asec(double theta)
  88. {
  89. return 1 / System.Math.Acos(theta);
  90. }
  91. [OperationContract]
  92. public static double acot(double theta)
  93. {
  94. return 1 / System.Math.Atan(theta);
  95. }
  96. //#region inverse hyperbolic trig functions
  97. [OperationContract]
  98. public static double asinh(double theta)
  99. {
  100. if (!(System.Math.Abs(theta) < 1))
  101. {
  102. throw new Exception("Sorry, but inverse hyperbolic sine nees a value between -1 and 1 exclusive.");
  103. }
  104. double answer = 0;
  105. for (int i = 0; i < 1000; i++)
  106. {
  107. double paren1 = (System.Math.Pow(-1, i) * (double)OthM.Factorial(2 * i)) /
  108. ((System.Math.Pow(2, 2 * i) * System.Math.Pow((double)OthM.Factorial(i), 2)));
  109. double paren2 = System.Math.Pow(theta, (2 * i + 1)) / ((2 * i) + 1);
  110. answer += (paren1 * paren2);
  111. }
  112. return answer;
  113. }
  114. [OperationContract]
  115. public static double acosh(double theta)
  116. {
  117. if (!(theta > 1))
  118. {
  119. throw new Exception("Sorry but inverse hyperbolic cosine needs to have an input of greater than one");
  120. }
  121. double buffer = 0;
  122. for (int i = 0; i < 1000; i++)
  123. {
  124. double paren1 = (double)OthM.Factorial(2 * i) /
  125. ((System.Math.Pow(2, 2 * i) * System.Math.Pow((double)OthM.Factorial(i), 2)));
  126. double paren2 = System.Math.Pow(theta, (-2 * i)) / (2 * i);
  127. buffer = paren1 + paren2;
  128. }
  129. return System.Math.Log(2 * theta, System.Math.E) - buffer;
  130. }
  131. [OperationContract]
  132. public static double atanh(double theta)
  133. {
  134. if (!(System.Math.Abs(theta) < 1))
  135. {
  136. throw new Exception("");
  137. }
  138. double answer = 0;
  139. for (int i = 0; i < 1000; i++)
  140. {
  141. answer += System.Math.Pow(theta, (2 * i + 1)) / ((2 * i) + 1);
  142. }
  143. return answer;
  144. }
  145. [OperationContract]
  146. public static double acsch(double theta)
  147. {
  148. if (!(System.Math.Abs(theta) > 1))
  149. {
  150. throw new Exception("Sorry, but inverse hyperbolic cotangent needs to be greater than 1 or less than -1 exclusive.");
  151. }
  152. return 1 / asinh(theta);
  153. }
  154. [OperationContract]
  155. public static double asech(double theta)
  156. {
  157. if (!((theta > 0) || (theta < 1)))
  158. {
  159. throw new Exception("Sorry, but value for inverse hyberbolic secant needs to be between 0 and 1 exclusive.");
  160. }
  161. return 1 / acosh(theta);
  162. }
  163. [OperationContract]
  164. public static double acoth(double theta)
  165. {
  166. if (!(System.Math.Abs(theta) > 1))
  167. {
  168. throw new Exception("Sorry, but inverse hyperbolic cotangent needs to be greater than 1 or less than -1 exclusive.");
  169. }
  170. return 1 / atanh(theta);
  171. }
  172. #region other trigFunctions
  173. [OperationContract]
  174. public static double exsec(double theta)
  175. {
  176. return sec(theta) - 1;
  177. }
  178. #endregion
  179. #region complex trig functions
  180. [OperationContract]
  181. public static Complex csc(Complex theta)
  182. {
  183. return 1 / Complex.Sin(theta);
  184. }
  185. [OperationContract]
  186. public static Complex sec(Complex theta)
  187. {
  188. return 1 / Complex.Cos(theta);
  189. }
  190. [OperationContract]
  191. public static Complex cot(Complex theta)
  192. {
  193. return 1 / Complex.Tan(theta);
  194. }
  195. [OperationContract]
  196. public static Complex csch(Complex theta)
  197. {
  198. return 1 / Complex.Sinh(theta);
  199. }
  200. [OperationContract]
  201. public static Complex sech(Complex theta)
  202. {
  203. return 1 / Complex.Cosh(theta);
  204. }
  205. [OperationContract]
  206. public static Complex coth(Complex theta)
  207. {
  208. return 1 / Complex.Tanh(theta);
  209. }
  210. [OperationContract]
  211. public static Complex asinh(Complex theta, long places)
  212. {
  213. Complex answer = new Complex();
  214. answer = Complex.Log(theta + (Complex.Sqrt(Complex.Pow(theta, 2) + 1)), System.Math.E);
  215. return answer;
  216. }
  217. [OperationContract]
  218. public static Complex acosh(Complex theta, long places)
  219. {
  220. Complex answer = new Complex();
  221. answer = Complex.Log((theta + (Complex.Sqrt(theta + 1) * Complex.Sqrt(theta - 1))), System.Math.E);
  222. return answer;
  223. }
  224. #endregion
  225. #region CircleUtils
  226. public static double X(double thetaDegrees, double radius)
  227. {
  228. return M.Cos(degreesToRadians(thetaDegrees)) * radius;
  229. }
  230. public static double Y(double thetaDegrees, double radius)
  231. {
  232. return M.Sin(degreesToRadians(thetaDegrees)) * radius;
  233. }
  234. #endregion
  235. }
  236. }