I am writing an algorithm that splits complex (self-intersecting) polygons into one or many simple polygons so that they can be used in my collision detection algorithm. This can be done by adding/removing vertices and creating new polygons.
To do so I detect all segment intersections in the polygon and sort them by lower intersection x, then handle each segment intersection in order. However, there are two types of intersection that can occur, and I need to split the polygon differently depending on which type has occurred. Here is an illustration of the two possible cases:
I already know what I should do to handle each intersection type, but what I don't know is how can I detect whether a given intersection corresponds to case 1 or case 2. Is there a way to determine this? I have all the information I needed in my algorithm (vertices and their order, intersection and segments that caused them, ...).
Suppose there is an intersection between segments (P_i, P_i+1) and (P_j, P_j+1) at point Q, with j>i.
Case 1: I split the polygon in 2 polygons, [Q, P_i+1, ..., P_j , Q] and [Q, P_j+1, ..., P_i, Q].
Case 2: I insert a vertex V in the polygon, and the resulting polygon is [P1, ..., P_i, V, P_i+1, ..., P_j, Q, P_j+1, ..., P1]
So the missing piece of information I need is to figure out whether the loop formed by [Q, P_i+1, ..., P_j] is an "outer" loop (case 1) or an "inner" loop (case 2).
I've read stuff about signed area of the polygon formed by the loop but didn't understand all of it. I'm not looking for the most efficient algorithm, just one that would work. Thanks!