Can someone please show me an algorithm to write a function that returns true if 4 points form a quadrilateral, and false otherwise? The points do not come with any order.
I've tried to check all permutations of the 4 points and see if there's 3 points that forms a straight line. If there's 3 points that forms a straight line than it's not quadrilateral. But then I realize that there's no way to tell the order. And then I struggle for several hours of thinking and googling with no result :(
I've read these questions:
- find if 4 points on a plane form a rectangle?
- Determining ordering of vertices to form a quadrilateral
But still find no solution. In the case of 1, it can't detect another kind of quadrilateral, and in 2 it assumes that the points are quadirateral already. Are there any other way to find out if 4 points form a quadirateral?
Thanks before.
EDIT FOR CLARIFICATION:
I define quadrilateral as simple quadrilateral, basically all shapes shown in this picture:
except the shape with "quadrilateral" and "complex" caption.
As for problems with the "checking for collinear triplets" approach, I tried to check the vertical, horizontal, and diagonal lines with something like this:
def is_linear_line(pt1, pt2, pt3):
return (pt1[x] == pt2[x] == pt3[x] ||
pt1[y] == pt2[y] == pt3[y] ||
slope(pt1, pt2) == slope(pt2, pt3))
And realize that rectangle and square will count as linear line since the slope of the points will be all the same. Hope this clears things out.