【python常用函数】限制数组模的最大最小值(速度约束)+向量标准化生成单位向量(方向向量)

1.学会写函数啊!!!

1.1.模限制

v = truncate(v, max_length=MAX_SPEED)
import numpy as np
MAX_SPEED = 30
v = np.array([50,50,0])

def truncate(vector, min_length=None, max_length=None):
    """
    Truncate the length of a vector to be within the specified range.

    Parameters:
        vector (numpy.ndarray): The input vector.
        min_length (float, optional): The minimum length of the vector. Defaults to None.
        max_length (float, optional): The maximum length of the vector. Defaults to None.

    Returns:
        numpy.ndarray: The truncated vector.
    """
    length = np.linalg.norm(vector)  # 计算向量的长度

    if min_length is not None and length < min_length:
        # 如果指定了最小长度且当前长度小于最小长度,则缩放到最小长度
        return vector / length * min_length
    elif max_length is not None and length > max_length:
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值