void cairo_to_clipper(cairo_t* cr, Polygons &pg, int scaling_factor, Transform transform) { if (scaling_factor > 8 || scaling_factor < 0) throw clipperCairoException("cairo_to_clipper: invalid scaling factor"); double scaling = std::pow((double)10, scaling_factor); pg.clear(); cairo_path_t *path = cairo_copy_path_flat(cr); int poly_count = 0; for (int i = 0; i < path->num_data; i += path->data[i].header.length) { if( path->data[i].header.type == CAIRO_PATH_CLOSE_PATH) poly_count++; } pg.resize(poly_count); int i = 0, pc = 0; while (pc < poly_count) { int vert_count = 1; int j = i; while(j < path->num_data && path->data[j].header.type != CAIRO_PATH_CLOSE_PATH) { if (path->data[j].header.type == CAIRO_PATH_LINE_TO) vert_count++; j += path->data[j].header.length; } pg[pc].resize(vert_count); if (path->data[i].header.type != CAIRO_PATH_MOVE_TO) { pg.resize(pc); break; } pg[pc][0].X = Round(path->data[i+1].point.x *scaling); pg[pc][0].Y = Round(path->data[i+1].point.y *scaling); if (transform != tNone) transform_point(cr, transform, &pg[pc][0].X, &pg[pc][0].Y); i += path->data[i].header.length; j = 1; while (j < vert_count && i < path->num_data && path->data[i].header.type == CAIRO_PATH_LINE_TO) { pg[pc][j].X = Round(path->data[i+1].point.x *scaling); pg[pc][j].Y = Round(path->data[i+1].point.y *scaling); if (transform != tNone) transform_point(cr, transform, &pg[pc][j].X, &pg[pc][j].Y); j++; i += path->data[i].header.length; } pc++; i += path->data[i].header.length; } cairo_path_destroy(path); }
void UpdatePolygons(bool updateSolutionOnly) { if (VertCount < 0) VertCount = -VertCount; if (VertCount > 50) VertCount = 50; if (VertCount < 3) VertCount = 3; Clipper c; if (!updateSolutionOnly) { delta = 0.0; RECT r; GetWindowRect(hStatus, &r); int statusHeight = r.bottom - r.top; GetClientRect(hWnd, &r); sub.resize(1); clp.resize(1); //int ints[] = {0,0,0, -100,100, -100,100, 0}; //int ints2[] = {0, 100, 100, -200,0, -200,100, 100}; //MakePolygonFromInts(ints, 8, sub[0]); //TranslatePolygon(sub[0], 100,220); //MakePolygonFromInts(ints2, 8, clp[0]); //TranslatePolygon(sub[1], 100,220); MakeRandomPoly(sub[0], r.right, r.bottom - statusHeight, VertCount); MakeRandomPoly(clp[0], r.right, r.bottom - statusHeight, VertCount); //SaveToFile("subj.txt", sub); //SaveToFile("clip.txt", clp); } c.AddPolygons(sub, ptSubject); c.AddPolygons(clp, ptClip); c.Execute(ct, sol, pft, pft); if (delta != 0.0) OffsetPolygons(sol,sol,delta,jt); InvalidateRect(hWnd, NULL, false); }