dist.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "astro.h"
  2. double
  3. dist(Obj1 *p, Obj1 *q)
  4. {
  5. double a;
  6. a = sin(p->decl2)*sin(q->decl2) +
  7. cos(p->decl2)*cos(q->decl2)*cos(p->ra-q->ra);
  8. a = fabs(atan2(pyth(a), a)) / radsec;
  9. return a;
  10. }
  11. int
  12. rline(int f)
  13. {
  14. char *p;
  15. int c;
  16. static char buf[1024];
  17. static int bc, bn, bf;
  18. if(bf != f) {
  19. bf = f;
  20. bn = 0;
  21. }
  22. p = line;
  23. do {
  24. if(bn <= 0) {
  25. bn = read(bf, buf, sizeof(buf));
  26. if(bn <= 0)
  27. return 1;
  28. bc = 0;
  29. }
  30. c = buf[bc];
  31. bn--; bc++;
  32. *p++ = c;
  33. } while(c != '\n');
  34. return 0;
  35. }
  36. double
  37. sunel(double t)
  38. {
  39. int i;
  40. i = floor(t);
  41. if(i < 0 || i > NPTS+1)
  42. return -90;
  43. t = osun.point[i].el +
  44. (t-i)*(osun.point[i+1].el - osun.point[i].el);
  45. return t;
  46. }
  47. double
  48. rise(Obj2 *op, double el)
  49. {
  50. Obj2 *p;
  51. int i;
  52. double e1, e2;
  53. e2 = 0;
  54. p = op;
  55. for(i=0; i<=NPTS; i++) {
  56. e1 = e2;
  57. e2 = p->point[i].el;
  58. if(i >= 1 && e1 <= el && e2 > el)
  59. goto found;
  60. }
  61. return -1;
  62. found:
  63. return i - 1 + (el-e1)/(e2-e1);
  64. }
  65. double
  66. set(Obj2 *op, double el)
  67. {
  68. Obj2 *p;
  69. int i;
  70. double e1, e2;
  71. e2 = 0;
  72. p = op;
  73. for(i=0; i<=NPTS; i++) {
  74. e1 = e2;
  75. e2 = p->point[i].el;
  76. if(i >= 1 && e1 > el && e2 <= el)
  77. goto found;
  78. }
  79. return -1;
  80. found:
  81. return i - 1 + (el-e1)/(e2-e1);
  82. }
  83. double
  84. solstice(int n)
  85. {
  86. int i;
  87. double d1, d2, d3;
  88. d3 = (n*pi)/2 - pi;
  89. if(n == 0)
  90. d3 += pi;
  91. d2 = 0.;
  92. for(i=0; i<=NPTS; i++) {
  93. d1 = d2;
  94. d2 = osun.point[i].ra;
  95. if(n == 0) {
  96. d2 -= pi;
  97. if(d2 < -pi)
  98. d2 += pipi;
  99. }
  100. if(i >= 1 && d3 >= d1 && d3 < d2)
  101. goto found;
  102. }
  103. return -1;
  104. found:
  105. return i - (d3-d2)/(d1-d2);
  106. }
  107. double
  108. betcross(double b)
  109. {
  110. int i;
  111. double d1, d2;
  112. d2 = 0;
  113. for(i=0; i<=NPTS; i++) {
  114. d1 = d2;
  115. d2 = osun.point[i].mag;
  116. if(i >= 1 && b >= d1 && b < d2)
  117. goto found;
  118. }
  119. return -1;
  120. found:
  121. return i - (b-d2)/(d1-d2);
  122. }
  123. double
  124. melong(Obj2 *op)
  125. {
  126. Obj2 *p;
  127. int i;
  128. double d1, d2, d3;
  129. d2 = 0;
  130. d3 = 0;
  131. p = op;
  132. for(i=0; i<=NPTS; i++) {
  133. d1 = d2;
  134. d2 = d3;
  135. d3 = dist(&p->point[i], &osun.point[i]);
  136. if(i >= 2 && d2 >= d1 && d2 >= d3)
  137. goto found;
  138. }
  139. return -1;
  140. found:
  141. return i - 2;
  142. }
  143. #define NEVENT 100
  144. Event events[NEVENT];
  145. Event* eventp = 0;
  146. void
  147. event(char *format, char *arg1, char *arg2, double tim, int flag)
  148. {
  149. Event *p;
  150. if(flag & DARK)
  151. if(sunel(tim) > -12)
  152. return;
  153. if(flag & LIGHT)
  154. if(sunel(tim) < 0)
  155. return;
  156. if(eventp == 0)
  157. eventp = events;
  158. p = eventp;
  159. if(p >= events+NEVENT) {
  160. fprint(2, "too many events\n");
  161. return;
  162. }
  163. eventp++;
  164. p->format = format;
  165. p->arg1 = arg1;
  166. p->arg2 = arg2;
  167. p->tim = tim;
  168. p->flag = flag;
  169. }
  170. void
  171. evflush(void)
  172. {
  173. Event *p;
  174. if(eventp == 0)
  175. return;
  176. qsort(events, eventp-events, sizeof *p, evcomp);
  177. for(p = events; p<eventp; p++) {
  178. if((p->flag&SIGNIF) && flags['s'])
  179. print("ding ding ding ");
  180. print(p->format, p->arg1, p->arg2);
  181. if(p->flag & PTIME)
  182. ptime(day + p->tim*deld);
  183. print("\n");
  184. }
  185. eventp = 0;
  186. }
  187. int
  188. evcomp(void *a1, void *a2)
  189. {
  190. double t1, t2;
  191. Event *p1, *p2;
  192. p1 = a1;
  193. p2 = a2;
  194. t1 = p1->tim;
  195. t2 = p2->tim;
  196. if(p1->flag & SIGNIF)
  197. t1 -= 1000.;
  198. if(p2->flag & SIGNIF)
  199. t2 -= 1000.;
  200. if(t1 > t2)
  201. return 1;
  202. if(t2 > t1)
  203. return -1;
  204. return 0;
  205. }
  206. double
  207. pyth(double x)
  208. {
  209. x *= x;
  210. if(x > 1)
  211. x = 1;
  212. return sqrt(1-x);
  213. }
  214. char*
  215. skip(int n)
  216. {
  217. int i;
  218. char *cp;
  219. cp = line;
  220. for(i=0; i<n; i++) {
  221. while(*cp == ' ' || *cp == '\t')
  222. cp++;
  223. while(*cp != '\n' && *cp != ' ' && *cp != '\t')
  224. cp++;
  225. }
  226. while(*cp == ' ' || *cp == '\t')
  227. cp++;
  228. return cp;
  229. }