GPC多边形初始化

我正在使用GPC Polygon Clipping lib并希望以编程方式创建多边形。 我只看到如何从文件创建一个代码。 如何在代码中进行初始化?

从您的链接中更好地阅读,找到文档页面并阅读; 特别是gpc_add_contour函数可能就是你所需要的。 struct gpc_vertex_list包含一个指向gpc_vertex-s的指针和顶点数,这是你必须填写的内容。

 gpc_polygon p = {0, NULL, NULL}; // "void" polygon gpc_vertex v[] = { {0.0, 0.0}, {10.0, 0.}, {10.0, 10.10}, {0.0, 10.0} }; gpc_vertex_list vl = { 4, v }; //... gpc_add_contour(&p, &vl, 0); 

文档不太清楚,但你可以推断出使用,测试(try-error循环)是你的朋友(无论如何我都不会安装gpc,所以我的代码可能是错误的)。 建议的代码段应该创建一个正方形。 其他gpc_add_countour具有相同&p但不同顶点列表的gpc_add_countour可用于创建更复杂的多边形,当然可以将vl更改为在开头具有更复杂的多边形。 如果要将定义的轮廓设置为当前(p)多边形中的“孔”,则第三个参数应为1。

 gpc_polygon subject; int w = 100, h = 100, verticesCnt = 30; //setup a gpc_polygon container and fill it with random vertices ... subject.num_contours = 1; subject.hole = 0; subject.contour = new gpc_vertex_list; //ie just a single polygon here subject.contour->num_vertices = verticesCnt; subject.contour->vertex = new gpc_vertex [verticesCnt]; for (i = 0; i < verticesCnt; i++){ subject.contour[0].vertex[i].x = random(w); subject.contour[0].vertex[i].y = random(h); } //do stuff with it here, then ... gpc_free_polygon(&subject);