C# 曲线上的点(一) 获取指定横坐标对应的纵坐标值

获取直线上的点,很容易,那曲线呢?二阶贝塞尔、三阶贝塞尔、多段混合曲线,如何获取指定横坐标对应的纵坐标?

如下图形:

C# 曲线上的点(一) 获取指定横坐标对应的纵坐标值

 实现方案 曲线上的点集

Geometry提供了一个函数GetFlattenedPathGeometry,可以获取其绘制后显示的多边形。

我们可以通过其Figures -> PathSegment -> Point,

1 public List<Point> GetPointsOnPath(Geometry geometry) 2 { 3 List<Point> points = new List<Point>(); 4 PathGeometry pathGeometry = geometry.GetFlattenedPathGeometry(); 5 foreach (var figure in pathGeometry.Figures) 6 { 7 var ordinateOnPathFigureByAbscissa = GetOrdinateOnPathFigureByAbscissa(figure); 8 points.AddRange(ordinateOnPathFigureByAbscissa); 9 } 10 return points; 11 } 12 private List<Point> GetOrdinateOnPathFigureByAbscissa(PathFigure figure) 13 { 14 List<Point> outputPoints = new List<Point>(); 15 Point current = figure.StartPoint; 16 foreach (PathSegment s in figure.Segments) 17 { 18 PolyLineSegment segment = s as PolyLineSegment; 19 LineSegment line = s as LineSegment; 20 Point[] points; 21 if (segment != null) 22 { 23 points = segment.Points.ToArray(); 24 } 25 else if (line != null) 26 { 27 points = new[] { line.Point }; 28 } 29 else 30 { 31 throw new InvalidOperationException("尼玛!"); 32 } 33 foreach (Point next in points) 34 { 35 var ellipse = new Ellipse() 36 { 37 Width = 6, 38 Height = 6, 39 Fill = Brushes.Blue 40 }; 41 Canvas.SetTop(ellipse, next.Y); 42 Canvas.SetLeft(ellipse, next.X); 43 ContentCanvas.Children.Add(ellipse); 44 current = next; 45 } 46 } 47 return outputPoints; 48 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyywpw.html