home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / C_DISK5.ZIP / MICROCAD / SHAPELST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-26  |  1.2 KB  |  51 lines

  1. // Copyright 1988 Bruce Eckel
  2. // Permission required to distribute source
  3.  
  4. // file: shapelst.cpp
  5. #include "shapelst.hpp"
  6.  
  7. cadshape * shapelist::next() {
  8.     cadshape * r = current->shp;
  9.     if ( r != (cadshape *)0 )
  10.         current = current->next;
  11.     return r;
  12. }
  13.  
  14. void shapelist::remove(cadshape * s) {
  15.     shapelist_element * pp = head, * qq;
  16.     if ( pp->shp == s) {  // check head
  17.         head = pp->next;
  18.         delete pp;
  19.     } else 
  20.     while ( pp->next->shp != 0 ) { 
  21.         // (the next element isn't the last)
  22.         if ( pp->next->shp == s) {
  23.             // unlink the matching element:
  24.             qq = pp->next;
  25.             pp->next = pp->next->next;
  26.             delete qq;
  27.             return;
  28.         }
  29.         pp++;
  30.     }
  31. }
  32.  
  33. // Find the nearest shape to co-ordinates (x,y) :
  34. cadshape * 
  35. shapelist::nearest(unsigned x, unsigned y) { 
  36.   cadshape * kk;
  37.   cadshape * cshape;
  38.   unsigned long distance;
  39.  
  40.   reset();  // start at beginning of list
  41.   cshape = next(); // test the first element
  42.   distance = cshape->range(x,y);
  43.   while ((kk = next()) != 0) {
  44.     if ( kk->range(x,y) < distance ) {
  45.        cshape = kk;
  46.        distance = cshape->range(x,y);
  47.     }
  48.   }
  49.   return cshape;
  50. }
  51.