refactored nearasPointOnLine function