occ.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "astro.h"
  10. Occ o1, o2;
  11. Obj2 xo1, xo2;
  12. void
  13. occult(Obj2 *p1, Obj2 *p2, double d)
  14. {
  15. int i, i1, N;
  16. double d1, d2, d3, d4;
  17. double x, di, dx, x1;
  18. d3 = 0;
  19. d2 = 0;
  20. occ.t1 = -100;
  21. occ.t2 = -100;
  22. occ.t3 = -100;
  23. occ.t4 = -100;
  24. occ.t5 = -100;
  25. for(i=0; i<=NPTS+1; i++) {
  26. d1 = d2;
  27. d2 = d3;
  28. d3 = dist(&p1->point[i], &p2->point[i]);
  29. if(i >= 2 && d2 <= d1 && d2 <= d3)
  30. goto found;
  31. }
  32. return;
  33. found:
  34. N = 2880*PER/NPTS; /* 1 min steps */
  35. i -= 2;
  36. set3pt(p1, i, &o1);
  37. set3pt(p2, i, &o2);
  38. di = i;
  39. x = 0;
  40. dx = 2./N;
  41. for(i=0; i<=N; i++) {
  42. setpt(&o1, x);
  43. setpt(&o2, x);
  44. d1 = d2;
  45. d2 = d3;
  46. d3 = dist(&o1.act, &o2.act);
  47. if(i >= 2 && d2 <= d1 && d2 <= d3)
  48. goto found1;
  49. x += dx;
  50. }
  51. fprint(2, "bad 1 \n");
  52. return;
  53. found1:
  54. if(d2 > o1.act.semi2+o2.act.semi2+50)
  55. return;
  56. di += x - 3*dx;
  57. x = 0;
  58. for(i=0; i<3; i++) {
  59. setime(day + deld*(di + x));
  60. (*p1->obj)();
  61. setobj(&xo1.point[i]);
  62. (*p2->obj)();
  63. setobj(&xo2.point[i]);
  64. x += 2*dx;
  65. }
  66. dx /= 60;
  67. x = 0;
  68. set3pt(&xo1, 0, &o1);
  69. set3pt(&xo2, 0, &o2);
  70. for(i=0; i<=240; i++) {
  71. setpt(&o1, x);
  72. setpt(&o2, x);
  73. d1 = d2;
  74. d2 = d3;
  75. d3 = dist(&o1.act, &o2.act);
  76. if(i >= 2 && d2 <= d1 && d2 <= d3)
  77. goto found2;
  78. x += 1./120.;
  79. }
  80. fprint(2, "bad 2 \n");
  81. return;
  82. found2:
  83. if(d2 > o1.act.semi2 + o2.act.semi2)
  84. return;
  85. i1 = i-1;
  86. x1 = x - 1./120.;
  87. occ.t3 = di + i1 * dx;
  88. occ.e3 = o1.act.el;
  89. d3 = o1.act.semi2 - o2.act.semi2;
  90. if(d3 < 0)
  91. d3 = -d3;
  92. d4 = o1.act.semi2 + o2.act.semi2;
  93. for(i=i1,x=x1;; i++) {
  94. setpt(&o1, x);
  95. setpt(&o2, x);
  96. d1 = d2;
  97. d2 = dist(&o1.act, &o2.act);
  98. if(i != i1) {
  99. if(d1 <= d3 && d2 > d3) {
  100. occ.t4 = di + (i-.5) * dx;
  101. occ.e4 = o1.act.el;
  102. }
  103. if(d2 > d4) {
  104. if(d1 <= d4) {
  105. occ.t5 = di + (i-.5) * dx;
  106. occ.e5 = o1.act.el;
  107. }
  108. break;
  109. }
  110. }
  111. x += 1./120.;
  112. }
  113. for(i=i1,x=x1;; i--) {
  114. setpt(&o1, x);
  115. setpt(&o2, x);
  116. d1 = d2;
  117. d2 = dist(&o1.act, &o2.act);
  118. if(i != i1) {
  119. if(d1 <= d3 && d2 > d3) {
  120. occ.t2 = di + (i+.5) * dx;
  121. occ.e2 = o1.act.el;
  122. }
  123. if(d2 > d4) {
  124. if(d1 <= d4) {
  125. occ.t1 = di + (i+.5) * dx;
  126. occ.e1 = o1.act.el;
  127. }
  128. break;
  129. }
  130. }
  131. x -= 1./120.;
  132. }
  133. }
  134. void
  135. setpt(Occ *o, double x)
  136. {
  137. double y;
  138. y = x * (x-1);
  139. o->act.ra = o->del0.ra +
  140. x*o->del1.ra + y*o->del2.ra;
  141. o->act.decl2 = o->del0.decl2 +
  142. x*o->del1.decl2 + y*o->del2.decl2;
  143. o->act.semi2 = o->del0.semi2 +
  144. x*o->del1.semi2 + y*o->del2.semi2;
  145. o->act.el = o->del0.el +
  146. x*o->del1.el + y*o->del2.el;
  147. }
  148. double
  149. pinorm(double a)
  150. {
  151. while(a < -pi)
  152. a += pipi;
  153. while(a > pi)
  154. a -= pipi;
  155. return a;
  156. }
  157. void
  158. set3pt(Obj2 *p, int i, Occ *o)
  159. {
  160. Obj1 *p1, *p2, *p3;
  161. double a;
  162. p1 = p->point+i;
  163. p2 = p1+1;
  164. p3 = p2+1;
  165. o->del0.ra = p1->ra;
  166. o->del0.decl2 = p1->decl2;
  167. o->del0.semi2 = p1->semi2;
  168. o->del0.el = p1->el;
  169. a = p2->ra - p1->ra;
  170. o->del1.ra = pinorm(a);
  171. a = p2->decl2 - p1->decl2;
  172. o->del1.decl2 = pinorm(a);
  173. o->del1.semi2 = p2->semi2 - p1->semi2;
  174. o->del1.el = p2->el - p1->el;
  175. a = p1->ra + p3->ra - 2*p2->ra;
  176. o->del2.ra = pinorm(a)/2;
  177. a = p1->decl2 + p3->decl2 - 2*p2->decl2;
  178. o->del2.decl2 = pinorm(a)/2;
  179. o->del2.semi2 = (p1->semi2 + p3->semi2 - 2*p2->semi2) / 2;
  180. o->del2.el = (p1->el + p3->el - 2*p2->el) / 2;
  181. }