我可以推荐使用JTS。只需创建要组合的几何图形并使用联合方法。之后,您可以使用新的几何图形并使用以前的代码对其进行绘制。联合的简单代码示例:
// build polygon p1
Polygon p1 = new GeometryFactory().createPolygon(new Coordinate[]{new Coordinate(0,0), new Coordinate(0,10), new Coordinate(10,10), new Coordinate(10,0), new Coordinate(0,0)});
// build polygon p2
Polygon p2 = new GeometryFactory().createPolygon(new Coordinate[]{new Coordinate(0,0), new Coordinate(0,30), new Coordinate(5,30), new Coordinate(5,0), new Coordinate(0,0)});
// calculate polygon3 as the union of p1 and p2
Polygon p3 = (Polygon) p1.union(p2);
// print simple WKT
System.out.println(p3.toText());
这种情况下的输出是(如预期的那样):
POLYGON ((0 0, 0 10, 0 30, 5 30, 5 10, 10 10, 10 0, 5 0, 0 0))