public static GraphicsPath ClipPath(GraphicsPath subjectPath, CombineMode combineMode, GraphicsPath clipPath) { GpcWrapper.Polygon.Validate(combineMode); GpcWrapper.Polygon basePoly = new GpcWrapper.Polygon(subjectPath); GraphicsPath clipClone = (GraphicsPath)clipPath.Clone(); clipClone.CloseAllFigures(); GpcWrapper.Polygon clipPoly = new GpcWrapper.Polygon(clipClone); clipClone.Dispose(); GpcWrapper.Polygon clippedPoly = GpcWrapper.Polygon.Clip(combineMode, basePoly, clipPoly); GraphicsPath returnPath = clippedPoly.ToGraphicsPath(); returnPath.CloseAllFigures(); return returnPath; }
public static Polygon Clip(CombineMode clipMode, Polygon subject_polygon, Polygon clip_polygon) { Validate(clipMode); NativeConstants.gpc_op gpcOp = Convert(clipMode); NativeStructs.gpc_polygon gpc_polygon = new NativeStructs.gpc_polygon(); NativeStructs.gpc_polygon gpc_subject_polygon = PolygonTo_gpc_polygon(subject_polygon); NativeStructs.gpc_polygon gpc_clip_polygon = PolygonTo_gpc_polygon(clip_polygon); NativeMethods.gpc_polygon_clip(gpcOp, ref gpc_subject_polygon, ref gpc_clip_polygon, ref gpc_polygon); Polygon polygon = gpc_polygon_ToPolygon(gpc_polygon); Free_gpc_polygon(gpc_subject_polygon); Free_gpc_polygon(gpc_clip_polygon); NativeMethods.gpc_free_polygon(ref gpc_polygon); return polygon; }
private static NativeStructs.gpc_polygon PolygonTo_gpc_polygon(Polygon polygon) { NativeStructs.gpc_polygon gpc_pol = new NativeStructs.gpc_polygon(); gpc_pol.num_contours = polygon.NofContours; int[] hole = new int[polygon.NofContours]; for (int i = 0; i < polygon.NofContours; i++) { hole[i] = (polygon.ContourIsHole[i] ? 1 : 0); } gpc_pol.hole = Marshal.AllocCoTaskMem(polygon.NofContours * Marshal.SizeOf(typeof(int) /*hole[0]*/)); if (polygon.NofContours > 0) { Marshal.Copy(hole, 0, gpc_pol.hole, polygon.NofContours); } gpc_pol.contour = Marshal.AllocCoTaskMem(polygon.NofContours * Marshal.SizeOf(typeof(NativeStructs.gpc_vertex_list))); IntPtr ptr = gpc_pol.contour; for (int i = 0; i < polygon.NofContours; i++) { NativeStructs.gpc_vertex_list gpc_vtx_list = new NativeStructs.gpc_vertex_list(); gpc_vtx_list.num_vertices = polygon.Contour[i].NofVertices; gpc_vtx_list.vertex = Marshal.AllocCoTaskMem(polygon.Contour[i].NofVertices * Marshal.SizeOf(typeof(NativeStructs.gpc_vertex))); IntPtr ptr2 = gpc_vtx_list.vertex; for (int j = 0; j < polygon.Contour[i].NofVertices; j++) { NativeStructs.gpc_vertex gpc_vtx = new NativeStructs.gpc_vertex(); gpc_vtx.x = polygon.Contour[i].Vertex[j].X; gpc_vtx.y = polygon.Contour[i].Vertex[j].Y; Marshal.StructureToPtr(gpc_vtx, ptr2, false); ptr2 = (IntPtr)(((int)ptr2) + Marshal.SizeOf(gpc_vtx)); } Marshal.StructureToPtr(gpc_vtx_list, ptr, false); ptr = (IntPtr)(((int)ptr) + Marshal.SizeOf(gpc_vtx_list)); } return gpc_pol; }
private static Polygon gpc_polygon_ToPolygon(NativeStructs.gpc_polygon gpc_polygon) { Polygon polygon = new Polygon(); polygon.NofContours = gpc_polygon.num_contours; polygon.ContourIsHole = new bool[polygon.NofContours]; polygon.Contour = new VertexList[polygon.NofContours]; short[] holeShort = new short[polygon.NofContours]; IntPtr ptr = gpc_polygon.hole; if (polygon.NofContours > 0) { Marshal.Copy(gpc_polygon.hole, holeShort, 0, polygon.NofContours); } for (int i = 0; i < polygon.NofContours; i++) polygon.ContourIsHole[i] = (holeShort[i] != 0); ptr = gpc_polygon.contour; for (int i = 0; i < polygon.NofContours; i++) { NativeStructs.gpc_vertex_list gpc_vtx_list = (NativeStructs.gpc_vertex_list)Marshal.PtrToStructure(ptr, typeof(NativeStructs.gpc_vertex_list)); polygon.Contour[i] = new VertexList(); polygon.Contour[i].NofVertices = gpc_vtx_list.num_vertices; polygon.Contour[i].Vertex = new Vertex[polygon.Contour[i].NofVertices]; IntPtr ptr2 = gpc_vtx_list.vertex; for (int j = 0; j < polygon.Contour[i].NofVertices; j++) { NativeStructs.gpc_vertex gpc_vtx = (NativeStructs.gpc_vertex)Marshal.PtrToStructure(ptr2, typeof(NativeStructs.gpc_vertex)); polygon.Contour[i].Vertex[j].X = gpc_vtx.x; polygon.Contour[i].Vertex[j].Y = gpc_vtx.y; ptr2 = (IntPtr)(((int)ptr2) + Marshal.SizeOf(gpc_vtx)); } ptr = (IntPtr)(((int)ptr) + Marshal.SizeOf(gpc_vtx_list)); } return polygon; }
public Polygon Clip(CombineMode operation, Polygon polygon) { return Clip(operation, this, polygon); }