icossin2.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. /*
  13. * Sine and Cosine of arctangents, calculated by
  14. * (sin(atan(index/100.0))*1024.+0.5)
  15. * (cos(atan(index/100.0))*1024.+0.5)
  16. * To use, get rational tangent between 0<=tan<=1, scale by 100,
  17. * and look up sin and cos, and use linear interpolation. divide by 1024.
  18. * Maximum error is 0.0020. Without linear interpolation, it's 0.010.
  19. */
  20. static
  21. int16_t sinus[] = {
  22. 0, /* 0.00 */
  23. 10, /* 0.01 */
  24. 20, /* 0.02 */
  25. 31, /* 0.03 */
  26. 41, /* 0.04 */
  27. 51, /* 0.05 */
  28. 61, /* 0.06 */
  29. 72, /* 0.07 */
  30. 82, /* 0.08 */
  31. 92, /* 0.09 */
  32. 102, /* 0.10 */
  33. 112, /* 0.11 */
  34. 122, /* 0.12 */
  35. 132, /* 0.13 */
  36. 142, /* 0.14 */
  37. 152, /* 0.15 */
  38. 162, /* 0.16 */
  39. 172, /* 0.17 */
  40. 181, /* 0.18 */
  41. 191, /* 0.19 */
  42. 201, /* 0.20 */
  43. 210, /* 0.21 */
  44. 220, /* 0.22 */
  45. 230, /* 0.23 */
  46. 239, /* 0.24 */
  47. 248, /* 0.25 */
  48. 258, /* 0.26 */
  49. 267, /* 0.27 */
  50. 276, /* 0.28 */
  51. 285, /* 0.29 */
  52. 294, /* 0.30 */
  53. 303, /* 0.31 */
  54. 312, /* 0.32 */
  55. 321, /* 0.33 */
  56. 330, /* 0.34 */
  57. 338, /* 0.35 */
  58. 347, /* 0.36 */
  59. 355, /* 0.37 */
  60. 364, /* 0.38 */
  61. 372, /* 0.39 */
  62. 380, /* 0.40 */
  63. 388, /* 0.41 */
  64. 397, /* 0.42 */
  65. 405, /* 0.43 */
  66. 412, /* 0.44 */
  67. 420, /* 0.45 */
  68. 428, /* 0.46 */
  69. 436, /* 0.47 */
  70. 443, /* 0.48 */
  71. 451, /* 0.49 */
  72. 458, /* 0.50 */
  73. 465, /* 0.51 */
  74. 472, /* 0.52 */
  75. 480, /* 0.53 */
  76. 487, /* 0.54 */
  77. 493, /* 0.55 */
  78. 500, /* 0.56 */
  79. 507, /* 0.57 */
  80. 514, /* 0.58 */
  81. 520, /* 0.59 */
  82. 527, /* 0.60 */
  83. 533, /* 0.61 */
  84. 540, /* 0.62 */
  85. 546, /* 0.63 */
  86. 552, /* 0.64 */
  87. 558, /* 0.65 */
  88. 564, /* 0.66 */
  89. 570, /* 0.67 */
  90. 576, /* 0.68 */
  91. 582, /* 0.69 */
  92. 587, /* 0.70 */
  93. 593, /* 0.71 */
  94. 598, /* 0.72 */
  95. 604, /* 0.73 */
  96. 609, /* 0.74 */
  97. 614, /* 0.75 */
  98. 620, /* 0.76 */
  99. 625, /* 0.77 */
  100. 630, /* 0.78 */
  101. 635, /* 0.79 */
  102. 640, /* 0.80 */
  103. 645, /* 0.81 */
  104. 649, /* 0.82 */
  105. 654, /* 0.83 */
  106. 659, /* 0.84 */
  107. 663, /* 0.85 */
  108. 668, /* 0.86 */
  109. 672, /* 0.87 */
  110. 676, /* 0.88 */
  111. 681, /* 0.89 */
  112. 685, /* 0.90 */
  113. 689, /* 0.91 */
  114. 693, /* 0.92 */
  115. 697, /* 0.93 */
  116. 701, /* 0.94 */
  117. 705, /* 0.95 */
  118. 709, /* 0.96 */
  119. 713, /* 0.97 */
  120. 717, /* 0.98 */
  121. 720, /* 0.99 */
  122. 724, /* 1.00 */
  123. 728, /* 1.01 */
  124. };
  125. static
  126. int16_t cosinus[] = {
  127. 1024, /* 0.00 */
  128. 1024, /* 0.01 */
  129. 1024, /* 0.02 */
  130. 1024, /* 0.03 */
  131. 1023, /* 0.04 */
  132. 1023, /* 0.05 */
  133. 1022, /* 0.06 */
  134. 1022, /* 0.07 */
  135. 1021, /* 0.08 */
  136. 1020, /* 0.09 */
  137. 1019, /* 0.10 */
  138. 1018, /* 0.11 */
  139. 1017, /* 0.12 */
  140. 1015, /* 0.13 */
  141. 1014, /* 0.14 */
  142. 1013, /* 0.15 */
  143. 1011, /* 0.16 */
  144. 1010, /* 0.17 */
  145. 1008, /* 0.18 */
  146. 1006, /* 0.19 */
  147. 1004, /* 0.20 */
  148. 1002, /* 0.21 */
  149. 1000, /* 0.22 */
  150. 998, /* 0.23 */
  151. 996, /* 0.24 */
  152. 993, /* 0.25 */
  153. 991, /* 0.26 */
  154. 989, /* 0.27 */
  155. 986, /* 0.28 */
  156. 983, /* 0.29 */
  157. 981, /* 0.30 */
  158. 978, /* 0.31 */
  159. 975, /* 0.32 */
  160. 972, /* 0.33 */
  161. 969, /* 0.34 */
  162. 967, /* 0.35 */
  163. 963, /* 0.36 */
  164. 960, /* 0.37 */
  165. 957, /* 0.38 */
  166. 954, /* 0.39 */
  167. 951, /* 0.40 */
  168. 947, /* 0.41 */
  169. 944, /* 0.42 */
  170. 941, /* 0.43 */
  171. 937, /* 0.44 */
  172. 934, /* 0.45 */
  173. 930, /* 0.46 */
  174. 927, /* 0.47 */
  175. 923, /* 0.48 */
  176. 920, /* 0.49 */
  177. 916, /* 0.50 */
  178. 912, /* 0.51 */
  179. 909, /* 0.52 */
  180. 905, /* 0.53 */
  181. 901, /* 0.54 */
  182. 897, /* 0.55 */
  183. 893, /* 0.56 */
  184. 890, /* 0.57 */
  185. 886, /* 0.58 */
  186. 882, /* 0.59 */
  187. 878, /* 0.60 */
  188. 874, /* 0.61 */
  189. 870, /* 0.62 */
  190. 866, /* 0.63 */
  191. 862, /* 0.64 */
  192. 859, /* 0.65 */
  193. 855, /* 0.66 */
  194. 851, /* 0.67 */
  195. 847, /* 0.68 */
  196. 843, /* 0.69 */
  197. 839, /* 0.70 */
  198. 835, /* 0.71 */
  199. 831, /* 0.72 */
  200. 827, /* 0.73 */
  201. 823, /* 0.74 */
  202. 819, /* 0.75 */
  203. 815, /* 0.76 */
  204. 811, /* 0.77 */
  205. 807, /* 0.78 */
  206. 804, /* 0.79 */
  207. 800, /* 0.80 */
  208. 796, /* 0.81 */
  209. 792, /* 0.82 */
  210. 788, /* 0.83 */
  211. 784, /* 0.84 */
  212. 780, /* 0.85 */
  213. 776, /* 0.86 */
  214. 773, /* 0.87 */
  215. 769, /* 0.88 */
  216. 765, /* 0.89 */
  217. 761, /* 0.90 */
  218. 757, /* 0.91 */
  219. 754, /* 0.92 */
  220. 750, /* 0.93 */
  221. 746, /* 0.94 */
  222. 742, /* 0.95 */
  223. 739, /* 0.96 */
  224. 735, /* 0.97 */
  225. 731, /* 0.98 */
  226. 728, /* 0.99 */
  227. 724, /* 1.00 */
  228. 720, /* 1.01 */
  229. };
  230. void
  231. icossin2(int x, int y, int *cosp, int *sinp)
  232. {
  233. int sinsign, cossign, tan, tan10, rem;
  234. int16_t *stp, *ctp;
  235. if(x == 0){
  236. if(y >= 0)
  237. *sinp = ICOSSCALE, *cosp = 0;
  238. else
  239. *sinp = -ICOSSCALE, *cosp = 0;
  240. return;
  241. }
  242. sinsign = cossign = 1;
  243. if(x < 0){
  244. cossign = -1;
  245. x = -x;
  246. }
  247. if(y < 0){
  248. sinsign = -1;
  249. y = -y;
  250. }
  251. if(y > x){
  252. tan = 1000*x/y;
  253. tan10 = tan/10;
  254. stp = &cosinus[tan10];
  255. ctp = &sinus[tan10];
  256. }else{
  257. tan = 1000*y/x;
  258. tan10 = tan/10;
  259. stp = &sinus[tan10];
  260. ctp = &cosinus[tan10];
  261. }
  262. rem = tan-(tan10*10);
  263. *sinp = sinsign*(stp[0]+(stp[1]-stp[0])*rem/10);
  264. *cosp = cossign*(ctp[0]+(ctp[1]-ctp[0])*rem/10);
  265. }