void drawMulPolygons(PointLong* points[256], int *nPts, int vertices, GR_COLOR color)
{
using Point = std::array<int, 2>;
using Points = std::vector<Point>;
using PolygonType = std::vector<Points>
Points allPts;
PolygonType polygon;
int index = 0;
allPts.clear();
for (int i = 0; i < vertices; i++)
{
Points pts;
pts.clear();
for (int j = 0; j < nPts[i] - 1; j++)
{
Point pt = { points[i][j].x, points[i][j].y };
std::vector<Point>::iterator it = std::find(pts.begin(), pts.end(), pt);
if (it == pts.end())
{
pts.push_back(pt);
}
}
if (pts.size() >= 3)
{
for (int j = 0; j < pts.size(); j++)
{
Point pt = pts[j];
allPts.push_back(pt);
}
polygon.push_back(pts);
}
}
std::vector<uint32_t> vectors = mapbox::earcut(polygon);
std::vector<uint32_t>::iterator itr = vectors.begin();
for (; itr != vectors.end(); itr++)
{
PointLong triPts[16];
uint32_t id = *itr;
triPts[index].x = allPts[id][0];
triPts[index].y = allPts[id][1];
index++;
if (index >= 3)
{
index = 0;
FillTriangle(triPts[0], triPts[1], triPts[2], color);
}
}
}
利用耳切算法三角化复杂多边形以实现复杂多边形填充
其中FillTriangle三角形填充的实现可以参照三角形填充算法(C实现)