连接简单的多边形并在 Eyeshot 中绘制生成的轮廓

Join simple polygons and draw the resulting contours in Eyeshot

连接多边形集合并使用 Eyeshot 绘制生成的轮廓的最简单方法是什么?到目前为止,我只能得到填充区域,但我对轮廓的合并很感兴趣。

我想到了这个解决方案。要显示多边形,只需遍历连接的区域,遍历 ContourList 并创建 LinearPaths

private List<PolyRegion2D> Joiner(IEnumerable<Polygon2D> polygons) {
        // The resulting polygons are unconnected
        List<PolyRegion2D> res = new List<PolyRegion2D>();

        // Put every polygon in a region to do the unions.
        LinkedList<PolyRegion2D> polygonRegions = new LinkedList<PolyRegion2D>();
        foreach (Polygon2D polygon in polygons) {
            polygonRegions.AddLast(new PolyRegion2D(new Polygon2D[]{polygon}));
        }

        while (polygonRegions.Count > 0) {
            PolyRegion2D first = polygonRegions.First.Value;
            polygonRegions.RemoveFirst();
            PolyRegion2D union;
            LinkedListNode<PolyRegion2D> connected = FindConnected(first, polygonRegions, out union);
            if (connected == null) {
                // Unconnected polygon
                res.Add(first);
            } else {
                // Intersection found
                polygonRegions.Remove(connected);
                polygonRegions.AddFirst(union);
            }
        }

        return res;
    }

    private LinkedListNode<PolyRegion2D> FindConnected(PolyRegion2D poly, LinkedList<PolyRegion2D> polys, out PolyRegion2D union) {
        LinkedListNode<PolyRegion2D> node = polys.First;
        while(node != null){
            PolyRegion2D[] union_ = PolyRegion2D.Union(poly, node.Value);
            if (union_.Length == 1) {
                union = union_[0];
                return node;
            }
            node = node.Next;
        }
        union = null;
        return null;
    }

因为你有这个区域,所以很容易从中得到轮廓。

// if you know the region is a simple region (not containing full circles) get the curves
List<ICurve> curves = (region.ContourList.FirstOrDefault() as CompositeCurve).CurveList;

据我所知,ICurve 只是直线和圆弧。所以你可以测试:

bool isLine = curves[0] is Line
bool isArc = curves[0] is Arc

列表中的所有曲线都是有序的,因此您可以轻松地改造区域。此外,如果 region.ContourList 包含超过 1 个轮廓,则表示您所在的区域有漏洞。第一个元素将始终是主要轮廓,并且所有后续元素也是轮廓但有孔。

轮廓曲线逆时针给出,内部顺时针给出