I have to make a program to find all convex quadrilaterals from the given list of 2d points.
I have tried it with vector cross product but it doesn’t seem to be a correct solution.
Maybe there is some effective algorithm to this problem but I can not find it.
This is an example case with inputs and outputs:
Input
Number of Points:
6
coordinates of points (x,y):
0 0
0 1
1 0
1 1
2 0
2 1
Output
Number of convex quadrilaterals:
9
A quadrilateral is convex if its diagonals intersect. Conversely, if two line segments intersect, then their four endpoints make a convex quadrilateral.
Every pair of points gives you a line segment, and every point of intersection between two line segments corresponds to a convex quadrilateral.
You can find the points of intersection using either the naïve algorithm that compares all pairs of segments, or the Bentley–Ottmann algorithm. The former takes O(n4); and the latter O((n2 + q) log n) (where q is the number of convex quadrilaterals). In the worst case q = Θ(n4) — consider n points on a circle — so Bentley–Ottmann is not always faster.
Here’s the naïve version in Python:
And an example run: