Skip to content

Instantly share code, notes, and snippets.

@afabri
Created June 7, 2022 13:17
Show Gist options
  • Save afabri/68caff224232b7bc0815b8c4ba948877 to your computer and use it in GitHub Desktop.
Save afabri/68caff224232b7bc0815b8c4ba948877 to your computer and use it in GitHub Desktop.
Join two polygons
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <list>
#include <CGAL/Polygon_2.h>
#include <CGAL/IO/WKT.h>
#include <iostream>
#include <fstream>
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef K::Point_2 Point;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;
typedef std::vector<Polygon_with_holes_2> Pwh_list_2;
int main(int argc, char* argv[])
{
std::ifstream is(argv[1]);
Pwh_list_2 polys;
do {
Polygon_with_holes_2 p;
CGAL::IO::read_polygon_WKT(is, p);
if (!p.outer_boundary().is_empty()) {
assert(p.outer_boundary().is_counterclockwise_oriented());
assert(p.outer_boundary().is_simple());
polys.push_back(p);
}
}while(is.good() && !is.eof());
Pwh_list_2 unionR;
CGAL::join(std::begin(polys), std::end(polys), std::back_inserter(unionR));
CGAL::IO::write_multi_polygon_WKT(std::cout, unionR);
return 0;
}
POLYGON(( 0 0, 1 0, 1 1, 0 1 ))
POLYGON(( 1 1, 2 1, 2 2, 1 2 ))
@afabri
Copy link
Author

afabri commented Jun 7, 2022

The output is a single polygon. We pass two times at (1,1) but is is a pinching point, but not an intersection
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment