Avoid using
in header files
It looks like the code you have written is in a header file, which is to be included in an actual application that needs to intersect polygons. If you include this file, it also means you pull in all the using
declarations, which could result in unexpected behavior. It might be safe to move the using
declarations into class Polygon
, but I'd rather avoid doing it altogether.
You also forgot using std::nullopt
.
The move constructor should take a non-const
reference
Since you want to move things from an object, that object might have to be modified, so move constructors should take non-const
references.
Consider adding operator&()
Polygon intersection shares a lot of similarities with bitwise AND operations. It makes sense to add an operator&()
to your class, so you can write:
Polygon a{...}, b{...};Polygon c = a & b;
Your sorting function is wrong
You can't sort polygons into counter-clockwise order the way you did. The angles you calculate are relative to coordinate (0, 0), but the polygons themselves can be anywhere. You have to calculate the angles relative to a point inside the intersection.
See this StackOverflow question for a possible algorithm.
Alternatively, since the points of the input polygons are already sorted, and the intersection of two convex polygons is just two pieces of each polygon concatenated, you should not need to sort at all.