home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / findtran.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-09  |  1.1 KB  |  49 lines

  1. /*
  2.  *    findtrans.c  --  search function for edtrans().
  3.  *
  4.  *    9 july 1989  Olle Olsson.
  5.  */
  6.  
  7. #include <math.h>
  8. #include "edtrans.h"
  9.  
  10. int findtrans( tp, size, ap, screenx, screeny )
  11. transform *tp;        /* the transforms to search among */
  12. int size;        /* number of transforms */
  13. area *ap;        /* limiting area */
  14. int screenx;        /* screen x */
  15. int screeny;        /* screen y */
  16. {
  17. double dis, l;        /* distance */
  18. int selt;        /* the index of the selected transform */
  19. int i;
  20. double x, y;        /* real coordinates of screenx, screeny */
  21.  
  22. /* find the (transformed) origin that is closest the screen point */
  23. /* (you could take the middle point or check if it is inside or ...) */
  24.  
  25. /* negative means "not found" */
  26. if (size <= 0)
  27.     return (-1);
  28.  
  29. /* compute the real coordinates  (y is upside down) */
  30. x = ap -> xlow + screenx * ap -> xlen / MAX_COL;
  31. y = ap -> ylow + (MAX_ROW - screeny) * ap -> ylen / MAX_ROW;
  32.  
  33. for (i = selt = 0, dis = 1e20; i < size; ++i, ++tp)
  34.     {
  35.     l = sqrt( (x - tp -> b1) * (x - tp -> b1) +
  36.           (y - tp -> b2) * (y - tp -> b2) );
  37.  
  38.     if (l < dis)
  39.         {
  40.         dis = l;
  41.         selt = i;
  42.         }
  43.     }
  44.  
  45. return (selt);
  46. }
  47.  
  48.  
  49.