zoukankan      html  css  js  c++  java
  • python bezier 曲线

    1.手写bezier公式,生成bezier代码, 如果给的点数过多,则会生成一半bezier曲线,剩下的一半就需要进行拼接:

     1 import numpy as np
     2 import matplotlib.pyplot as plt
     3 import bezier
     4 b_xs = []
     5 b_ys = []
     6 
     7 
     8 # xs表示原始数据
     9 # n表示阶数
    10 # k表示索引
    11 def one_bezier_curve(a, b, t):
    12     return (1 - t) * a + t * b
    13 
    14 
    15 def n_bezier_curve(xs, n, k, t):
    16     if n == 1:
    17         return one_bezier_curve(xs[k], xs[k + 1], t)
    18     else:
    19         return (1 - t) * n_bezier_curve(xs, n - 1, k, t) + t * n_bezier_curve(xs, n - 1, k + 1, t)
    20 
    21 
    22 def bezier_curve(xs, ys, num, b_xs, b_ys):
    23     n = 5  # 采用5次bezier曲线拟合
    24     t_step = 1.0 / (num - 1)
    25     # t_step = 1.0 / num
    26     print(t_step)
    27     t = np.arange(0.0, 1 + t_step, t_step)
    28     print(len(t))
    29     for each in t:
    30         b_xs.append(n_bezier_curve(xs, n, 0, each))
    31         b_ys.append(n_bezier_curve(ys, n, 0, each))
    32 
    33 
    34 def func():
    35     xs = [1.0, 2.1, 3.0, 4.0, 5.0, 6.0]
    36     ys = [0, 1.1, 2.1, 1.0, 0.2, 0]
    37     num = 20
    38 
    39     bezier_curve(xs, ys, num, b_xs, b_ys)  # 将计算结果加入到列表中
    40     print(b_xs, b_ys)
    41     plt.figure()
    42     plt.plot(b_xs, b_ys, 'r')  # bezier曲线
    43     # plt.plot(xs, ys)  # 原曲线
    44     # plt.show()
    45 
    46 func()

    2. 拼接bezier曲线

    def point_bezier(avoid_point):
        global p
        xs = avoid_point[0, 0]  # 0
        ys = avoid_point[1, 0]  # 0
        xe = avoid_point[0, -1]  # 34.5844
        ye = avoid_point[1, -1]  # 0
        Latoff = 2.3
        startp = np.array([xs, ys])
        endp = np.array([xe, ye])
        endp1 = np.array([xe+2, ye])
        # print(startp, endp)
        P0 = startp
        P1 = np.array([startp[0] + (endp[0] - startp[0]) / 8, startp[1]])
        P2 = np.array([startp[0] + (endp[0] - startp[0]) / 8 * 2, startp[1]])
        P3 = np.array([startp[0] + (endp[0] - startp[0]) / 8 * 2, startp[1] + Latoff])
        P4 = np.array([startp[0] + (endp[0] - startp[0]) / 8 * 3, startp[1] + Latoff])
        P5 = np.array([startp[0] + (endp[0] - startp[0]) / 8 * 4, startp[1] + Latoff])
        P6 = np.array([startp[0] + (endp[0] - startp[0]) / 8 * 5, startp[1] + Latoff])
        P7 = np.array([startp[0] + (endp[0] - startp[0]) / 8 * 6, startp[1] + Latoff])
        P8 = np.array([startp[0] + (endp[0] - startp[0]) / 8 * 6, startp[1] + Latoff])
        P9 = np.array([startp[0] + (endp[0] - startp[0]) / 8 * 7, startp[1]])
        P10 = endp
        P11 = endp1
        i = 1
        half_length = 0.5 * (xe + xs)
        for u in np.arange(startp[0], startp[0] + (endp[0] - startp[0]) / 2, 0.2):
            # for u =startp[0]:0.2: startp[1] + (endp[1] - startp[1]) / 2
            c = (1 - (u - startp[0]) / half_length) ** 5 * P0 + 5 * (1 - (u - startp[0]) / half_length) ** 4 * (
                    u - startp[0]) / half_length * P1 + 10 * (1 - (u - startp[0]) / half_length) ** 3 * (
                        (u - startp[0]) / half_length) ** 2 * P2 + 10 * (1 - (u - startp[0]) / half_length) ** 2 * (
                        (u - startp[0]) / half_length) ** 3 * P3 + 5 * (1 - (u - startp[0]) / half_length) * (
                        (u - startp[0]) / half_length) ** 4 * P4 + ((u - startp[0]) / half_length) ** 5 * P5
            i = i + 1
            p = np.append(p, [c], axis=0)
    
        for u in np.arange(startp[0] + half_length, endp[0], 0.2):
            d = (1 - (u - startp[0] - half_length) / half_length) ** 5 * P6 + 5 * (
                        1 - (u - startp[0] - half_length) / half_length) ** 4 * (
                        u - startp[0] - half_length) / half_length * P7 + 10 * (
                            1 - (u - startp[0] - half_length) / half_length) ** 3 * (
                        (u - startp[0] - half_length) / half_length) ** 2 * P8 + 10 * (
                            1 - (u - startp[0] - half_length) / half_length) ** 2 * (
                        (u - startp[0] - half_length) / half_length) ** 3 * P9 + 5 * (
                            1 - (u - startp[0] - half_length) / half_length) * (
                        (u - startp[0] - half_length) / half_length) ** 4 * P10 + (
                            (u - startp[0] - half_length) / half_length) ** 5 * P11
            i = i + 1
            p = np.append(p, [d], axis=0)
        return p
        # print(p)
        # plt.plot(p[:, 0], p[:, 1], 'r')
        # plt.show()

    3.使用python 内置bezier包,完成bezier曲线(使用前需安装bezier包)

    1 a = np.array([[1.0, 2.1, 3.0, 4.0, 5.0, 6.0], [0, 1.1, 2.1, 1.0, 0.2, 0]])
    2 curve = bezier.Curve(a, degree=5)
    3 # print(curve)
    4 s_vals = np.linspace(0.0, 1.0, 30)
    5 data = curve.evaluate_multi(s_vals)
    6 x33 = data[0]
    7 y33 = data[1]
    8 plt.plot(x33, y33, 'y', linewidth=2.0, linestyle="-", label="y2")
    9 plt.show()
  • 相关阅读:
    20210123
    20210122
    20210121
    20210120
    2020119
    20210118
    20210117
    20210116
    例2-8
    例2-6
  • 原文地址:https://www.cnblogs.com/yang220/p/12024532.html
Copyright © 2011-2022 走看看