/** * 坐标转换,与clipper库交互必须坐标转换 * @param polygon * @return */ public static Path scaleUp2ClipperCoordinates(NestPath polygon) { Path p = new Path(); foreach (Segment s in polygon.getSegments()) { ClipperCoor cc = CommonUtil.toClipperCoor(s.x, s.y); p.Add(new IntPoint(cc.getX(), cc.getY())); } return(p); }
public static Path NestPath2Path(NestPath nestPath) { Path path = new Path(); foreach (Segment s in nestPath.getSegments()) { ClipperCoor coor = CommonUtil.toClipperCoor(s.getX(), s.getY()); var lp = new IntPoint(coor.getX(), coor.getY()); path.Add(lp); } return(path); }
public static List <NestPath> polygonOffset(NestPath polygon, double offset) { List <NestPath> result = new List <NestPath>(); if (offset == 0 || GeometryUtil.almostEqual(offset, 0)) { /** * return EmptyResult */ return(result); } Path p = new Path(); foreach (Segment s in polygon.getSegments()) { ClipperCoor cc = toClipperCoor(s.getX(), s.getY()); p.Add(new IntPoint(cc.getX(), cc.getY())); } int miterLimit = 2; ClipperOffset co = new ClipperOffset(miterLimit, Config.CURVE_TOLERANCE * Config.CLIIPER_SCALE); co.AddPath(p, JoinType.jtRound, EndType.etClosedPolygon); Paths newpaths = new Paths(); co.Execute(ref newpaths, offset * Config.CLIIPER_SCALE); /** * 这里的length是1的话就是我们想要的 */ for (int i = 0; i < newpaths.Count; i++) { result.Add(CommonUtil.clipperToNestPath(newpaths[i])); } if (offset > 0) { NestPath from = result[0]; if (GeometryUtil.polygonArea(from) > 0) { from.reverse(); } from.add(from.get(0)); from.getSegments().RemoveAt(0); } return(result); }