home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_08 / 9n08055a < prev    next >
Text File  |  1991-06-20  |  1KB  |  49 lines

  1. /***************** Listing 1 *************************
  2.  
  3.             Nearest Neighbour Interpolation
  4.  
  5.    double nearest(image, x, y)
  6.      float image[2][2], x, y;
  7.  
  8.    image: pointer to the four values of grid i.e.
  9.           a 2 x 2 array ( image[2][2] )
  10.    x    : sample coordinate
  11.    y    : line coordinate
  12.  
  13.                p
  14.             |----->
  15.           image(0,0)         image(0,1)
  16.          -  *-------------------*
  17.          |  |                   |
  18.       q  |  |                   |    
  19.          |  |                   |     y = line
  20.          V  |     o             |     x = sample
  21.             |   (y,x)           |
  22.             |                   |
  23.             *-------------------*
  24.          image(1,0)          image(1,1)
  25.  
  26.     If point "o" falls on upper left corner, then 
  27.     return image(0,0). The point should never fall on 
  28.     any of the other corners because the calling 
  29.     program will ensure against this according to the
  30.     filling sequence of the array image[2][2].
  31.  
  32. ******************************************************
  33. #include <stdio.h>
  34.  
  35. double nearest(image, x, y)
  36.  float image[2][2], x, y;
  37. {
  38.    register i;
  39.    float p, q;
  40.  
  41.    p = x - (int) x;
  42.    q = y - (int) y;
  43.  
  44.    if( (p == 0.0) && (q == 0.0) )
  45.      return( (double) image[0][0] ); /* upper left */
  46.  
  47.    return( image[ (int) (q+0.5) ][ (int) (p+0.5) ] );
  48. }
  49.