home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / astrnomy / ephem421.zip / WATCH.C < prev   
C/C++ Source or Header  |  1990-09-13  |  12KB  |  417 lines

  1. /* these functions allow you to watch the sky or the solar system via a
  2.  * simple character-graphics representation on the screen. 
  3.  * the interaction starts by using the current time. then control with
  4.  *    END returns to table form; or
  5.  *    RETURN advances time by one StpSz; or
  6.  *    h advances once by 1 hour; or
  7.  *    d advances once by 24 hours (1 day); or
  8.  *    w advances once by 7 days (1 week); or
  9.  *    any other key free runs by StpSz until any key is hit.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include "astro.h"
  15. #include "circum.h"
  16. #include "screen.h"
  17.  
  18. #define    SSZCOL    1        /* column to show solar system z coords */
  19.  
  20. #define    DOMESKY        0    /* flags for watch_sky() */
  21. #define    ALTAZSKY    1    /* flags for watch_sky() */
  22.  
  23. #define    SKYACC    3600.    /* desired sky plot accuracy, in arc seconds */
  24. #define    SSACC    3600.    /* desired solar system plot accuracy, in arc secs */
  25.  
  26. /* macros to convert row(col) in range 1..NR(1..NC) to fraction in range 0..1 */
  27. #define    r2fr(r)        (((r)-1)/(NR-1))
  28. #define    c2fc(c)        (((c)-1)/(NC-1))
  29. #define    fr2r(fr)    ((int)((fr)*(NR-1)+.5)+1)
  30. #define    fc2c(fc)    ((int)((fc)*(NC-1)+.5)+1)
  31.  
  32. /* single-character tag for each body.
  33.  * order must match the #defines in astro.h and screen.h additions.
  34.  */
  35. static char body_tags[] = "evmjsunpSMxy";
  36.  
  37. /* multiple and single loop prompts */
  38. static char frprompt[] = "Running... press any key to stop.";
  39. static char qprompt[]  =
  40. "q to quit, RETURN/h/d/w to step by StpSz/hr/day/wk, or any other to freerun";
  41.  
  42. /* used to locate, record and then erase last plotted chars */
  43. typedef struct {
  44.     double l_fr, l_fc;    /* 2d coords as 0..1 (lower left corner is [0,0]) */
  45.     int     l_r, l_c;    /* screen 2d coords (upper left corner is [1,1]) */
  46.     char l_tag;        /* char to use to print on screen */
  47. } LastDraw;
  48.  
  49. static int trails;    /* !0 if want to leave trails */
  50.  
  51. watch (np, tminc, wbodies)
  52. Now *np;    /* time now and on each step */
  53. double tminc;    /* hrs to increment time by each step */
  54. int wbodies;    /* each bit is !=0 if want that body */
  55. {
  56.     static char *flds[4] = {
  57.         "Sky dome", "Alt/az sky", "Solar system"
  58.     };
  59.     static int fn;    /* begin with 0, then remember for next time */
  60.     int didone = 0;
  61.  
  62.     while (1) {
  63.         int nf;
  64.         flds[3] = trails ? "Leave trails" : "No trails";
  65.         if ((nf = popup (flds, fn, 4)) < 0)
  66.         break;
  67.         fn = nf;
  68.         switch (nf) {
  69.         case 0: watch_sky (DOMESKY, np, tminc, wbodies); didone = 1; break;
  70.         case 1: watch_sky (ALTAZSKY, np, tminc, wbodies); didone = 1; break;
  71.         case 2: watch_solarsystem (np, tminc, wbodies); didone = 1; break;
  72.         case 3: trails ^= 1; break;
  73.         }
  74.     }
  75.  
  76.     if (didone)
  77.         redraw_screen(2);
  78. }
  79.  
  80. /* full alt/az or dome sky view (like the popular astro mags).
  81.  * alt/az: north is at left and right of screen, south at center.
  82.  *   0 elevation is at bottom of screen, zenith at the top.
  83.  * dome: east is left, north is up.
  84.  */
  85. static
  86. watch_sky (style, np, tminc, wbodies)
  87. int style;    /* DOMESKY or ALTAZSKY */
  88. Now *np;    /* time now and on each step */
  89. double tminc;    /* hrs to increment time by each step */
  90. int wbodies;    /* each bit is !=0 if want */
  91. {
  92.     static char east[] = "East";
  93.     static char west[] = "West";
  94.     static char north[] = "North";
  95.     static char south[] = "South";
  96.     double tminc0 = tminc;    /* remember the original */
  97.     /* two draw buffers so we can leave old up while calc new then
  98.      * erase and draw in one quick operation. always calc new in newp
  99.      * buffer and erase previous from lastp. buffers alternate roles.
  100.      */
  101.     LastDraw ld0[NOBJ], ld1[NOBJ], *lp, *lastp = ld0, *newp = ld1;
  102.     int nlast = 0, nnew;
  103.     int once = 1;
  104.     double lmjd, tmp;
  105.     Sky s;
  106.     int p;
  107.  
  108.     /* clear screen and put up the permanent labels */
  109.     c_erase();
  110.     if (style == DOMESKY) {
  111.         f_string (fr2r(.5), fc2c(.5-.5/ASPECT)-7, "East |");
  112.         f_char (fr2r(.5+.5*.707), fc2c(.5-.5*.707/ASPECT)-1, '\\');
  113.         f_string (fr2r(1.), fc2c(.5)-2, south);
  114.         f_char (fr2r(.5+.5*.707), fc2c(.5+.5*.707/ASPECT)+1, '/');
  115.         f_string (fr2r(.5), fc2c(.5+.5/ASPECT)+1, "| West");
  116.         f_char (fr2r(.5-.5*.707), fc2c(.5+.5*.707/ASPECT)+1, '\\');
  117.         f_string (2, NC/2-2, north);
  118.         f_char (fr2r(.5-.5*.707), fc2c(.5-.5*.707/ASPECT)-1, '/');
  119.     } else {
  120.         f_string (NR, 1, north);
  121.         f_string (NR, NC/4, east);
  122.         f_string (NR, NC/2, south);
  123.         f_string (NR, 3*NC/4, west);
  124.         f_string (NR, NC-5, north);   /* -1 more to avoid scrolling */
  125.         f_string (2, NC/2-3, "Zenith");
  126.     }
  127.     f_string (2, 1, tznm);
  128.     f_string (3, 1, "LST");
  129.  
  130.     while (1) {
  131.         if (once)
  132.         print_updating();
  133.  
  134.         /* calculate desired stuff into newp[] */
  135.         nnew = 0;
  136.         for (p = nxtbody(-1); p != -1; p = nxtbody(p))
  137.         if (wbodies & (1<<p)) {
  138.             (void) body_cir (p, SKYACC, np, &s);
  139.             if (s.s_alt >= 0) {
  140.             LastDraw *lnp = newp + nnew;
  141.             if (style == DOMESKY) {
  142.                 tmp = 0.5 - s.s_alt/PI;
  143.                 lnp->l_fr = 0.5 - tmp*cos(s.s_az);
  144.                 lnp->l_fc = 0.5 - tmp*sin(s.s_az)/ASPECT;
  145.             } else {
  146.                 lnp->l_fr = 1.0 - s.s_alt/(PI/2);
  147.                 lnp->l_fc = s.s_az/(2*PI);
  148.             }
  149.             lnp->l_tag = body_tags[p];
  150.             nnew++;
  151.             }
  152.         }
  153.         set_screencoords (newp, nnew);
  154.  
  155.         /* unless we want trails,
  156.          * erase any previous tags (in same order as written) from lastp[].
  157.          */
  158.         if (!trails)
  159.         for (lp = lastp; --nlast >= 0; lp++)
  160.             f_char (lp->l_r, lp->l_c, ' ');
  161.  
  162.         /* print LOCAL time and date we will be using */
  163.         lmjd = mjd - tz/24.0;
  164.         f_time (2, 5, mjd_hr(lmjd));
  165.         f_date (2, 14, mjd_day(lmjd));
  166.         now_lst (np, &tmp);
  167.         f_time (3, 5, tmp);
  168.  
  169.         /* now draw new stuff from newp[] */
  170.         for (lp = newp; lp < newp + nnew; lp++)
  171.         f_char (lp->l_r, lp->l_c, lp->l_tag);
  172.  
  173.         /* swap new and last roles and save new count */
  174.         if (newp == ld0)
  175.         newp = ld1, lastp = ld0;
  176.         else
  177.         newp = ld0, lastp = ld1;
  178.         nlast = nnew;
  179.  
  180.         if (!once)
  181.         slp_sync();
  182.  
  183.         if (once || (chk_char()==0 && read_char()!=0)) {
  184.         if (readwcmd (tminc0, &tminc, &once) < 0)
  185.             break;
  186.         }
  187.  
  188.         /* advance time */
  189.         inc_mjd (np, tminc);
  190.     }
  191. }
  192.  
  193. /* solar system view, "down from the top", first point of aries to the right.
  194.  * always include earth.
  195.  */
  196. static
  197. watch_solarsystem (np, tminc, wbodies)
  198. Now *np;    /* time now and on each step */
  199. double tminc;    /* hrs to increment time by each step */
  200. int wbodies;
  201. {
  202.     /* max au of each planet from sun; in astro.h #defines order */
  203.     static double auscale[] = {.38, .75, 1.7, 5.2, 11., 20., 31., 50.};
  204.     double tminc0 = tminc;    /* remember the original */
  205.     /* two draw buffers so we can leave old up while calc new then
  206.      * erase and draw in one quick operation. always calc new in newp
  207.      * buffer and erase previous from lastp. buffers alternate roles.
  208.      */
  209.     LastDraw ld0[2*NOBJ], ld1[2*NOBJ], *lp, *lastp = ld0, *newp = ld1;
  210.     int nlast = 0, nnew;
  211.     int once = 1;
  212.     double lmjd;
  213.     double scale;
  214.     Sky s;
  215.     int p;
  216.  
  217.     /* set screen scale: largest au we will have to plot.
  218.      * never make it less than 1 au since we always show earth.
  219.      */
  220.     scale = 1.0;
  221.     for (p = MARS; p <= PLUTO; p++)
  222.         if ((wbodies & (1<<p)) && auscale[p] > scale)
  223.         scale = auscale[p];
  224.  
  225.     /* clear screen and put up the permanent labels */
  226.     c_erase();
  227.     f_string (2, 1, tznm);
  228.  
  229.     while (1) {
  230.         if (once)
  231.         print_updating();
  232.  
  233.         /* calculate desired stuff into newp[].
  234.          * fake a sun at center and add earth first.
  235.          * (we get earth's loc when ask for sun)
  236.          */
  237.         nnew = 0;
  238.         set_ss (newp+nnew, 0.0, 0.0, 0.0, 'S');
  239.         nnew += 2;
  240.         (void) body_cir (SUN, SSACC, np, &s);
  241.         set_ss (newp+nnew, s.s_edist/scale, s.s_hlong, 0.0, 'E');
  242.         nnew += 2;
  243.         for (p = MERCURY; p <= PLUTO; p++)
  244.         if (p != MOON && (wbodies & (1<<p))) {
  245.             (void) body_cir (p, SSACC, np, &s);
  246.             set_ss (newp+nnew, s.s_sdist/scale, s.s_hlong, s.s_hlat,
  247.                                 body_tags[p]);
  248.             nnew += 2;
  249.         }
  250.         for (p = OBJX; p != -1; p = (p == OBJX) ? OBJY : -1)
  251.         if (wbodies & (1<<p)) {
  252.             (void) body_cir (p, SSACC, np, &s);
  253.             if (s.s_hlong != NOHELIO && s.s_sdist <= scale) {
  254.             set_ss (newp+nnew, s.s_sdist/scale, s.s_hlong, s.s_hlat,
  255.                                 body_tags[p]);
  256.             nnew += 2;
  257.             }
  258.         }
  259.  
  260.         set_screencoords (newp, nnew);
  261.  
  262.         /* unless we want trails,
  263.          * erase any previous tags (in same order as written) from lastp[].
  264.          */
  265.         if (!trails)
  266.         for (lp = lastp; --nlast >= 0; lp++)
  267.             safe_f_char (lp->l_r, lp->l_c, ' ');
  268.  
  269.         /* print LOCAL time and date we will be using */
  270.         lmjd = mjd - tz/24.0;
  271.         f_time (2, 5, mjd_hr(lmjd));
  272.         f_date (2, 14, mjd_day(lmjd));
  273.  
  274.         /* now draw new stuff from newp[] */
  275.         for (lp = newp; lp < newp + nnew; lp++)
  276.         safe_f_char (lp->l_r, lp->l_c, lp->l_tag);
  277.  
  278.         /* swap new and last roles and save new count */
  279.         if (newp == ld0)
  280.         newp = ld1, lastp = ld0;
  281.         else
  282.         newp = ld0, lastp = ld1;
  283.         nlast = nnew;
  284.  
  285.         if (!once)
  286.         slp_sync();
  287.  
  288.         if (once || (chk_char()==0 && read_char()!=0)) {
  289.         if (readwcmd (tminc0, &tminc, &once) < 0)
  290.             break;
  291.         }
  292.  
  293.         /* advance time */
  294.         inc_mjd (np, tminc);
  295.     }
  296. }
  297.  
  298. /* fill in two LastDraw solar system entries,
  299.  * one for the x/y display, one for the z.
  300.  */
  301. static
  302. set_ss (lp, dist, lg, lt, tag)
  303. LastDraw *lp;
  304. double dist, lg, lt;    /* scaled heliocentric distance, longitude and lat */
  305. char tag;
  306. {
  307.     lp->l_fr = 0.5 - dist*sin(lg)*0.5;
  308.     lp->l_fc = 0.5 + dist*cos(lg)*0.5/ASPECT;
  309.     lp->l_tag = tag;
  310.     lp++;
  311.     /* row is to show course helio altitude but since we resolve collisions
  312.      * by adjusting columns we can get more detail by smaller variations
  313.      * within one column.
  314.      */
  315.     lp->l_fr = 0.5 - dist*sin(lt)*0.5;
  316.     lp->l_fc = c2fc(SSZCOL) + (1 - lp->l_fr)/NC;
  317.     lp->l_tag = tag;
  318. }
  319.  
  320. /* given a list of LastDraw structs with their l_{fr,fc} filled in,
  321.  * fill in their l_{r,c}.
  322.  * TODO: better collision avoidance.
  323.  */
  324. static
  325. set_screencoords (lp, np)
  326. LastDraw lp[];
  327. int np;
  328. {
  329.     LastDraw *lpi;    /* the current basis for comparison */
  330.     LastDraw *lpj;    /* the sweep over other existing cells */
  331.     int i;        /* index of the current basis cell, lpi */
  332.     int j;        /* index of sweep cell, lpj */
  333.     int n;        /* total cells placed so far (ie, # to check) */
  334.  
  335.     /* idea is to place each new item onto the screen.
  336.      * after each placement, look for collisions.
  337.      * if find a colliding pair, move the one with the greater l_fc to
  338.      * the right one cell, then rescan for more collisions.
  339.      * this will yield a result that is sorted by columns by l_fc.
  340.      * TODO: don't just move to the right, try up too for true 2d adjusts.
  341.      */
  342.     for (n = 0; n < np; n++) {
  343.         lpi = lp + n;
  344.         i = n;
  345.         lpi->l_r = fr2r(lpi->l_fr);
  346.         lpi->l_c = fc2c(lpi->l_fc);
  347.       chk:
  348.         for (j = 0; j < n; j++) {
  349.         lpj = lp + j;
  350.         if (i!=j && lpi->l_r == lpj->l_r && lpi->l_c == lpj->l_c) {
  351.             if (lpj->l_fc > lpi->l_fc) {
  352.             /* move lpj and use it as basis for checks now */
  353.             lpi = lpj;
  354.             i = j;
  355.             }
  356.             if (++lpi->l_c > NC)
  357.             lpi->l_c = 1;
  358.             goto chk;
  359.         }
  360.         }
  361.     }
  362. }
  363.  
  364. /* since the solar system scaling is only approximate, and doesn't include
  365.  * object x/y at all, characters might get mapped off screen. this funtion
  366.  * guards against drawing chars off screen. it also moves a char being drawn
  367.  * on the lower right corner of the screem left one to avoid scrolling.
  368.  */
  369. static
  370. safe_f_char (r, c, tag)
  371. int r, c;
  372. char tag;
  373. {
  374.     if (r >= 1 && r <= NR && c >= 1 && c <= NC) {
  375.         if (r == NR && c == NC)
  376.         c -= 1;
  377.         f_char (r, c, tag);
  378.     }
  379. }
  380.  
  381. /* see what the op wants to do now and update prompt/times accordingly.
  382.  * return -1 if we are finished, else 0.
  383.  */
  384. static int
  385. readwcmd (tminc0, tminc, once)
  386. double tminc0;
  387. double *tminc;
  388. int *once;
  389. {
  390.     f_prompt (qprompt);
  391.  
  392.     switch (read_char()) {
  393.     case END:     /* back to table */
  394.         return (-1);
  395.     case '\r':    /* one StpSz step */
  396.         *tminc = tminc0;
  397.         *once = 1;
  398.         break;
  399.     case 'h':    /* one 1-hour step */
  400.         *tminc = 1.0;
  401.         *once = 1;
  402.         break;
  403.     case 'd':    /* one 24-hr step */
  404.         *tminc = 24.0;
  405.         *once = 1;
  406.         break;
  407.     case 'w':    /* 7 day step */
  408.         *tminc = 7*24.0;
  409.         *once = 1;
  410.         break;
  411.     default:        /* free-run */
  412.         *once = 0;
  413.         f_prompt (frprompt);
  414.     }
  415.     return (0);
  416. }
  417.