public static Polygon2d SplitToTargetLength(Polygon2d poly, double length) { Polygon2d result = new Polygon2d(); result.AppendVertex(poly[0]); for (int j = 0; j < poly.VertexCount; ++j) { int next = (j + 1) % poly.VertexCount; double len = poly[j].Distance(poly[next]); if (len < length) { result.AppendVertex(poly[next]); continue; } int steps = (int)Math.Ceiling(len / length); for (int k = 1; k < steps; ++k) { double t = (double)(k) / (double)steps; Vector2d v = (1.0 - t) * poly[j] + (t) * poly[next]; result.AppendVertex(v); } if (j < poly.VertexCount - 1) { Util.gDevAssert(poly[j].Distance(result.Vertices[result.VertexCount - 1]) > 0.0001); result.AppendVertex(poly[next]); } } return(result); }
public void AddBox(AxisAlignedBox2d box, Style style) { Polygon2d poly = new Polygon2d(); for (int k = 0; k < 4; ++k) { poly.AppendVertex(box.GetCorner(k)); } AddPolygon(poly, style); }
public static void Restore(Polygon2d polygon, BinaryReader reader) { int count = reader.ReadInt32(); for (int i = 0; i < count; ++i) { double x = reader.ReadDouble(); double y = reader.ReadDouble(); polygon.AppendVertex(new Vector2d(x, y)); } }
void compute_polygon() { SpansPoly = new Polygon2d(); for (int i = 0; i < FillSpans.Count; ++i) { foreach (int vid in FillSpans[i].Vertices) { Vector2d v = to2D(Mesh.GetVertex(vid)); SpansPoly.AppendVertex(v); } } Bounds = SpansPoly.Bounds; }
/// <summary> /// 从输入点提取凸包多边形 /// Extract convex hull polygon from input points /// </summary> public Polygon2d GetHullPolygon() { if (mIndices == null) { return(null); } Polygon2d poly = new Polygon2d(); for (int i = 0; i < mIndices.Length; ++i) { poly.AppendVertex(mVertices[mIndices[i]]); } return(poly); }
void compute_polygons() { Bounds = AxisAlignedBox2d.Empty; for (int i = 0; i < Loops.Count; ++i) { EdgeLoop loop = Loops[i].edgeLoop; Polygon2d poly = new Polygon2d(); foreach (int vid in loop.Vertices) { Vector2d v = to2D(Mesh.GetVertex(vid)); poly.AppendVertex(v); } Loops[i].poly = poly; Bounds.Contain(poly.Bounds); } }
/// <summary> /// Convert cells to polygons, with optional filter. /// If filter returns false, polygon is not included in output /// </summary> public List <Polygon2d> CellsToPolygons(Func <Polygon2d, bool> FilterF = null) { var result = new List <Polygon2d>(); for (int i = 0; i < CellLoops.Count; ++i) { int[] loop = CellLoops[i]; var poly = new Polygon2d(); for (int k = 0; k < loop.Length; ++k) { poly.AppendVertex(Graph.GetVertex(loop[k])); } if (FilterF != null && FilterF(poly) == false) { continue; } result.Add(poly); } return(result); }
/// <summary> /// Decompose graph into simple polylines and polygons. /// </summary> public static Curves ExtractCurves(DGraph2 graph) { Curves c = new Curves(); c.Loops = new List <Polygon2d>(); c.Paths = new List <PolyLine2d>(); HashSet <int> used = new HashSet <int>(); // find boundary and junction vertices HashSet <int> boundaries = new HashSet <int>(); HashSet <int> junctions = new HashSet <int>(); foreach (int vid in graph.VertexIndices()) { if (graph.IsBoundaryVertex(vid)) { boundaries.Add(vid); } if (graph.IsJunctionVertex(vid)) { junctions.Add(vid); } } // walk paths from boundary vertices foreach (int start_vid in boundaries) { int vid = start_vid; int eid = graph.GetVtxEdges(vid)[0]; if (used.Contains(eid)) { continue; } PolyLine2d path = new PolyLine2d(); path.AppendVertex(graph.GetVertex(vid)); while (true) { used.Add(eid); Index2i next = NextEdgeAndVtx(eid, vid, graph); eid = next.a; vid = next.b; path.AppendVertex(graph.GetVertex(vid)); if (boundaries.Contains(vid) || junctions.Contains(vid)) { break; // done! } } c.Paths.Add(path); } // ok we should be done w/ boundary verts now... boundaries.Clear(); foreach (int start_vid in junctions) { foreach (int outgoing_eid in graph.VtxEdgesItr(start_vid)) { if (used.Contains(outgoing_eid)) { continue; } int vid = start_vid; int eid = outgoing_eid; PolyLine2d path = new PolyLine2d(); path.AppendVertex(graph.GetVertex(vid)); while (true) { used.Add(eid); Index2i next = NextEdgeAndVtx(eid, vid, graph); eid = next.a; vid = next.b; path.AppendVertex(graph.GetVertex(vid)); if (eid == int.MaxValue || junctions.Contains(vid)) { break; // done! } } c.Paths.Add(path); } } // all that should be left are continuous loops... foreach (int start_eid in graph.EdgeIndices()) { if (used.Contains(start_eid)) { continue; } int eid = start_eid; Index2i ev = graph.GetEdgeV(eid); int vid = ev.a; Polygon2d poly = new Polygon2d(); poly.AppendVertex(graph.GetVertex(vid)); while (true) { used.Add(eid); Index2i next = NextEdgeAndVtx(eid, vid, graph); eid = next.a; vid = next.b; poly.AppendVertex(graph.GetVertex(vid)); if (eid == int.MaxValue || junctions.Contains(vid)) { throw new Exception("how did this happen??"); } if (used.Contains(eid)) { break; } } poly.RemoveVertex(poly.VertexCount - 1); c.Loops.Add(poly); } return(c); }