bool earQ(int i, int j, int k, const polygon& p) { triangle t(p.vec_points[i], p.vec_points[k], p.vec_points[k]); if (cw(p.vec_points[i], p.vec_points[j], p.vec_points[k])) return false; for (int m = 0; m < p.get_n(); m++) { if ((m != i) && (m != j) && (m != k)) if (t.contains(p.vec_points[m])) return false; } return true; }
/////////////////////////////////////////////////////////////////////////// // triangulation implementation /////////////////////////////////////////////////////////////////////////// triangulation::triangulation(const polygon& p) { int n = p.get_n(); vector<int> vleft(n), // left neighbor indices vright(n); // rigth neighbor indices for (int j = 0; j < n; j++) { vleft[j] = ((j-1) + n) % n; vright[j] = ((j+1) + n) % n; } int i = n - 1; while ((int)vec_triangles.size() < (n-2)) { i = vright[i]; if (earQ(vleft[i], i, vright[i], p)) { triangle t(p.vec_points[vleft[i]], p.vec_points[i], p.vec_points[vright[i]]); vec_triangles.push_back(t); vleft[vright[i]] = vleft[i]; vright[vleft[i]] = vright[i]; } } }