3
votes

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?

2

2 Answers

0
votes

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.

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