Created function for detecting self-intersection