GPC 多边形初始化

发布于 2024-09-04 13:27:05 字数 155 浏览 8 评论 0原文

我正在使用 GPC 多边形裁剪库 并想要创建一个以编程方式多边形。我只看到如何从文件创建一个的代码。如何在我的代码中进行初始化?

I am using the GPC Polygon Clipping lib and want to create a polygon programatically. I only see code for how to create one from a file. How can I do the initialization in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偷得浮生 2024-09-11 13:27:05

从您的链接中更好地阅读,找到 doc翻页并阅读;特别是 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);

该文档不太清楚,但您可以推断出用途,并且测试(尝试错误循环)是您的朋友(无论如何我都不会安装 gpc 来做到这一点,所以我的代码可能是错误的)。建议的代码片段应该创建一个正方形。其他几个具有相同&p但不同顶点列表的gpc_add_countour可用于创建更复杂的多边形,当然vl也可以更改为在开始时具有更复杂的多边形。如果您希望定义的轮廓是当前 (p) 多边形中的“洞”,则第三个参数应为 1。

Read better from your link, find the doc page and read; in particular gpc_add_contour function is likely what you need. The struct gpc_vertex_list holds a pointer to gpc_vertex-s and the number of vertex, and is what you must fill in. Like


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);

The doc is not too much clear, but you can deduce the use, and testing (try-error loops) is your friend (I won't install gpc to do it anyway, so my code could be wrong). The proposed code snippet should create a square. Several other gpc_add_countour with the same &p but different vertex list can be used to create a more complex polygon, and of course vl can be changed to have at the beginning a more complex polygon. The third parameter should be 1 if you want the defined contour to be a "hole" in the current (p) polygon.

情深缘浅 2024-09-11 13:27:05
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);
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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文