home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 223.dms / 223.adf / Source / star5.c < prev    next >
C/C++ Source or Header  |  1988-07-25  |  8KB  |  256 lines

  1. /*=========================================================================
  2.   Star5.c -- This module handles star and constellation selection. It is
  3.   invoked when the user clicks the right or left mouse button on a star.
  4.   It also includes the search algorithms and search table initialization
  5.   for searching by star and constellation name.
  6.  
  7.   Credits for Star Chart:
  8.        Robert L. Hill of the Orange County, CA. Amiga Friends User Group
  9.                       wrote the original version of StarChart in AmigaBasic
  10.                       The star data and many of the main functions of this
  11.                       version are derived from that program.
  12.  
  13.        Ray R. Larson  wrote the c version 1.0 of StarChart, 'intuitionizing'
  14.                       and enhancing the speed and functions of the original.
  15.  
  16.   Copyright (c) 1986 by Ray R. Larson
  17.   
  18.   This program may be freely distributed and copied, but may not be sold
  19.   without the permission of the author. If you modify or enhance it, 
  20.   please include the above credits (and please send me a copy!).
  21.  
  22. Ray R. Larson
  23. 6425 Central Ave. #304
  24. El Cerrito, CA 94530
  25.  
  26. BitNet  LARSON@UCBCMSA
  27. =========================================================================*/
  28. /*------------Header file for all of the standard stuff----*/ 
  29. /*-------------plus definitions of global structures-------*/
  30. #include "star.h" 
  31.  
  32. /*------------------ Global search tables ---------*/
  33. extern SHORT StarIndex[];
  34. extern SHORT *ConstList[];
  35. extern SHORT *GreekList[];
  36.  
  37. /*------------------ extern vars ------------------*/
  38.  
  39. extern struct star_rec StarTable[];
  40. extern char *Greek[]; 
  41. extern struct cons_rec Constel[];
  42. extern struct Coord coords[];
  43. extern struct RastPort *rp;
  44. extern struct Window *w;
  45. extern struct Screen *scr;      /* pointer to the custom screen    - RRL */
  46. extern UBYTE WTMessage[];
  47. extern UBYTE WinTitle[];
  48. extern SHORT DisplayFlags, ConDispFlags; /* type of info display */
  49.  
  50. /**************************************************************************
  51.  * Handle mouse button clicks in the star display area
  52.  **************************************************************************/
  53. Do_buttons(code,mousex,mousey)
  54.    USHORT code;
  55.    SHORT mousex, mousey;
  56.   SHORT stars, constindex, *starlist, hitlist[15], scancoords();
  57.   
  58.   if(ONCHART((LONG)mousex,(LONG)mousey) == 0) return; /* mouse not in star area */
  59.  
  60.   switch(code) {
  61.  
  62.     case SELECTUP:
  63.           stars = scancoords((LONG)mousex,(LONG)mousey,hitlist,15);
  64.               if (stars) DisplayInfo(hitlist,stars,0,DisplayFlags);
  65.               break;
  66.  
  67.     case MENUUP: 
  68.           stars = scancoords((LONG)mousex,(LONG)mousey,hitlist,1);
  69.               if (stars)
  70.                 { constindex = StarTable[hitlist[0]].ConsNum;
  71.                   sprintf(WTMessage,
  72.                            "The Constellation of %s - The %s",
  73.                              Constel[constindex].ConsName,
  74.                  Constel[constindex].ConsMean);
  75.                   SetWindowTitles(w,WTMessage,(UBYTE *)-1);
  76.                   PlotConst(ConstList[constindex],1,ConDispFlags);
  77.                   Delay(200L);
  78.           PlotConst(ConstList[constindex],0,ConDispFlags);
  79.                   SetWindowTitles(w,WinTitle,(UBYTE *)-1);
  80.                 }
  81.          break;
  82.  
  83.   } /* end code switch */
  84. }
  85.  
  86.  
  87. /**************************************************************************
  88.  * SearchIndex - This function performs a binary search on the StarIndex
  89.  *   table to locate a named star record. If successful, it returns a
  90.  *   pointer to a star_rec structure for the matching star, if the star
  91.  *   is not in the table, it returns NULL.
  92.  **************************************************************************/
  93. SHORT SearchIndex(key)
  94. char *key;
  95. {
  96.   SHORT mid, low, high, compval;
  97.   BOOL found = FALSE;
  98.  
  99.   low = 1;
  100.   high = NumStars; /* in star.h */
  101.   while (low <= high)
  102.      { mid = (low + high)/2;
  103.        compval = strcmp(key,StarTable[StarIndex[mid]].StarName);
  104.        if (compval == 0) return(StarIndex[mid]);
  105.        else
  106.           { if (compval < 0) high = mid-1;
  107.         else low = mid + 1;
  108.           }
  109.       }
  110.    /* if we get to here nothing was found */
  111.    return(0);
  112. }
  113.  
  114. /**********************************************************************
  115.  * scancoords() - this function performs a simple sequential search of
  116.  *                the coords array of coordinates for stars, it finds
  117.  *                up to MaxCount objects located within +-2 pixels of
  118.  *                the supplied findx and findy position on the starchart
  119.  *                the indexes of the matching coords elements are added
  120.  *                to the List array, and the total number found is returned
  121.  *                as the result of the function.
  122.  *********************************************************************/
  123. SHORT scancoords(findx,findy,list,maxcount)
  124. LONG findx, findy;
  125. SHORT list[], maxcount;
  126. {
  127.   LONG xmax,xmin,ymax,ymin;
  128.   SHORT i, hitcount = 0;
  129.   
  130.   xmax = findx + 3;
  131.   xmin = findx - 3;
  132.   ymax = findy + 3;
  133.   ymin = findy - 3;
  134.  
  135.   for(i=1;(i <= NumStars) && (maxcount); i++)
  136.      {
  137.     if (INRANGE(coords[i].x,xmin,xmax))
  138.        { 
  139.              if(INRANGE(coords[i].y,ymin,ymax))
  140.            { 
  141.                   list[hitcount] = i;
  142.           hitcount++;
  143.              maxcount--;
  144.            }
  145.           }
  146.      }
  147.  
  148.   return(hitcount);
  149. }
  150.  
  151. /**************************************************************************
  152.  * SearchConst - This function performs a binary search on the Constell
  153.  *   table to locate a named constellation record. If successful, it returns
  154.  *   the index of the found record. If the constellation name is not in the
  155.  *   table, it returns NULL. It only performs partial matching of the names
  156.  **************************************************************************/
  157. SHORT SearchConst(key)
  158. char *key;
  159. {
  160.   SHORT mid, low, high; 
  161.   SHORT compval, complen;
  162.  
  163.   low = 1;
  164.   high = NumCons; /* in star.h */
  165.   complen = strlen(key) - 2;
  166.   if (complen < 3) complen = strlen(key);
  167.  
  168.   while (low <= high)
  169.      { mid = (low + high)/2;
  170.        compval = strncmp(key,Constel[mid].ConsName,complen);
  171.        if (compval == 0) return(mid);
  172.        else
  173.           { if (compval < 0) high = mid-1;
  174.         else low = mid + 1;
  175.           }
  176.       }
  177.    /* if we get to here nothing was found */
  178.    return(0);
  179. }
  180.  
  181. /**************************************************************************
  182.  * SearchGreek - This function performs a sequential search on the Greek
  183.  *   table to locate a named Greek letter record. If successful, it returns
  184.  *   the index of the found record. If the key is not in the table, 
  185.  *   it returns NULL.
  186.  **************************************************************************/
  187. SHORT SearchGreek(key)
  188. char *key;
  189. {
  190.   SHORT compval, i;
  191.  
  192.   for (i = 1; i <= NumGreek; i++)
  193.      {
  194.        compval = strncmp(key,Greek[i],strlen(key));
  195.        if (compval == 0) return(i);
  196.      }
  197.    /* if we get to here nothing was found */
  198.    return(0);
  199. }
  200.  
  201. /* the following routine extracts the first word */
  202. firststr(str,first,rest)
  203. char *str, *first, *rest;
  204.   char *blank;
  205.   extern char *index();
  206.  
  207.   /* extract first word from input key */
  208.  
  209.   while (isspace(*str)) str++;
  210.  
  211.   strcpy(first,str); /* first non-blank chars */
  212.   blank = index(first,' ');
  213.   if (blank == NULL)
  214.      { *rest = '\0';
  215.        return;
  216.      }
  217.   *blank = '\0';
  218.   blank++;
  219.   while(isspace(*blank)) blank++;
  220.   strcpy(rest, blank);
  221.   return;
  222. }
  223.  
  224. /**********************************************************************
  225.  * AndLists - perform Boolean set intersection on two arrays of numbers
  226.  * this function returns the single number that the sets have in common
  227.  * (that is, it is pretty simple version of set intersection, could have
  228.  * it fill in another list with all the common members, but for this 
  229.  * application that should not be needed.) Note that the first ([0])
  230.  * element in each list holds the number of array members in the rest
  231.  * of the list, and that each list is in ascending order of the values
  232.  * of the elements.
  233.  **********************************************************************/
  234. SHORT AndLists(L1,L2)
  235. SHORT L1[],L2[];
  236. {
  237.   SHORT i, j, count1, count2;
  238.  
  239.   count1 = L1[0];
  240.   count2 = L2[0];
  241.   i = j = 1;
  242.  
  243.   while ((i <= count1) && (j <= count2))
  244.      {
  245.        if (L1[i] == L2[j]) return(L1[i]);
  246.        if (L1[i] < L2[j]) i++;
  247.        if (L1[i] > L2[j]) j++;
  248.  
  249.      }
  250.  
  251. /* if we get to here there were no common elements */
  252. return(0);
  253. }
  254.