//"1,1;2,2;3,3" public static void Main(string[] args) { //args = new string[3]; //string ss = "0,0;0,1;1,1;1,0"; //string ss1 = "0,0;0,1;1,1;1,0"; //string ss2 = "0"; //args[0] = ss; //args[1] = ss1; //args[2] = ss2; if (args?.Count() != 3) { return; } try { string[] ptStr1 = args[0].Split(';'); string[] ptStr2 = args[1].Split(';'); //0:相差 1:相交 2:异或 3:合并 GpcOperation operation = (GpcOperation)(int.Parse(args[2])); //组织多边形A顶点数据 GpcWrapper.Polygon polygonA = new GpcWrapper.Polygon(); PointF[] aa = new PointF[ptStr1.Count()]; for (int i = 0; i < ptStr1.Count(); i++) { var subPtStr = ptStr1[i].Split(','); aa[i] = new PointF(float.Parse(subPtStr[0]), float.Parse(subPtStr[1])); } VertexList vtxA = new VertexList(aa); polygonA.AddContour(vtxA, false); //组织多边形B顶点数据 GpcWrapper.Polygon polygonB = new GpcWrapper.Polygon(); PointF[] bb = new PointF[ptStr2.Count()]; for (int i = 0; i < ptStr2.Count(); i++) { var subPtStr = ptStr2[i].Split(','); bb[i] = new PointF(float.Parse(subPtStr[0]), float.Parse(subPtStr[1])); } VertexList vtxB = new VertexList(bb); polygonB.AddContour(vtxB, false); //调用GPC库的剪辑算法 GpcWrapper.Polygon result = polygonA.Clip(operation, polygonB); //组织返回结果字符串 if (result.Contour == null || result.Contour.Count() == 0) { Console.WriteLine(""); return; } string str = string.Join(";", result.Contour[0].Vertex); str = str.Replace("(", string.Empty).Replace(")", string.Empty); Console.WriteLine(str); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static Tristrip ClipToTristrip( GpcOperation operation, Polygon subject_polygon, Polygon clip_polygon ) { gpc_tristrip gpc_strip = new gpc_tristrip(); gpc_polygon gpc_subject_polygon = GpcWrapper.PolygonTo_gpc_polygon( subject_polygon ); gpc_polygon gpc_clip_polygon = GpcWrapper.PolygonTo_gpc_polygon( clip_polygon ); gpc_tristrip_clip( operation, ref gpc_subject_polygon, ref gpc_clip_polygon, ref gpc_strip ); Tristrip tristrip = GpcWrapper.gpc_strip_ToTristrip( gpc_strip ); GpcWrapper.Free_gpc_polygon( gpc_subject_polygon ); GpcWrapper.Free_gpc_polygon( gpc_clip_polygon ); GpcWrapper.gpc_free_tristrip( ref gpc_strip ); return tristrip; }
public static Polygon Clip( GpcOperation operation, Polygon subject_polygon, Polygon clip_polygon ) { gpc_polygon gpc_polygon = new gpc_polygon(); gpc_polygon gpc_subject_polygon = GpcWrapper.PolygonTo_gpc_polygon( subject_polygon ); gpc_polygon gpc_clip_polygon = GpcWrapper.PolygonTo_gpc_polygon( clip_polygon ); gpc_polygon_clip( operation, ref gpc_subject_polygon, ref gpc_clip_polygon, ref gpc_polygon ); Polygon polygon = GpcWrapper.gpc_polygon_ToPolygon( gpc_polygon ); GpcWrapper.Free_gpc_polygon( gpc_subject_polygon ); GpcWrapper.Free_gpc_polygon( gpc_clip_polygon ); GpcWrapper.gpc_free_polygon( ref gpc_polygon ); return polygon; }
public static Tristrip PolygonToTristrip( Polygon polygon ) { gpc_tristrip gpc_strip = new gpc_tristrip(); gpc_polygon gpc_pol = GpcWrapper.PolygonTo_gpc_polygon( polygon ); gpc_polygon_to_tristrip( ref gpc_pol, ref gpc_strip ); Tristrip tristrip = GpcWrapper.gpc_strip_ToTristrip( gpc_strip ); GpcWrapper.Free_gpc_polygon( gpc_pol ); GpcWrapper.gpc_free_tristrip( ref gpc_strip ); return tristrip; }
public Tristrip ClipToTristrip( GpcOperation operation, Polygon polygon ) { return GpcWrapper.ClipToTristrip( operation, this, polygon ); }
private static gpc_polygon PolygonTo_gpc_polygon( Polygon polygon ) { gpc_polygon gpc_pol = new 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(hole[0]) ); Marshal.Copy( hole, 0, gpc_pol.hole, polygon.NofContours ); gpc_pol.contour = Marshal.AllocCoTaskMem( polygon.NofContours * Marshal.SizeOf( new gpc_vertex_list() ) ); IntPtr ptr = gpc_pol.contour; for ( int i=0 ; i<polygon.NofContours ; i++ ) { gpc_vertex_list gpc_vtx_list = new gpc_vertex_list(); gpc_vtx_list.num_vertices = polygon.Contour[i].NofVertices; gpc_vtx_list.vertex = Marshal.AllocCoTaskMem( polygon.Contour[i].NofVertices * Marshal.SizeOf(new gpc_vertex()) ); IntPtr ptr2 = gpc_vtx_list.vertex; for ( int j=0 ; j<polygon.Contour[i].NofVertices ; j++ ) { gpc_vertex gpc_vtx = new 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; }
public Polygon Clip( GpcOperation operation, Polygon polygon ) { return GpcWrapper.Clip( operation, this, polygon ); }
private static Polygon gpc_polygon_ToPolygon( 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; 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++ ) { gpc_vertex_list gpc_vtx_list = (gpc_vertex_list)Marshal.PtrToStructure( ptr, typeof(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++ ) { gpc_vertex gpc_vtx = (gpc_vertex)Marshal.PtrToStructure( ptr2, typeof(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 static void SavePolygon( string filename, bool writeHoleFlags, Polygon polygon ) { gpc_polygon gpc_polygon = GpcWrapper.PolygonTo_gpc_polygon( polygon ); IntPtr fp = fopen( filename, "wb" ); gpc_write_polygon( fp, writeHoleFlags?((int)1):((int)0), ref gpc_polygon ); fclose( fp ); GpcWrapper.Free_gpc_polygon( gpc_polygon ); }
public static Tristrip ClipToTristrip(GpcOperation operation, Polygon subject_polygon, Polygon clip_polygon) { gpc_tristrip gpc_strip = new gpc_tristrip(); gpc_polygon gpc_subject_polygon = GpcWrapper.PolygonTo_gpc_polygon(subject_polygon); gpc_polygon gpc_clip_polygon = GpcWrapper.PolygonTo_gpc_polygon(clip_polygon); gpc_tristrip_clip(operation, ref gpc_subject_polygon, ref gpc_clip_polygon, ref gpc_strip); Tristrip tristrip = GpcWrapper.gpc_strip_ToTristrip(gpc_strip); GpcWrapper.Free_gpc_polygon(gpc_subject_polygon); GpcWrapper.Free_gpc_polygon(gpc_clip_polygon); GpcWrapper.gpc_free_tristrip(ref gpc_strip); return(tristrip); }
public Polygon Clip(GpcOperation operation, Polygon polygon) { return(GpcWrapper.Clip(operation, this, polygon)); }
public Tristrip ClipToTristrip(GpcOperation operation, Polygon polygon) { return(GpcWrapper.ClipToTristrip(operation, this, polygon)); }