78Point nextToTop(std::stack<Point> *S) {
93 return (p1.x - p2.x) * (p1.x - p2.x) + (p1.
y - p2.
y) * (p1.
y - p2.
y);
105 int val = (q.y - p.
y) * (r.x - q.x) - (q.x - p.x) * (r.
y - q.y);
110 return (val > 0) ? 1 : 2;
120int compare(
const void *vp1,
const void *vp2) {
121 auto *p1 =
static_cast<const Point *
>(vp1);
122 auto *p2 =
static_cast<const Point *
>(vp2);
125 int o = orientation(p0, *p1, *p2);
127 return (distSq(p0, *p2) >= distSq(p0, *p1)) ? -1 : 1;
130 return (o == 2) ? -1 : 1;
139std::vector<Point> convexHull(std::vector<Point> points, uint64_t size) {
141 int ymin = points[0].y, min = 0;
142 for (
int i = 1; i < size; i++) {
146 if ((y < ymin) || (ymin == y && points[i].x < points[min].x)) {
147 ymin = points[i].y, min = i;
152 std::swap(points[0], points[min]);
165 for (
int i = 1; i < size; i++) {
167 while (i < size - 1 && orientation(p0, points[i], points[i + 1]) == 0) {
171 points[m] = points[i];
182 std::stack<Point> St;
188 for (
int i = 3; i < m; i++) {
192 while (St.size() > 1 &&
193 orientation(nextToTop(&St), St.top(), points[i]) != 2) {
199 std::vector<Point>
result;
201 while (!St.empty()) {