public static Shape ToShape(this PolygonGroup polygonGroup) { var shape = new Shape(); foreach (var polygon in polygonGroup.Children.Cast<Polygon>()) { var polyPoints = polygon.Children.Cast<PolyPoint>().ToList(); var vertices = new List<Vector2>(polyPoints.Count); vertices.AddRange(polyPoints.Select(point => ConvertUnits.ToSimUnits(new Vector2(point.X, point.Y)))); shape.Vertices.Add(vertices); } return shape; }
private static Shape ScaleConvertAndPartition(StringBuilder spool, bool strict, Image image, IEnumerable<Vertices> polygons, TriangulationAlgorithm algorithm) { var scale = ConvertUnits.ToSimUnits(1, 1); var width = ConvertUnits.ToSimUnits(image.Width); var height = ConvertUnits.ToSimUnits(image.Height); var translation = new Vector2(-width, -height)*0.5f; var final = new List<List<Vector2>>(); foreach (var polygon in polygons) { polygon.Scale(scale); polygon.Translate(translation); var thisPolygon = SimplifyTools.CollinearSimplify(polygon); if (strict) { var errors = thisPolygon.CheckPolygon(); if (errors != PolygonError.NoError) { spool.AppendFormat("Invalid shape ({0})", errors); return null; } } try { var partition = Triangulate.ConvexPartition(thisPolygon, algorithm); var vertices = partition.Select(verts => verts.Select(v => new Vector2(v.X, v.Y)).ToList()); final.AddRange(vertices); } catch { spool.AppendFormat("Cannot triangulate polygon"); spool.AppendLine(); } } var shape = new Shape { Vertices = final }; return shape; }