[1106]python bezier(贝塞尔)曲线


首先简单了解一下什么是贝塞尔曲线(余弦函数曲线我就不多说了哈!),贝塞尔曲线又称贝兹曲线,是法国工程师皮埃尔.贝塞尔于1962年发表。贝塞尔曲线广泛应用于二维绘图软件,早期用于汽车车体设计。

三阶贝塞尔曲线

三阶贝塞尔曲线由如下方程描述:
image.png

其中t的范围是0到1的闭区间。P0和P3是三阶贝塞尔曲线的起点和终点,P1和P2是曲线的控制点。

然后我们讲一下计算机绘制曲线的原理。从数学定义上,一条连续函数曲线有无数个点,从算法的特点将,算法具有有穷性。所以我们不可能把所有的点全部刻画在屏幕上。另一方面,计算机的屏幕像素是离散的,无法表示连续的曲线。于是引入一个概念,那就是微分思想。将曲线分为一个个小段,将曲线“化曲为直”。

最后说明一下计算机屏幕的坐标系。数学里的笛卡尔坐标系通常以水平向右为x轴正方向,垂直于x轴向上为y轴正方向。而计算机屏幕表示像素点时,其坐标原点位于屏幕左上角,x轴水平向右,而y轴垂直于x轴向下。

下面展示贝赛尔曲线函数代码:

def tri_bezier(p1,p2,p3,p4,t):
    parm_1 = (1-t)**3
    parm_2 = 3*(1-t)**2 * t
    parm_3 = 3 * t**2 * (1-t)
    parm_4 = t**3
 
    px = p1[0] * parm_1 + p2[0] * parm_2 + p3[0] * parm_3 + p4[0] * parm_4
    py = p1[1] * parm_1 + p2[1] * parm_2 + p3[1] * parm_3 + p4[1] * parm_4
    
    return (px,py)

效果展示:
image.png

python bezier曲线

pip install bezier

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

import numpy as np
import matplotlib.pyplot as plt
import bezier
b_xs = []
b_ys = []


# xs表示原始数据
# n表示阶数
# k表示索引
def one_bezier_curve(a, b, t):
    return (1 - t) * a + t * b


def n_bezier_curve(xs, n, k, t):
    if n == 1:
        return one_bezier_curve(xs[k], xs[k + 1], t)
    else:
        return (1 - t) * n_bezier_curve(xs, n - 1, k, t) + t * n_bezier_curve(xs, n - 1, k + 1, t)


def bezier_curve(xs, ys, num, b_xs, b_ys):
    n = 5  # 采用5次bezier曲线拟合
    t_step = 1.0 / (num - 1)
    # t_step = 1.0 / num
    print(t_step)
    t = np.arange(0.0, 1 + t_step, t_step)
    print(len(t))
    for each in t:
        b_xs.append(n_bezier_curve(xs, n, 0, each))
        b_ys.append(n_bezier_curve(ys, n, 0, each))


def func():
    xs = [1.0, 2.1, 3.0, 4.0, 5.0, 6.0]
    ys = [0, 1.1, 2.1, 1.0, 0.2, 0]
    num = 20

    bezier_curve(xs, ys, num, b_xs, b_ys)  # 将计算结果加入到列表中
    print(b_xs, b_ys)
    plt.figure()
    plt.plot(b_xs, b_ys, 'r')  # bezier曲线
    # plt.plot(xs, ys)  # 原曲线
    # plt.show()

func()

拼接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()

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

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]])
curve = bezier.Curve(a, degree=5)
# print(curve)
s_vals = np.linspace(0.0, 1.0, 30)
data = curve.evaluate_multi(s_vals)
x33 = data[0]
y33 = data[1]
plt.plot(x33, y33, 'y', linewidth=2.0, linestyle="-", label="y2")
plt.show()

参考:https://blog.csdn.net/PengphyLee21/article/details/100187050
https://www.cnblogs.com/yang220/p/12024532.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值