python - 转换自然范围外的角度(对于该范围内的 theta : 0 to 180 degrees) to an equivalent angle,

标签 python numpy coordinate-systems

如何缩短以下代码?这是一个简单的代码,用于查看角度值并确保它们不会超出范围。问题是,虽然这可以完成工作,但我真的想要更像 pythonic、更易于使用且编写起来更简单的东西。

theta_min_lim = 0.0
theta_max_lim = 180.0
if theta_min<theta_min_lim:
    new_theta_min = theta_max_lim-abs(theta_min)
    theta_row_index_along_new = np.array(np.where(sph_pos_count[:,1]>=new_theta_min)).flatten()
    theta_row_index_along = np.concatenate((theta_row_index_along_new, theta_row_index_along))

if theta_max>theta_max_lim:
    new_theta_max = theta_max-theta_max_lim
    theta_row_index_along_new = np.array(np.where(sph_pos_count[:,1]<=new_theta_max)).flatten()
    theta_row_index_along = np.concatenate((theta_row_index_along_new, theta_row_index_along))

if phi_min<phi_min_lim:
    new_phi_min = phi_max_lim-abs(phi_min)
    phi_row_index_along_new = np.array(np.where(sph_pos_count[:,2]>=new_phi_min)).flatten()
    phi_row_index_along = np.concatenate((phi_row_index_along_new, phi_row_index_along))

if phi_max>phi_max_lim:
    new_phi_max = phi_max-phi_max_lim
    phi_row_index_along_new = np.array(np.where(sph_pos_count[:,2]<=new_phi_max)).flatten()
    phi_row_index_along = np.concatenate((phi_row_index_along_new, phi_row_index_along))

#theta_row_index_along = np.concatenate(theta_row_index_along_new, theta_row_index_along)
row_index_along = np.intersect1d(theta_row_index_along,phi_row_index_along)

sph_pos_count_along = sph_pos_count[row_index_along]

#GIving range for theta and phi in the direction along the velocity
 theta_min_opp = vtheta_opp - 2.0*max(sph_cord[:,1])/dtheta
 theta_max_opp = vtheta_opp + 2.0*max(sph_cord[:,1])/dtheta
 phi_min_opp = vphi_opp - 2.0*max(sph_cord[:,2])/dphi
 phi_max_opp = vphi_opp + 2.0*max(sph_cord[:,2])/dphi

 #Finding index of spheres opposite to the direction of motion

 theta_row_index_opp = np.array(np.where(np.logical_and(sph_pos_count[:,1]>=theta_min_opp, sph_pos_count[:,1]<=theta_max_opp))).flatten()
phi_row_index_opp = np.array(np.where(np.logical_and(sph_pos_count[:,2]>=phi_min_opp,  sph_pos_count[:,2]<=phi_max_opp))).flatten()

if theta_min_opp<theta_min_lim:
    new_theta_min_opp = theta_max_lim-abs(theta_min_opp)
    theta_row_index_opp_new = np.array(np.where(sph_pos_count[:,1]>=new_theta_min_opp)).flatten()
    theta_row_index_opp = np.concatenate((theta_row_index_opp_new, theta_row_index_opp))

if theta_max_opp>theta_max_lim:
    new_theta_max_opp = theta_max_opp-theta_max_lim
    theta_row_index_opp_new = np.array(np.where(sph_pos_count[:,1]<=new_theta_max_opp)).flatten()
    theta_row_index_opp = np.concatenate((theta_row_index_opp_new, theta_row_index_opp))

if phi_min_opp<phi_min_lim:
    new_phi_min_opp = phi_max_lim-abs(phi_min_opp)
    phi_row_index_opp_new = np.array(np.where(sph_pos_count[:,2]>=new_phi_min_opp)).flatten()
    phi_row_index_opp = np.concatenate((phi_row_index_opp_new, phi_row_index_opp))

if phi_max_opp>phi_max_lim:
    new_phi_max_opp = phi_max_opp-phi_max_lim
    phi_row_index_opp_new = np.transpose(np.where(sph_pos_count[:,2]<=new_phi_max_opp)).flatten()
    phi_row_index_opp = np.concatenate((phi_row_index_opp_new, phi_row_index_opp))

最佳答案

只需使用模运算(Python 中的模 % 运算符):

    theta = theta % 180

对于 -30,这给你 150。对于 190,这将为您提供 10

关于python - 转换自然范围外的角度(对于该范围内的 theta : 0 to 180 degrees) to an equivalent angle,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21668281/

相关文章:

ios - 当我将 nil 传递给 view 参数时,convert(_ rect : CGRect, to view : UIView? ) 什么也不做

matlab - 相对于不同坐标系的轴缩放和旋转 3D 点

arduino - 转换交集面积以生成坐标

android - 使用 python 进行跨平台 GUI 编程

python - 如何使用python+MySQLdb创建utf8mb4数据库,mysql命令行已经显示utf8mb4,但数据库仍然是utf8

python - 列表中每个连续段的平均值

python - 如何使用Python绘制模拟几何随机变量的直方图?

python - 使用 numpy 切片数组索引 numpy 数组

python - 安装包之间的pycurl版本冲突

python - 为什么 NaN 被视为 float ?