added piecewise function for nearest neighbor and tested it