icossin.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * Integer sine and cosine for integral degree argument.
  14. * Tables computed by (sin,cos)(PI*d/180).
  15. */
  16. static int16_t sinus[91] = {
  17. 0, /* 0 */
  18. 18, /* 1 */
  19. 36, /* 2 */
  20. 54, /* 3 */
  21. 71, /* 4 */
  22. 89, /* 5 */
  23. 107, /* 6 */
  24. 125, /* 7 */
  25. 143, /* 8 */
  26. 160, /* 9 */
  27. 178, /* 10 */
  28. 195, /* 11 */
  29. 213, /* 12 */
  30. 230, /* 13 */
  31. 248, /* 14 */
  32. 265, /* 15 */
  33. 282, /* 16 */
  34. 299, /* 17 */
  35. 316, /* 18 */
  36. 333, /* 19 */
  37. 350, /* 20 */
  38. 367, /* 21 */
  39. 384, /* 22 */
  40. 400, /* 23 */
  41. 416, /* 24 */
  42. 433, /* 25 */
  43. 449, /* 26 */
  44. 465, /* 27 */
  45. 481, /* 28 */
  46. 496, /* 29 */
  47. 512, /* 30 */
  48. 527, /* 31 */
  49. 543, /* 32 */
  50. 558, /* 33 */
  51. 573, /* 34 */
  52. 587, /* 35 */
  53. 602, /* 36 */
  54. 616, /* 37 */
  55. 630, /* 38 */
  56. 644, /* 39 */
  57. 658, /* 40 */
  58. 672, /* 41 */
  59. 685, /* 42 */
  60. 698, /* 43 */
  61. 711, /* 44 */
  62. 724, /* 45 */
  63. 737, /* 46 */
  64. 749, /* 47 */
  65. 761, /* 48 */
  66. 773, /* 49 */
  67. 784, /* 50 */
  68. 796, /* 51 */
  69. 807, /* 52 */
  70. 818, /* 53 */
  71. 828, /* 54 */
  72. 839, /* 55 */
  73. 849, /* 56 */
  74. 859, /* 57 */
  75. 868, /* 58 */
  76. 878, /* 59 */
  77. 887, /* 60 */
  78. 896, /* 61 */
  79. 904, /* 62 */
  80. 912, /* 63 */
  81. 920, /* 64 */
  82. 928, /* 65 */
  83. 935, /* 66 */
  84. 943, /* 67 */
  85. 949, /* 68 */
  86. 956, /* 69 */
  87. 962, /* 70 */
  88. 968, /* 71 */
  89. 974, /* 72 */
  90. 979, /* 73 */
  91. 984, /* 74 */
  92. 989, /* 75 */
  93. 994, /* 76 */
  94. 998, /* 77 */
  95. 1002, /* 78 */
  96. 1005, /* 79 */
  97. 1008, /* 80 */
  98. 1011, /* 81 */
  99. 1014, /* 82 */
  100. 1016, /* 83 */
  101. 1018, /* 84 */
  102. 1020, /* 85 */
  103. 1022, /* 86 */
  104. 1023, /* 87 */
  105. 1023, /* 88 */
  106. 1024, /* 89 */
  107. 1024, /* 90 */
  108. };
  109. void
  110. icossin(int deg, int *cosp, int *sinp)
  111. {
  112. int sinsign, cossign;
  113. int16_t *stp, *ctp;
  114. deg %= 360;
  115. if(deg < 0)
  116. deg += 360;
  117. sinsign = 1;
  118. cossign = 1;
  119. stp = 0;
  120. ctp = 0;
  121. switch(deg/90){
  122. case 2:
  123. sinsign = -1;
  124. cossign = -1;
  125. deg -= 180;
  126. /* fall through */
  127. case 0:
  128. stp = &sinus[deg];
  129. ctp = &sinus[90-deg];
  130. break;
  131. case 3:
  132. sinsign = -1;
  133. cossign = -1;
  134. deg -= 180;
  135. /* fall through */
  136. case 1:
  137. deg = 180-deg;
  138. cossign = -cossign;
  139. stp = &sinus[deg];
  140. ctp = &sinus[90-deg];
  141. break;
  142. }
  143. *sinp = sinsign*stp[0];
  144. *cosp = cossign*ctp[0];
  145. }