HiveBrain v1.2.0
Get Started
← Back to all entries
snippetModerate

How can I determine if two vertices on a polygon are consecutive?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
canaretwodeterminehowpolygonverticesconsecutive

Problem

Suppose I have a set of points that construct a convex polygon in the Cartesian plane with the points as its vertices. I randomly choose two vertices and want to know if they are consecutive vertices on the polygon. Does anyone know of an algorithm that would do this, as in you would input the array of points and the two vertices as parameters and it would return a boolean?

It's given that the polygon exists and is unique.

Solution

As the polygon is convex, it is simple! Two vertices are consecutive if all other vertices are located on the same side of the line that goes through these two points. This means that the cross product of the vector feom one of the pionts to the other one with the vector from the first point to any other one in the polygon have the same sign (all negative or all positive). For example, if those two points are called $P_1$ and $P_2$, you should compute all cross product of $\langle P_1, P_2\rangle$ with $\langle P_1, P_i\rangle$ ($P_i$ here means all other vertices).

Also, as discussed in the comment, we have some degenerate cases. The degenerate case is a common phenomenon in computational geometry, that is mostly handled separately. Here, the degenerate case is three points are located on a line.

Context

StackExchange Computer Science Q#116045, answer score: 12

Revisions (0)

No revisions yet.