protected List < int [ ] > getNearestNeighbors ( float xSource , float ySource ) { List < int [ ] > results = new ArrayList < int [ ] > ( ) ; // Get the coverage data source pixels for x and y CoverageDataSourcePixel xPixel = getXSourceMinAndMax ( xSource ) ; CoverageDataSourcePixel yPixel = getYSourceMinAndMax ( ySource ) ; // Determine which x pixel is the closest, the second closest, and the // distance to the second pixel int firstX ; int secondX ; float xDistance ; if ( xPixel . getOffset ( ) > .5 ) { firstX = xPixel . getMax ( ) ; secondX = xPixel . getMin ( ) ; xDistance = 1.0f - xPixel . getOffset ( ) ; } else { firstX = xPixel . getMin ( ) ; secondX = xPixel . getMax ( ) ; xDistance = xPixel . getOffset ( ) ; } // Determine which y pixel is the closest, the second closest, and the // distance to the second pixel int firstY ; int secondY ; float yDistance ; if ( yPixel . getOffset ( ) > .5 ) { firstY = yPixel . getMax ( ) ; secondY = yPixel . getMin ( ) ; yDistance = 1.0f - yPixel . getOffset ( ) ; } else { firstY = yPixel . getMin ( ) ; secondY = yPixel . getMax ( ) ; yDistance = yPixel . getOffset ( ) ; } // Add the closest neighbor results . add ( new int [ ] { firstX , firstY } ) ; // Add the second and third neighbor based upon the x and y distances to // second coordinates if ( xDistance <= yDistance ) { results . add ( new int [ ] { secondX , firstY } ) ; results . add ( new int [ ] { firstX , secondY } ) ; } else { results . add ( new int [ ] { firstX , secondY } ) ; results . add ( new int [ ] { secondX , firstY } ) ; } // Add the farthest neighbor results . add ( new int [ ] { secondX , secondY } ) ; // If right on the boundary between the forward and backwards pixel, add // the backwards pixel options if ( xPixel . getOffset ( ) == 0 ) { results . add ( new int [ ] { xPixel . getMin ( ) - 1 , yPixel . getMin ( ) } ) ; results . add ( new int [ ] { xPixel . getMin ( ) - 1 , yPixel . getMax ( ) } ) ; } if ( yPixel . getOffset ( ) == 0 ) { results . add ( new int [ ] { xPixel . getMin ( ) , yPixel . getMin ( ) - 1 } ) ; results . add ( new int [ ] { xPixel . getMax ( ) , yPixel . getMin ( ) - 1 } ) ; } if ( xPixel . getOffset ( ) == 0 && yPixel . getOffset ( ) == 0 ) { results . add ( new int [ ] { xPixel . getMin ( ) - 1 , yPixel . getMin ( ) - 1 } ) ; } return results ; }
Determine the nearest neighbors of the source pixel sorted by closest to farthest neighbor