34 inline float isLeft(ChainHull::Point P0, ChainHull::Point P1, ChainHull::Point P2)
36 return (P1.GetX() - P0.GetX())*(P2.GetY() - P0.GetY()) - (P2.GetX() - P0.GetX())*(P1.GetY() - P0.GetY());
42 std::vector<std::array<double, 2> > ChainHull::Compute(
const std::vector<std::array<double, 2> >& polygon)
46 Point* p =
new Point[polygon.size() + 1];
47 for (
auto const& vertex : polygon) {
48 p[pc++] = Point(vertex[0], vertex[1]);
55 int np = polygon.size();
56 Point* hull =
new Point[np + 1];
57 int nph = chainHull_2D(p, np, hull);
60 std::vector<std::array<double, 2> > result;
61 for (
int i = 0; i < nph - 1; i++) {
62 result.push_back({ {hull[i].GetX(), hull[i].GetY()} });
75 if (p1.GetY() < p2.GetY()) {
77 }
else if (p1.GetY() > p2.GetY()) {
79 }
else if (p1.GetX() < p2.GetX()) {
87 int ChainHull::chainHull_2D(Point* P,
int n, Point* H)
97 float xmin = P[0].GetX();
98 for (i = 1; i < n; i++) {
99 if (P[i].GetX() != xmin) {
104 if (minmax == n - 1) {
105 H[++top] = P[minmin];
106 if (P[minmax].GetY() != P[minmin].GetY()) {
107 H[++top] = P[minmax];
109 H[++top] = P[minmin];
116 float xmax = P[n - 1].GetX();
117 for (i = n - 2; i >= 0; i--) {
118 if (P[i].GetX() != xmax) {
125 H[++top] = P[minmin];
127 while (++i <= maxmin) {
129 if (isLeft(P[minmin], P[maxmin], P[i]) >= 0 && i < maxmin) {
135 if (isLeft(H[top - 1], H[top], P[i]) > 0)
144 if (maxmax != maxmin) {
145 H[++top] = P[maxmax];
149 while (--i >= minmax) {
151 if (isLeft(P[maxmax], P[minmax], P[i]) >= 0 && i > minmax) {
157 if (isLeft(H[top - 1], H[top], P[i]) > 0)
164 if (minmax != minmin) {
165 H[++top] = P[minmin];
Point class needed by 2D convex hull code.
Compute the convex hull of a set of two-dimensional points.
bool operator<(ChainHull::Point const &p1, ChainHull::Point const &p2)
Required to sort points (e.g.
Namespace for the core simulator.