An algorithm for inflating/deflating (offsetting, buffering) polygons

后端 未结 12 2082
醉话见心
醉话见心 2020-11-22 12:58

How would I "inflate" a polygon? That is, I want to do something similar to this:

\"alt

<
12条回答
  •  自闭症患者
    2020-11-22 13:20

    Big thanks to Angus Johnson for his clipper library. There are good code samples for doing the clipping stuff at the clipper homepage at http://www.angusj.com/delphi/clipper.php#code but I did not see an example for polygon offsetting. So I thought that maybe it is of use for someone if I post my code:

        public static List GetOffsetPolygon(List originalPath, double offset)
        {
            List resultOffsetPath = new List();
    
            List polygon = new List();
            foreach (var point in originalPath)
            {
                polygon.Add(new ClipperLib.IntPoint(point.X, point.Y));
            }
    
            ClipperLib.ClipperOffset co = new ClipperLib.ClipperOffset();
            co.AddPath(polygon, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon);
    
            List> solution = new List>();
            co.Execute(ref solution, offset);
    
            foreach (var offsetPath in solution)
            {
                foreach (var offsetPathPoint in offsetPath)
                {
                    resultOffsetPath.Add(new Point(Convert.ToInt32(offsetPathPoint.X), Convert.ToInt32(offsetPathPoint.Y)));
                }
            }
    
            return resultOffsetPath;
        }
    

提交回复
热议问题