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

## 实现方案

### 曲线上的点集

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

 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);
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);
44                 current = next;
45             }
46         }
47         return outputPoints;
48     }

### 曲线上的点

 1         private bool TryGetOrdinateOnVectorByAbscissa(Point start, Point end, double abscissa, out double ordinate)
2         {
3             ordinate = 0.0;
4             if ((start.X < end.X && abscissa >= start.X && abscissa <= end.X) ||
5                 (start.X > end.X && abscissa <= start.X && abscissa >= end.X))
6             {
7                 var xRatio = (abscissa - start.X) / (end.X - start.X);
8                 var yLength = end.Y - start.Y;
9                 var y = yLength * xRatio + start.Y;
10                 ordinate = y;
11                 return true;
12             }
13             return false;
14         }

Github： Demo

posted @ 2019-03-25 22:37 唐宋元明清2188 阅读(...) 评论(...) 编辑 收藏