Algorithm for checking line intersection