SHARE
TWEET

Deg_Trig_Sample.h

KeithS Jun 21st, 2012 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef DEG_TRIG_H
  2. #define DEG_TRIG_H
  3. #include <math.h>
  4.  
  5. #define _USE_MATH_DEFINES
  6. #define RAD_TO_DEG(x) (x)*(180.0/M_PI)
  7. #define DEG_TO_RAD(x) (x)*(M_PI/180.0)
  8.  
  9. // FLOAT FUNCTIONS
  10.  
  11. float my_atan2f_PIx2_rads(float y, float x)
  12. {
  13.     float rads;
  14.     rads = atan2f(y, x);
  15.     if(rads < 0.0f)
  16.         rads += (M_PI * 2);
  17.     return rads;
  18. }
  19.  
  20. float my_atan2f_deg(float y, float x)
  21. {
  22.     float rads;
  23.     float degs;
  24.     rads = atan2f(y, x);
  25.     if(rads < 0.0f)
  26.         rads += (M_PI * 2);
  27.     degs = RAD_TO_DEG(rads);
  28.     /*if(degs < 0.0f) // Optional method
  29.         degs += 360.0f;*/
  30.     return degs;
  31. }
  32.  
  33. float my_cosf_deg(float deg)
  34. {
  35.     // It might also be a good idea to handle degs < -180º
  36.     float rads;
  37.     float new_degs = deg;
  38.     while(new_degs > 180.0f)
  39.         new_degs -= 360.0f;
  40.     rads = DEG_TO_RAD(new_degs);
  41.     return cosf(rads);
  42. }
  43.  
  44. float my_sinf_deg(float deg)
  45. {
  46.     float rads;
  47.     float new_degs = deg;
  48.     while(new_degs > 180.0f)
  49.         new_degs -= 360.0f;
  50.     rads = DEG_TO_RAD(new_degs);
  51.     return sinf(rads);
  52. }
  53.  
  54. float my_tanf_deg(float deg)
  55. {
  56.     float rads;
  57.     float new_degs = deg;
  58.     while(new_degs > 180.0f)
  59.         new_degs -= 360.0f;
  60.     rads = DEG_TO_RAD(new_degs);
  61.     return tanf(rads);
  62. }
  63.  
  64. // NOD - North Orientated Display
  65.  
  66. float my_nod_atan2f_deg(float y, float x)
  67. {
  68.     float rads;
  69.     float degs;
  70.     rads = atan2f(y, x);
  71.     if(rads < 0.0f)
  72.         rads += (M_PI * 2);
  73.     degs = RAD_TO_DEG(rads);
  74.     if(degs >= 0.0f && degs <= 360.0f)
  75.         degs += 90.0f;
  76.     if(degs > 360.0f)
  77.         degs -= 360.0f;
  78.  
  79.     return degs;
  80. }
  81.  
  82. float my_nod_cosf_deg(float deg)
  83. {
  84.     float rads;
  85.     float new_degs = deg;
  86.     new_degs -= 90;
  87.     while(new_degs > 180.0f)
  88.         new_degs -= 360.0f;
  89.     rads = DEG_TO_RAD(new_degs);
  90.     return cosf(rads);
  91. }
  92.  
  93. float my_nod_sinf_deg(float deg)
  94. {
  95.     float rads;
  96.     float new_degs = deg;
  97.     new_degs -= 90;
  98.     while(new_degs > 180.0f)
  99.         new_degs -= 360.0f;
  100.     rads = DEG_TO_RAD(new_degs);
  101.     return sinf(rads);
  102. }
  103.  
  104. // DOUBLE FUNCTIONS (not overloaded)
  105.  
  106. double my_atan2d_PIx2_rads(double y, double x)
  107. {
  108.     double rads;
  109.     rads = atan2f(y, x);
  110.     if(rads < 0.0f)
  111.         rads += (M_PI * 2);
  112.     return rads;
  113. }
  114.  
  115. double my_atan2d_deg(double y, double x)
  116. {
  117.     double rads;
  118.     double degs;
  119.     rads = atan2f(y, x);
  120.     if(rads < 0.0f)
  121.         rads += (M_PI * 2);
  122.     degs = RAD_TO_DEG(rads);
  123.     /*if(degs < 0.0f) // Optional method
  124.         degs += 360.0f;*/
  125.     return degs;
  126. }
  127.  
  128. double my_cosd_deg(double deg)
  129. {
  130.     double rads;
  131.     double new_degs = deg;
  132.     while(new_degs > 180.0f)
  133.         new_degs -= 360.0f;
  134.     rads = DEG_TO_RAD(new_degs);
  135.     return cosf(rads);
  136. }
  137.  
  138. double my_sind_deg(double deg)
  139. {
  140.     double rads;
  141.     double new_degs = deg;
  142.     while(new_degs > 180.0f)
  143.         new_degs -= 360.0f;
  144.     rads = DEG_TO_RAD(new_degs);
  145.     return sinf(rads);
  146. }
  147.  
  148. double my_tand_deg(double deg)
  149. {
  150.     double rads;
  151.     double new_degs = deg;
  152.     while(new_degs > 180.0f)
  153.         new_degs -= 360.0f;
  154.     rads = DEG_TO_RAD(new_degs);
  155.     return tanf(rads);
  156. }
  157.  
  158. // NOD - North Orientated Display
  159.  
  160. double my_nod_atan2d_deg(double y, double x)
  161. {
  162.     double rads;
  163.     double degs;
  164.     rads = atan2f(y, x);
  165.     if(rads < 0.0f)
  166.         rads += (M_PI * 2);
  167.     degs = RAD_TO_DEG(rads);
  168.     if(degs >= 0.0f && degs <= 360.0f)
  169.         degs += 90.0f;
  170.     if(degs > 360.0f)
  171.         degs -= 360.0f;
  172.  
  173.     return degs;
  174. }
  175.  
  176. double my_nod_cosd_deg(double deg)
  177. {
  178.     double rads;
  179.     double new_degs = deg;
  180.     new_degs -= 90;
  181.     while(new_degs > 180.0f)
  182.         new_degs -= 360.0f;
  183.     rads = DEG_TO_RAD(new_degs);
  184.     return cosf(rads);
  185. }
  186.  
  187. double my_nod_sind_deg(double deg)
  188. {
  189.     double rads;
  190.     double new_degs = deg;
  191.     new_degs -= 90;
  192.     while(new_degs > 180.0f)
  193.         new_degs -= 360.0f;
  194.     rads = DEG_TO_RAD(new_degs);
  195.     return sinf(rads);
  196. }
  197.  
  198. // TODO:
  199. // acos(), asin(), secant?
  200. #endif
RAW Paste Data
Want to get better at C?
Learn to code C in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top