Beispiel #1
0
    /////////////////////////

    //polygon union
    static public List <Vector2> PolygonUnion(List <Vector2> _polygon1, Vector2[] _polygon)
    {
        List <Vector2> _polygon2 = new List <Vector2> (_polygon);

        if (_polygon1 == null)
        {
            return(_polygon2);
        }
        if (_polygon1.Count == 0)
        {
            return(_polygon2);
        }

        // List<Vector2> result = new List<Vector2>();

        // _polygon1.AddRange(polygon2);
        // return _polygon1;
        // return result;
        EPPZ.Geometry.Model.Polygon polygon1 = EPPZ.Geometry.Model.Polygon.PolygonWithPointList(_polygon1);
        EPPZ.Geometry.Model.Polygon polygon2 = EPPZ.Geometry.Model.Polygon.PolygonWithPointList(_polygon2);
        polygon1.AddPolygon(polygon2);
        EPPZ.Geometry.Model.Polygon polygon_union = polygon1.UnionPolygon();

        return(new List <Vector2> (polygon_union.points));
    }
        public static Polygon PolygonWithSource(InfluenceCircle polygonSource)
        {
            Transform          cTr            = polygonSource.transform;
            List <BorderPoint> borderPoints_1 = polygonSource.borderPoints_subPolygons[0];
            Polygon            rootPolygon    = Polygon.PolygonWithVectorPoints(
                /*polygonSource.*/ borderPoints_1.Select(v => cTr.InverseTransformPoint(v.pointA)).ToArray());

            for (int i = 1; i < polygonSource.borderPoints_subPolygons.Count; i++)
            {
                List <BorderPoint> borderPoints_N = polygonSource.borderPoints_subPolygons[i];
                Polygon            subPolygon     = PolygonWithVectorPoints(
                    borderPoints_N
                    .Select(v => cTr.InverseTransformPoint(v.pointA))
                    .ToArray()
                    );
                rootPolygon.AddPolygon(subPolygon);
            }

            // Collect sub-polygons if any.

/*	        foreach( Transform eachChildTransform in polygonSource.gameObject.transform )
 *              {
 *                  GameObject eachChildGameObject = eachChildTransform.gameObject;
 *                  Source.Polygon eachChildPolygonSource = eachChildGameObject.GetComponent<Source.Polygon>();
 *                  if( eachChildPolygonSource != null )
 *                  {
 *                      Polygon eachSubPolygon = Polygon.PolygonWithSource(eachChildPolygonSource);
 *                      rootPolygon.AddPolygon( eachSubPolygon );
 *                  }
 *              }*/

            return(rootPolygon);
        }
        public static Polygon PolygonWithSource(Source.Polygon polygonSource)
        {
            Polygon rootPolygon = Polygon.PolygonWithPointTransforms(polygonSource.points, polygonSource.coordinates);

            // Collect sub-polygons if any.
            foreach (Transform eachChildTransform in polygonSource.gameObject.transform)
            {
                GameObject     eachChildGameObject    = eachChildTransform.gameObject;
                Source.Polygon eachChildPolygonSource = eachChildGameObject.GetComponent <Source.Polygon>();
                if (eachChildPolygonSource != null)
                {
                    Polygon eachSubPolygon = Polygon.PolygonWithSource(eachChildPolygonSource);
                    rootPolygon.AddPolygon(eachSubPolygon);
                }
            }

            return(rootPolygon);
        }
        public static Polygon PolygonWithPolygon(Polygon polygon)
        {
            Polygon rootPolygon = null;

            // Compose with sub-olygons (if any).
            polygon.EnumeratePolygons((Polygon eachPolygon) =>
            {
                if (rootPolygon == null)
                {
                    rootPolygon = Polygon.PolygonWithPoints(eachPolygon.points);
                }
                else
                {
                    rootPolygon.AddPolygon(Polygon.PolygonWithPoints(eachPolygon.points));
                }
            });

            return(rootPolygon);
        }
        public Polygon UnionPolygon()
        {
            // Calculate Polygon-Clipper scale.
            float maximum      = Mathf.Max(bounds.width, bounds.height);
            float maximumScale = (float)Int32.MaxValue / maximum;
            float scale        = Mathf.Min(clipperScale, maximumScale);

            // Convert to Clipper.
            Paths subjectPaths = new Paths();
            Paths clipPaths    = new Paths();

            {
                Path path = new Path();
                EnumeratePoints((Vector2 eachPoint) =>
                {
                    path.Add(new IntPoint(eachPoint.x * scale, eachPoint.y * scale));
                });
                subjectPaths.Add(path);
            }
            foreach (Polygon eachPolygon in polygons)
            {
                Path path = new Path();
                eachPolygon.EnumeratePoints((Vector2 eachPoint) =>
                {
                    path.Add(new IntPoint(eachPoint.x * scale, eachPoint.y * scale));
                });
                clipPaths.Add(path);
            }

            // Clipper union.
            Paths   unionPaths = new Paths();
            Clipper clipper    = new Clipper();

            clipper.AddPaths(subjectPaths, PolyType.ptSubject, true);
            clipper.AddPaths(clipPaths, PolyType.ptClip, true);
            clipper.Execute(ClipType.ctUnion, unionPaths);

            // Remove self intersections.
            Paths simplifiedUnionPaths = new Paths();

            simplifiedUnionPaths = Clipper.SimplifyPolygons(unionPaths);

            // Convert from Cipper.
            Polygon simplifiedUnionPolygon = null;

            for (int index = 0; index < simplifiedUnionPaths.Count; index++)
            {
                Path    eachSolutionPath    = simplifiedUnionPaths[index];
                Polygon eachSolutionPolygon = PolygonFromClipperPath(eachSolutionPath, scale);

                if (index == 0)
                {
                    simplifiedUnionPolygon = Polygon.PolygonWithPoints(eachSolutionPolygon.points);                     // Copy
                }
                else
                {
                    simplifiedUnionPolygon.AddPolygon(eachSolutionPolygon);
                }
            }

            // Back to Polygon.
            return(simplifiedUnionPolygon);
        }
        Polygon OffsetPolygon(float offset, bool simplify, bool rounded)
        {
            // Calculate Polygon-Clipper scale.
            float maximum      = Mathf.Max(bounds.width, bounds.height) + offset * 2.0f + offset;
            float maximumScale = (float)Int32.MaxValue / maximum;
            float scale        = Mathf.Min(clipperScale, maximumScale);


            // Convert to Clipper.
            Paths paths = new Paths();

            {
                Path path = new Path();
                EnumeratePoints((Vector2 eachPoint) =>
                {
                    path.Add(new IntPoint(eachPoint.x * scale, eachPoint.y * scale));
                });
                paths.Add(path);
            }
            foreach (Polygon eachPolygon in polygons)
            {
                Path path = new Path();
                eachPolygon.EnumeratePoints((Vector2 eachPoint) =>
                {
                    path.Add(new IntPoint(eachPoint.x * scale, eachPoint.y * scale));
                });
                paths.Add(path);
            }

            // Mode.
            JoinType joinType = (rounded) ? JoinType.jtRound : JoinType.jtMiter;

            // Clipper offset.
            Paths         offsetPaths   = new Paths();
            ClipperOffset clipperOffset = new ClipperOffset();

            if (rounded)
            {
                clipperOffset.ArcTolerance = 0.25 * clipperArcTolerance;
            }                                                                                     // "The default ArcTolerance is 0.25 units." from http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Classes/ClipperOffset/Properties/ArcTolerance.htm
            clipperOffset.AddPaths(paths, joinType, EndType.etClosedPolygon);
            clipperOffset.Execute(ref offsetPaths, (double)offset * scale);

            // Remove self intersections (if requested).
            if (simplify)
            {
                offsetPaths = Clipper.SimplifyPolygons(offsetPaths);
            }

            // Convert from Clipper.
            Polygon offsetPolygon = null;

            for (int index = 0; index < offsetPaths.Count; index++)
            {
                Path    eachSolutionPath    = offsetPaths[index];
                Polygon eachSolutionPolygon = PolygonFromClipperPath(eachSolutionPath, scale);

                if (index == 0)
                {
                    offsetPolygon = Polygon.PolygonWithPoints(eachSolutionPolygon.points);                     // Copy
                }
                else
                {
                    offsetPolygon.AddPolygon(eachSolutionPolygon);
                }
            }

            // Back to Polygon.
            return(offsetPolygon);
        }