c# 贝塞尔计算

  public class PointF
        {
            public float X { get; set; }
            public float Y { get; set; }
        }
      public Dictionary<string,string> Getpoints(string[] args)
        {
           Dictionary<string,string>dic=new Dictionary<string,string>();
            PointF[] pointList = new PointF[] { new PointF {X=1.3F, Y=2.4F}, new PointF { X = 2.0F, Y=3.00F }, new PointF { X = 12.3F, Y=13.2F } };
            PointF[] aa = draw_bezier_curves(pointList, pointList.Length, 0.001F); // 在起点和终点之间画1/0.001=1000个点
            foreach (PointF item in aa)
            {
                dic.Add(item.X.ToString(),item.Y.ToString());
            }
               return dic;
        
        }
        public static PointF[] draw_bezier_curves(PointF[] points, int count, float step)
        {
            List<PointF> bezier_curves_points = new List<PointF>();
            float t = 0F;
            do
            {
                PointF temp_point = bezier_interpolation_func(t, points, count);    // 计算插值点
                t += step;
                bezier_curves_points.Add(temp_point);
            }
            while (t <= 1 && count > 1);    // 一个点的情况直接跳出.
            return bezier_curves_points.ToArray();  // 曲线轨迹上的所有坐标点
        }
       
        private static PointF bezier_interpolation_func(float t, PointF[] points, int count)
        {
            if (points.Length < 1)  // 一个点都没有
                throw new ArgumentOutOfRangeException();
            PointF[] tmp_points = new PointF[count];
            for (int i = 1; i < count; ++i)
            {
                for (int j = 0; j < count - i; ++j)
                {
                    if (i == 1) 
                    {
                        float x1= (float)(points[j].X * (1 - t) + points[j + 1].X * t);
                        float y1= (float)(points[j].Y * (1 - t) + points[j + 1].Y * t);
                        tmp_points[j] = new PointF { X = x1, Y = y1 };
                        continue;
                    }
                    float x = (float)(tmp_points[j].X * (1 - t) + tmp_points[j + 1].X * t);
                    float y = (float)(tmp_points[j].Y * (1 - t) + tmp_points[j + 1].Y * t);
                    tmp_points[j] = new PointF { X = x, Y = y };
                }
            }
            return tmp_points[0];

        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值