Skip to content

Instantly share code, notes, and snippets.

@scemino
Created August 19, 2022 12:31
Show Gist options
  • Save scemino/89fd2b3cddc95b319d6ec07cf0aa8159 to your computer and use it in GitHub Desktop.
Save scemino/89fd2b3cddc95b319d6ec07cf0aa8159 to your computer and use it in GitHub Desktop.
Clipper1/Clipper2 merge polygons
(750,120), (737,125), (695,132), (639,136), (621,142), (616,147), (625,151), (636,153), (706,164), (699,170), (679,177), (666,181), (663,184), (682,190), (721,195), (747,196), (766,203), (761,203), (747,198), (720,197), (679,192), (658,185), (664,178), (675,175), (696,168), (699,164), (633,154), (620,152), (609,148), (613,143), (638,134), (693,130), (731,121), (730,114), (532,94), (506,97), (494,102), (416,102), (412,109), (331,109), (329,104), (186,104), (190,112), (197,113), (198,116), (173,113), (91,113), (19,104), (25,90), (8,86), (7,70), (660,66), (554,90), (732,109), (746,114)
number solutions: 1
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string_view>
#include "clipper.hpp"
static ClipperLib::Path parsePolygon(std::string_view text) {
const char *p = text.data();
size_t size = text.size();
char *p2;
ClipperLib::Path path;
do {
long int x = std::strtol(p + 1, &p2, 10);
long int y = std::strtol(p2 + 1, &p2, 10);
path.push_back(ClipperLib::IntPoint(x, y));
p = p2 + 2;
} while ((p - text.data()) < size);
return path;
}
int main(int argc, char** argv) {
const char* wbTexts[] = {
"{198,116};{197,113};{190,112};{186,104};{173,113}",
"{91,113};{173,113};{186,104};{329,104};{331,109};{412,109};{416,102};{494,102};{506,97};{532,94};{554,90};{660,66};{7,70};{8,86};{25,90};{19,104}",
"{730,114};{732,109};{554,90};{532,94}",
"{730,114};{731,121};{737,125};{750,120};{746,114};{732,109}",
"{693,130};{695,132};{737,125};{731,121}",
"{639,136};{695,132};{693,130};{638,134}",
"{609,148};{616,147};{621,142};{639,136};{638,134};{613,143}",
"{699,164};{706,164};{636,153};{625,151};{616,147};{609,148};{620,152};{633,154}",
"{658,185};{663,184};{666,181};{679,177};{699,170};{706,164};{699,164};{696,168};{675,175};{664,178}",
"{747,198};{761,203};{766,203};{747,196};{721,195};{682,190};{663,184};{658,185};{679,192};{720,197}"};
ClipperLib::Paths subjects, clips, solutions;
for (int i = 0; i < 10; ++i) {
subjects.push_back(parsePolygon(wbTexts[i]));
}
ClipperLib::Clipper clipper;
clipper.AddPaths(subjects, ClipperLib::ptSubject, true);
clipper.AddPaths(clips, ClipperLib::ptClip, true);
clipper.Execute(ClipperLib::ctUnion, solutions, ClipperLib::pftEvenOdd);
std::cout << solutions << std::endl;
std::cout << "number solutions: " << solutions.size() << std::endl;
return EXIT_SUCCESS;
}
682,190 , 721,195 , 747,196 , 766,203 , 761,203 , 747,198 , 720,197 , 679,192 , 658,185 , 663,184
706,164 , 699,170 , 679,177 , 666,181 , 663,184 , 658,185 , 664,178 , 675,175 , 696,168 , 699,164 , 633,154 , 620,152 , 609,148 , 616,147 , 625,151 , 636,153
506,97 , 494,102 , 416,102 , 412,109 , 331,109 , 329,104 , 186,104 , 190,112 , 197,113 , 198,116 , 173,113 , 91,113 , 19,104 , 25,90 , 8,86 , 7,70 , 660,66 , 554,90 , 732,109 , 746,114 , 750,120 , 737,125 , 695,132 , 639,136 , 621,142 , 616,147 , 609,148 , 613,143 , 638,134 , 693,130 , 731,121 , 730,114 , 532,94
number solutions: 3
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string_view>
#include "clipper.h"
static Clipper2Lib::Path64 parsePolygon(std::string_view text) {
const char *p = text.data();
size_t size = text.size();
char *p2;
Clipper2Lib::Path64 path;
do {
long int x = std::strtol(p + 1, &p2, 10);
long int y = std::strtol(p2 + 1, &p2, 10);
path.push_back(Clipper2Lib::Point64(x, y));
p = p2 + 2;
} while ((p - text.data()) < size);
return path;
}
int main(int argc, char** argv) {
const char* wbTexts[] = {
"{198,116};{197,113};{190,112};{186,104};{173,113}",
"{91,113};{173,113};{186,104};{329,104};{331,109};{412,109};{416,102};{494,102};{506,97};{532,94};{554,90};{660,66};{7,70};{8,86};{25,90};{19,104}",
"{730,114};{732,109};{554,90};{532,94}",
"{730,114};{731,121};{737,125};{750,120};{746,114};{732,109}",
"{693,130};{695,132};{737,125};{731,121}",
"{639,136};{695,132};{693,130};{638,134}",
"{609,148};{616,147};{621,142};{639,136};{638,134};{613,143}",
"{699,164};{706,164};{636,153};{625,151};{616,147};{609,148};{620,152};{633,154}",
"{658,185};{663,184};{666,181};{679,177};{699,170};{706,164};{699,164};{696,168};{675,175};{664,178}",
"{747,198};{761,203};{766,203};{747,196};{721,195};{682,190};{663,184};{658,185};{679,192};{720,197}"};
Clipper2Lib::Paths64 subjects, clips, solutions;
for (int i = 0; i < 10; ++i) {
subjects.push_back(parsePolygon(wbTexts[i]));
}
Clipper2Lib::Clipper64 clipper;
clipper.AddSubject(subjects);
clipper.AddClip(clips);
clipper.Execute(Clipper2Lib::ClipType::Union, Clipper2Lib::FillRule::EvenOdd, solutions);
std::cout << solutions << std::endl;
std::cout << "number solutions: " << solutions.size() << std::endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment