home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume24 / chemtab / part01 / doregisgph.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-12  |  10.3 KB  |  428 lines

  1. #ifdef    ReGIS
  2.  
  3. /*
  4.  * chemtab - a periodic table data base (C) 1990 Jim King
  5.  *           jek5036@ritvax.isc.rit.edu (formerly pulsar@lsrhs)
  6.  *
  7.  * doregisgph.c - Graph better for ReGIS compatible users (OCT 1990)
  8.  */
  9.  
  10. #include <curses.h>
  11. #include "windows.h"
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include "variables.h"
  15. #include "element.h"
  16. #include "graph.h"
  17. #include "undefs.h"
  18. #include <signal.h>
  19.  
  20. extern int numr(), stop();
  21.  
  22. #endif ReGIS
  23. /*
  24.  * find(ch, en)
  25.  *
  26.  * inputs: char ch; int    enum;
  27.  * returns: floating point integer
  28.  * purpose: replace many switch statements
  29.  */
  30. find(ch, en)
  31. int    ch, en;
  32. {
  33.     struct elem    *pos;
  34.     int    i;
  35.     
  36.     for (pos = e; en != pos->anum; pos = pos->next) ;
  37.  
  38.     switch(ch) {
  39.         case 'a': x = pos->anum; break;
  40.         case 'b': x = pos->amass; break;
  41.         case 'c': x = pos->melt; if (x == MEL) x = -999.0;
  42.               break;
  43.         case 'd': x = pos->boil; if (x == BOI) x = -999.0;
  44.               break;
  45.         case 'e': x = pos->fio; if (x == FIO) x = -999.0;
  46.               break;
  47.         case 'f': x = pos->eneg; if (x == ENG) x = -999.0;
  48.                break;
  49.         case 'g': x = pos->spht; if (x == SPHT) x = -999.0;
  50.               break;
  51.         case 'h': x = pos->dens; if (x == DENS) x = -999.0;
  52.               break;
  53.         case 'i': x = pos->arad; if (x == ARD) x = -999.0;
  54.               break;
  55.         default:  x = pos->year; if (x == YEA) x = -999.0;
  56.               break;
  57.     }
  58. }
  59.  
  60. /*
  61.  * doregisgph - no input
  62.  *
  63.  * purpose:    ReGIS graphics support
  64.  */
  65. doregisgph()
  66. {
  67. #ifdef    ReGIS
  68.     char    c, str[80];
  69.     int    i, j, grid, lastx, lasty, connect, type, firstup;
  70.     struct elem    *tmp;
  71.     
  72. #ifdef    USERSHELL
  73.     signal(SIGTSTP, SIG_IGN);
  74. #endif    USERSHELL
  75.  
  76.     for (i = 0; i < XAXISDEF; i++)
  77.         xaxis[i] = 0;
  78.     for (i = 0; i < YAXISDEF; i++)
  79.         yaxis[i] = 0;
  80.  
  81.     wclear(graph); wrefresh(graph); clear(); refresh();
  82.     menu("ReGIS Graphing Module");
  83.     mvwaddstr(mn, 0, 0, "a] Atomic Number");
  84.     mvwaddstr(mn, 1, 0, "b] Atomic Mass");
  85.     mvwaddstr(mn, 2, 0, "c] Melting Temp.");
  86.     mvwaddstr(mn, 3, 0, "d] Boiling Temp.");
  87.     mvwaddstr(mn, 4, 0, "e] Ionization Energy");
  88.     mvwaddstr(mn, 5, 0, "f] Electronegativity");
  89.     mvwaddstr(mn, 6, 0, "g] Specific Heat");
  90.     mvwaddstr(mn, 7, 0, "h] Density");
  91.     mvwaddstr(mn, 8, 0, "i] Atomic Radius");
  92.     mvwaddstr(mn, 9, 0, "j] Discovery Year");
  93.     mvwaddstr(mn, 10, 0, "Graphing by this, across the X axis.");
  94.     mvwaddstr(mn, 13, 0, "*** Due to lack of information at this time, graphs beyond");
  95.     mvwaddstr(mn, 14, 0, "*** Atomic number 86 may be misleading.  The program is designed");
  96.     mvwaddstr(mn, 15, 0, "*** to compensate, but nothing is perfect.");
  97.     mvwaddstr(mn, 16, 0, ">>> PRESS RETURN TO RETURN TO MAIN MENU.");
  98. first:    mvwaddstr(mn, 11, 0, "Your choice: ");
  99.     wrefresh(mn);                /* collect their choices */
  100.     cur = mn; xp = 14; yp = 11;
  101.     crmode();
  102.     noecho();
  103.     c1 = getchar();    /* X axis chc */
  104.     if (c1 == '?') { help(3); goto first; }
  105.     if (c1 == '\n') {
  106. #ifdef    USERSHELL
  107.         signal(SIGTSTP, stop);
  108. #endif    USERSHELL
  109.         wclear(mn);
  110.         return;
  111.     }
  112.     if (c1 < 'a' || c1 > 'j')
  113.         goto first;
  114.     for (i = 12; i < 17; i++) {
  115.         wmove(mn, i, 0); wclrtoeol(mn);
  116.     }
  117.  
  118.     find(c1, e->anum);
  119.     xmax = xmin = x;
  120.     if (gtot > 0) {
  121.         for (tmp = e; tmp->next != NULL; tmp = tmp->next) {
  122.             find(c1, tmp->anum);
  123.             if (x == -999.0) continue;
  124.             if (x > xmax) xmax = x;
  125.             if (x < xmin) xmin = x;
  126.         }
  127.     } else {
  128.         for (i = 1; i < gtot; i++) {
  129.             find(c1, sub1[i]);
  130.             if (x == -999.0) continue;
  131.             if (i == 1) xmax = xmin = x;
  132.             else {
  133.                 if (x > xmax) xmax = x;
  134.                 if (x < xmin) xmin = x;
  135.             }
  136.         }
  137.     }
  138.  
  139.     mvwaddstr(mn, 10, 0, "If you would rather use the maximum and minimum values of");
  140.     mvwaddstr(mn, 11, 0, "all the elements, hit RETURN once.");
  141.     wmove(mn, 12, 0); wprintw(mn, "All the element's values: Minimum %f, Maximum %f", xmin, xmax);
  142. rl4:    mvwaddstr(mn, 13, 0, "What range, X Minimum? ");
  143.     wrefresh(mn);
  144.     cur = mn; xp = 23; yp = 13;
  145.     echo(); nocrmode();
  146.     gets(str);
  147.     if (str[0] == '?') { help(4); goto rl4; }
  148.     if (!strlen(str)) {
  149.         xmin = -999;
  150.         goto nt;
  151.     }
  152.     xmin = atof(str);
  153.     fixup(mn); fixup(stdscr);
  154.     mvwaddstr(mn, 13, 0, "What range, X Maximum [RETURN for value above]? ");
  155.     wrefresh(mn);
  156.     cur = mn; xp = 49; yp = 13;
  157.     echo(); nocrmode();
  158.     gets(str);
  159.     if (strlen(str))
  160.         xmax = atof(str);
  161.     fixup(mn);
  162.     
  163. nt:    wmove(mn, 10, 0); wclrtoeol(mn); wmove(mn, 11, 0); wclrtoeol(mn);
  164.     mvwaddstr(mn, 10, 0, "Graphing by this, down the Y axis.   ");
  165. scnd:    wmove(mn, 12, 0); wclrtoeol(mn); wmove(mn, 13, 0); wclrtoeol(mn);
  166.     mvwaddstr(mn, 11, 0, "Your choice: ");
  167.     wrefresh(mn);
  168.     cur = mn; xp = 13; yp = 11;
  169.     noecho(); crmode();
  170.     c2 = wgetch(mn); /* Y axis chc */
  171.     if (c2 == '?') { help(5); goto scnd; }
  172.     if (c2 < 'a' || c2 > 'j')
  173.         goto scnd;
  174.     noecho();
  175.  
  176.     /* Here we find the x axis max and min values */
  177.  
  178.     bot("Calculating the X axis max and min values...");
  179.  
  180.     if (xmin == -999) {
  181.         find(c1, e->anum);
  182.         xmax = xmin = x;
  183.         for (tmp = e; tmp->next != NULL; tmp = tmp->next) {
  184.             find(c1, tmp->anum);
  185.             if (x == -999.0) continue;
  186.             if (x > xmax) xmax = x;
  187.             if (x < xmin) xmin = x;
  188.         }
  189.     }
  190.  
  191.     /* Here we find the y axis min and max values */
  192.     bot("Calculating the Y axis max and min values...");
  193.  
  194.     if (gtot > 0) {
  195.         find(c2, e->anum);
  196.         ymax = ymin = x;
  197.         for (tmp = e; tmp->next != NULL; tmp = tmp->next) {
  198.             find(c2, tmp->anum);
  199.             if (x == -999.0) continue;
  200.             if (x > ymax) ymax = x;
  201.             if (x < ymin) ymin = x;
  202.         }
  203.     } else {
  204.         for (i = 1; i < gtot; i++) {
  205.             find(c2, sub1[i]);
  206.             if (x == -999.0) continue;
  207.             if (i == 1)
  208.                 ymax = ymin = x;
  209.             else {
  210.                 if (x > ymax) ymax = x;
  211.                 if (x < ymin) ymin = x;
  212.             }
  213.         }
  214.     }
  215.  
  216.     /* Now we have min & maxs we have to put a number in
  217.        each slot on both axes using the scale */
  218.  
  219.     bot("Scaling...");
  220.     scale = (xmax - xmin) / (double)XAXISDEF;
  221.     xaxis[1] = xmin;
  222.     for (i = 2; i < XAXISDEF; i++)
  223.         xaxis[i] = xaxis[i-1] + scale;
  224.  
  225.     scale = (ymax - ymin) / (double)YAXISDEF;
  226.     yaxis[YAXISDEF-1] = ymin;
  227.     for (i = YAXISDEF-2; i > 1; i--)
  228.         yaxis[i] = yaxis[i+1] + scale;
  229.  
  230. rl6:    noecho(); crmode();
  231.     mvwaddstr(btm, 0, 0, "Would you like a grid? ");
  232.     wrefresh(btm);
  233.     cur = btm; xp = 24; yp = 0;
  234.     switch(getchar()) {
  235.         case 'y':
  236.         case 'Y': grid = 1; break;
  237.         case '?': help(6); goto rl6;
  238.         default:  grid = 0; break;
  239.     }
  240.     if (c1 < 'c') {
  241. rl7:        mvwaddstr(btm, 0, 0, "Would you like the points connected? ");
  242.         wrefresh(btm);
  243.         cur = btm; xp = 38; yp = 0;
  244.         switch(getchar()) {
  245.             case '?': help(7); goto rl7;
  246.             case 'y': case 'Y': connect = 1; break;
  247.             default: connect = 0; break;
  248.         }
  249.     } else connect = 0;
  250. rl8:    wclear(btm);
  251.     mvwaddstr(btm, 0, 0, "Would you like dots instead of hashmarks (slower) ");
  252.     wrefresh(btm);
  253.     cur = btm; xp = 44; yp = 0;
  254.     switch(getchar()) {
  255.         case 'y': case 'Y': type = 1; break;
  256.         case '?': help(8); goto rl8;
  257.         default: type = 0; break;
  258.     }
  259. #ifdef    LASERPRT
  260. rl9:    wclear(btm);
  261.     mvwaddstr(btm, 0, 0, "Would you like a laser print file of this? ");
  262.     wrefresh(btm);
  263.     cur = btm; xp = 40; yp = 0;
  264.     switch(getchar()) {
  265.         case 'y': case 'Y': laser = 1; break;
  266.         case '?': help(19); goto rl9;
  267.         default: laser = 0; break;
  268.     }
  269.     if (laser == 1) {
  270.         if ((lsr = fopen("laser", "w")) == NULL) {
  271.             bot("Sorry, the file 'laser' could not be opened for writing.");
  272.             sleep(2);
  273.             bot("No laser print document will be made.");
  274.             sleep(2);
  275.             bot(" ");
  276.             laser = 0;
  277.         } else
  278.             fprintf(lsr, "\014");
  279.         bot("A copy will be found in the file 'laser' when you exit chemtab.");
  280.         sleep(2);
  281.     }
  282. #endif    LASERPRT
  283.     bot("Initializing for graphics...");
  284.     wclear(mn); wrefresh(mn);
  285.     wclear(btm); wrefresh(btm);
  286.     clear(); refresh();
  287.  
  288.     initregis();
  289.     regisgoto(450, 10);
  290.     chcolor(RED_3);
  291.     text(3, 0, -5, "Chemtab V2.01");
  292.     regisgoto(400 - (strlen(gname[numr(c1)]) * 10), YMAXDEF + 20);
  293.     text(2, 0, 0, gname[numr(c1)]);
  294.     regisgoto(YMINDEF - 46, 200 + (strlen(gname[numr(c2)]) * 10));
  295.     text(2, 90, 0, gname[numr(c2)]);
  296.     chcolor(WHT_3);
  297.     mybox(1, XMINDEF, YMINDEF, XMAXDEF, YMAXDEF);
  298.     mybox(2, 0, 0, 798, 478);
  299.     for (i = YAXISDEF-1; i > 1; i -= YAXISDEF/7) {
  300.         if (c2 < 'f' || c2 > 'i')
  301.             sprintf(str, "%d -", (int)(yaxis[i]+.5));
  302.         else if (yaxis[i] > 9.99)
  303.             sprintf(str, "%2.3f", yaxis[i]);
  304.         else
  305.             sprintf(str, "%2.3f", yaxis[i]);
  306.         chcolor(BLU_3);
  307.         if (grid) line(XMINDEF, i+YBOUND+1, XMAXDEF, i+YBOUND+1);
  308.         chcolor(RED_3);
  309.         regisgoto(XMINDEF-60, i-7+YBOUND);
  310.         text(1, 0, 5, str);
  311.     }
  312.     for (i = 1; i < XAXISDEF; i += XAXISDEF/10) {
  313.         if (c1 < 'f' || c1 > 'i')
  314.             sprintf(str, "%d", (int)(xaxis[i]+.5));
  315.         else
  316.             sprintf(str, "%4.2f", xaxis[i]);
  317.         chcolor(BLU_3);
  318.         if (grid && i < XAXISDEF-20) line(i+XBOUND+1, YMINDEF, i+XBOUND+1, YMAXDEF);
  319.         chcolor(RED_3);
  320.         regisgoto(i-6+XBOUND, YMAXDEF+2);
  321.         text(1, 0, 5, str);
  322.     }
  323.  
  324.     firstup++;
  325.     if (gtot <= 0) {
  326.         for (tmp = e; tmp->next != NULL; tmp = tmp->next) {
  327.             find(c1, tmp->anum);
  328.             if (x == -999.0) continue;
  329.             if (x < xmin)
  330.                 continue;
  331.             else if (x < xaxis[2])
  332.                 xspot = 1;
  333.             else {
  334.                 for (j = 2; j < XAXISDEF; j++) {
  335.                     if (x > xaxis[j])
  336.                         continue;
  337.                     else
  338.                         break;
  339.                 }
  340.                 xspot = j;
  341.                 if (x > xmax)
  342.                     continue;
  343.             }
  344.  
  345.             /* Found where it goes on the x, now the y */
  346.  
  347.             find(c2, tmp->anum);    
  348.             if (x == -999.0) continue;
  349.             if (x < yaxis[YAXISDEF-1])
  350.                 yspot = YAXISDEF-1;
  351.             else {
  352.                 for (j = YAXISDEF-2; j > 1; j--) {
  353.                     if (x > yaxis[j])
  354.                         continue;
  355.                     else
  356.                         break;
  357.                 }
  358.                 yspot = j;
  359.             }
  360.             chcolor(YEL_3);
  361.             if (firstup) { lastx = xspot; lasty = yspot; firstup = 0; }
  362.             if (connect && i != 1) {
  363.                 chcolor(GRN_3);
  364.                 line(xspot+XBOUND, yspot+YBOUND, lastx+XBOUND, lasty+YBOUND);
  365.                 lastx = xspot; lasty = yspot;
  366.             }
  367.             hash(xspot+XBOUND, yspot+YBOUND, type);
  368.         }
  369.     } else {
  370.         for (i = 1; i < gtot; i++) {
  371.             find(c1, sub1[i]);
  372.             if (x == -999.0) continue;
  373.             if (x < xmin)
  374.                 continue;
  375.             else if (x < xaxis[2])
  376.                 xspot = 1;
  377.             else {
  378.                 for (j = 2; j < XAXISDEF; j++) {
  379.                     if (x > xaxis[j])
  380.                         continue;
  381.                     else
  382.                         break;
  383.                 }
  384.                 xspot = j;
  385.                 if (x > xmax)
  386.                     continue;
  387.             }
  388.  
  389.             /* Found where it goes on the x, now the y */
  390.  
  391.             find(c2, sub1[i]);
  392.             if (x == -999.0) continue;
  393.             if (x < yaxis[YAXISDEF-1])
  394.                 yspot = YAXISDEF-1;
  395.             else {
  396.                 for (j = YAXISDEF; j > 1; j--) {
  397.                     if (x > yaxis[j])
  398.                         continue;
  399.                     else
  400.                         break;
  401.                 }
  402.                 yspot = j;
  403.             }
  404.             chcolor(YEL_3);
  405.             if (i == 1) lastx = xspot, lasty = yspot;
  406.             if (connect && i != 1) {
  407.                 chcolor(GRN_3);
  408.                 line(xspot+XBOUND, yspot+YBOUND, lastx+XBOUND, lasty+YBOUND);
  409.                 lastx = xspot; lasty = yspot;
  410.             }
  411.             hash(xspot+XBOUND, yspot+YBOUND, type);
  412.         }
  413.     }
  414.     regispc();
  415.     endregis();
  416. #ifdef    LASERPRT
  417.     if (laser)
  418.         fclose(lsr);
  419. #endif    LASERPRT
  420.     clear(); refresh();
  421.     wclear(graph); wrefresh(graph);
  422. #ifdef    USERSHELL
  423.     signal(SIGTSTP, stop);
  424. #endif    USERSHELL
  425.     return(0);
  426. #endif ReGIS
  427. }
  428.