Modified algorithm to find the closest item on the display at a specified direction.
Modified the algorithm to take care of edge cases.