我有几个要点,我尝试使用下面的代码绘制贝塞尔曲线

 PathFigure pf = new PathFigure(points.From, ps, false); //ps - list of Bezier segments
    PathFigureCollection pfc = new PathFigureCollection();
    pfc.Add(pf);
    var pge = new PathGeometry();
    pge.Figures = pfc;
    Path p = new Path();
    p.Data = pge;
    p.Stroke = new SolidColorBrush(Color.FromRgb(244, 111, 011));

我的贝塞尔曲线分割看起来像这样
  • 1,2,3点-第一段
  • 3,4,5点-第二
  • 5,6,7 .. ..

  • 但是我得到了一条奇怪的曲线(这是3个大(节点)和7个小椭圆(这是我的观点)):

    最佳答案

    您得到的线是三个不同的Bezier曲线的并集-每三个点一组。 (每个“贝塞尔分割市场”各有一个?)

    如果需要一条平滑曲线,则需要将9个(或更多)点作为单个点集合(单个“贝塞尔曲线”?)传递,而不是作为三个点的组传递。

    编辑:显然 BezierSegment 仅支持三点,因此也难怪这是行不通的。甚至'PolyBezierSegment'也只是给出了Bezier片段的集合,而不是单个平滑的Bezier ...

    因此,由于WPF无法为您提供任何有用的信息,因此我使用数学here将某些东西组合在一起。这是一个数字解决方案,但即使有足够多的点看起来又平滑又好看,它的表现还是不错的:

    PolyLineSegment GetBezierApproximation(Point[] controlPoints, int outputSegmentCount)
    {
        Point[] points = new Point[outputSegmentCount + 1];
        for (int i = 0; i <= outputSegmentCount; i++)
        {
            double t = (double)i / outputSegmentCount;
            points[i] = GetBezierPoint(t, controlPoints, 0, controlPoints.Length);
        }
        return new PolyLineSegment(points, true);
    }
    
    Point GetBezierPoint(double t, Point[] controlPoints, int index, int count)
    {
        if (count == 1)
            return controlPoints[index];
        var P0 = GetBezierPoint(t, controlPoints, index, count - 1);
        var P1 = GetBezierPoint(t, controlPoints, index + 1, count - 1);
        return new Point((1 - t) * P0.X + t * P1.X, (1 - t) * P0.Y + t * P1.Y);
    }
    

    使用这个,
    private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        Point[] points = new[] {
                new Point(0, 200),
                new Point(0, 0),
                new Point(300, 0),
                new Point(350, 200),
                new Point(400, 0)
            };
        var b = GetBezierApproximation(points, 256);
        PathFigure pf = new PathFigure(b.Points[0], new[] { b }, false);
        PathFigureCollection pfc = new PathFigureCollection();
        pfc.Add(pf);
        var pge = new PathGeometry();
        pge.Figures = pfc;
        Path p = new Path();
        p.Data = pge;
        p.Stroke = new SolidColorBrush(Color.FromRgb(255, 0, 0));
        ((Grid)sender).Children.Add(p);
    }
    

    关于c# - 如何用几点画贝塞尔曲线?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13940983/

    10-15 06:47