private void DrawMainPolygon(Canvas canvas, List <Point> points) { if (points.Count < 3) { return; } var path = new Path(); var pathSegmentCollection = new PathSegmentCollection(); var pathFigure = new PathFigure(); List <Point> processedPoints = JarvisAlgorithm.ConvexHull(points); for (int i = 0; i < processedPoints.Count; i++) { processedPoints[i] = new Point(processedPoints[i].X + 100, processedPoints[i].Y + 100); } pathFigure.StartPoint = PointMapper.fromMyPointToSystemPoint(processedPoints.First()); var beizerSegments = InterpolationUtils.InterpolatePointWithBeizerCurves(processedPoints, true); foreach (var beizerCurveSegment in beizerSegments) { var segment = new BezierSegment { Point1 = PointMapper.fromMyPointToSystemPoint(beizerCurveSegment.FirstControlPoint), Point2 = PointMapper.fromMyPointToSystemPoint(beizerCurveSegment.SecondControlPoint), Point3 = PointMapper.fromMyPointToSystemPoint(beizerCurveSegment.EndPoint) }; pathSegmentCollection.Add(segment); } pathFigure.Segments = pathSegmentCollection; var pathFigureCollection = new PathFigureCollection { pathFigure }; var pathGeometry = new PathGeometry { Figures = pathFigureCollection }; path.Data = pathGeometry; path.Stroke = Brushes.Black; path.StrokeThickness = 1; canvas.Children.Add(path); }
public static System.Windows.Shapes.Path DrawPathByCurve(Contours cons) { System.Windows.Shapes.Path path = new System.Windows.Shapes.Path(); path.Stroke = System.Windows.Media.Brushes.Red; path.StrokeThickness = 2; path.Fill = System.Windows.Media.Brushes.Green; var group = new PathGeometry(); path.Data = group; int i = 0; foreach (var contour in cons.Items) { //var subPath = new PathGeometry(); //subPath.Figures.Add(new Polygon()); var polygon = new PathFigure(); polygon.StartPoint = contour.Pts.First().ToPoint(); polygon.IsClosed = true; //polygon.IsFilled = true; //var seg = new PolyLineSegment(contour.Pts.Skip(1).Select(v => v.ToPoint()).ToList(), true); var pts1 = contour.Pts.Select(v => v.ToPoint()).ToList(); ///////////////////////////////////////// var beizerSegments = InterpolationUtils.InterpolatePointWithBeizerCurves(pts1, true); if (beizerSegments == null || beizerSegments.Count < 1) { //Add a line segment <this is generic for more than one line> //foreach (var point in points.GetRange(1, points.Count - 1)) //{ // var myLineSegment = new LineSegment { Point = point }; // myPathSegmentCollection.Add(myLineSegment); //} } else { foreach (var beizerCurveSegment in beizerSegments) { var segment = new BezierSegment { Point1 = beizerCurveSegment.FirstControlPoint, Point2 = beizerCurveSegment.SecondControlPoint, Point3 = beizerCurveSegment.EndPoint }; polygon.Segments.Add(segment); } } ///////////////////////////////////////// //polygon.Segments.Add(seg); //if (i == 0 || i == 2 || i == 4) // group.Figures.Add(polygon); i++; } return(path); }
public static Geometry GetGeometryByCurve(Contours cons, Prov prov) { //var path = new System.Windows.Shapes.Path(); //path.Stroke = System.Windows.Media.Brushes.Red; //path.StrokeThickness = 1; //path.Fill = System.Windows.Media.Brushes.Green; //////////////////////////////////////////////////////// ///////////////////////////////////// // Create a StreamGeometry to use to specify myPath. StreamGeometry geometry = new StreamGeometry(); geometry.FillRule = FillRule.EvenOdd; // Open a StreamGeometryContext that can be used to describe this StreamGeometry // object's contents. using (StreamGeometryContext ctx = geometry.Open()) { int left = 0; int top = 0; if (prov != null) { left = prov.Rectangle.Left; top = prov.Rectangle.Top; } foreach (var contour in cons.Items) { if (contour.Pts.Length <= 1) { continue; } //var first = beizerSegments.First().StartPoint; var firstPt = contour.Pts.First().ToPoint(left, top, 3); ////////////////////////////// // Begin the triangle at the point specified. Notice that the shape is set to // be closed so only two lines need to be specified below to make the triangle. ctx.BeginFigure(firstPt, true /* is filled */, true /* is closed */); // Draw a line to the next specified point. //ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */); //ctx.PolyLineTo(contour.Pts.Skip(1).Select(v => v.ToPoint(left, top, 2)).ToList(), true, false); var pts1 = contour.Pts.Select(v => v.ToPoint(left, top, 3)).ToList(); var beizerSegments = InterpolationUtils.InterpolatePointWithBeizerCurves(pts1, true); if (beizerSegments != null) { var points = beizerSegments.SelectMany( v => new List <System.Windows.Point> { v.FirstControlPoint, v.SecondControlPoint, v.EndPoint }) .ToList(); ctx.PolyBezierTo(points, true, false); } } } // Freeze the geometry (make it unmodifiable) // for additional performance benefits. geometry.Freeze(); //// Specify the shape (triangle) of the Path using the StreamGeometry. //path.Data = geometry; return(geometry); }
void SetPathData() { if (Points == null) { return; } var points = new List <Point>(); foreach (var point in Points) { var pointProperties = point.GetType().GetProperties(); if (pointProperties.All(p => p.Name != "X") || pointProperties.All(p => p.Name != "Y")) { continue; } var x = (float)point.GetType().GetProperty("X").GetValue(point, new object[] { }); var y = (float)point.GetType().GetProperty("Y").GetValue(point, new object[] { }); points.Add(new Point(x, y)); } if (points.Count <= 1) { return; } var myPathFigure = new PathFigure { StartPoint = points.FirstOrDefault() }; var myPathSegmentCollection = new PathSegmentCollection(); var beizerSegments = InterpolationUtils.InterpolatePointWithBeizerCurves(points, IsClosedCurve); if (beizerSegments == null || beizerSegments.Count < 1) { //Add a line segment <this is generic for more than one line> foreach (var point in points.GetRange(1, points.Count - 1)) { var myLineSegment = new LineSegment { Point = point }; myPathSegmentCollection.Add(myLineSegment); } } else { foreach (var beizerCurveSegment in beizerSegments) { var segment = new BezierSegment { Point1 = beizerCurveSegment.FirstControlPoint, Point2 = beizerCurveSegment.SecondControlPoint, Point3 = beizerCurveSegment.EndPoint }; myPathSegmentCollection.Add(segment); } } myPathFigure.Segments = myPathSegmentCollection; var myPathFigureCollection = new PathFigureCollection { myPathFigure }; var myPathGeometry = new PathGeometry { Figures = myPathFigureCollection }; path.Data = myPathGeometry; }