gpt4 book ai didi

python - 角度插补

转载 作者:行者123 更新时间:2023-11-30 22:41:01 24 4
gpt4 key购买 nike

我试图插入列表中的角度。

Dir DirOffset
0 109.6085
30 77.5099
60 30.5287
90 -10.2748
120 -75.359
150 -147.6015
180 -162.7055
210 21.0103
240 3.5502
270 -11.5475
300 -39.8371
330 -109.5473
360 109.6085

enter image description here

我已经编写了插值角度的代码(它不断计算角度之间的平均值以达到插值值),这需要很长时间。如果有人有更快更短的代码,请帮助我。

enter image description here

from cmath import rect, phase
from math import radians, degrees, sqrt

#Calculate the mean of angles in List
def mean_angle(degArray):
return degrees(phase(sum(rect(1, radians(d)) for d in degArray)/len(degArray)))

#Calculate Interpolation Angle
def Interpolate_angle(Dir, DirOffset, ValuetoInterpolate):
#Create Lower and Higher bin of ValuetoInterpolate
DirLBin = round(float(ValuetoInterpolate)/30,0)*30
DirHBin = round(float(ValuetoInterpolate+15)/30,0)*30

#Check if the ValuetoInterpolate lies between Lower and Higher bin
if DirLBin == DirHBin:
DirLBin = DirHBin-30
if DirLBin <= ValuetoInterpolate <= DirHBin:
DBin = [float(DirLBin), float(DirHBin)]
Doff = [DirOffset[Dir.index(DirLBin)], DirOffset[Dir.index(DirHBin)]]
else:
DirHBin = DirLBin+30
DBin = [float(DirLBin), float(DirHBin)]
Doff = [DirOffset[Dir.index(DirLBin)], DirOffset[Dir.index(DirHBin)]]

else:
DBin = [float(DirLBin), float(DirHBin)]
Doff = [DirOffset[Dir.index(DirLBin)], DirOffset[Dir.index(DirHBin)]]

#Run 50 iterations to calculate the mean of angle and find the ValuetoInterpolate
for i in range(51):
DMean = mean_angle(DBin)
DOMean = mean_angle(Doff)
if DMean < 0 :
DMean = 360+DMean

if DBin[0] <= ValuetoInterpolate <=DMean:
DBin = [float(DBin[0]), float(DMean)]
Doff = [float(Doff[0]), float(DOMean)]
else:
DBin = [float(DMean), float(DBin[1])]
Doff = [float(DOMean), float(Doff[1])]

return DOMean

Dir = range(0,370,30)
DirOffset = [109.6085,77.5099,30.5287,-10.2748,-75.359,-147.6015,-162.7055,21.0103,3.5502,-11.5475,-39.8371,-109.5473,109.6085]
ValuetoInterpolate = 194.4
print Interpolate_angle(Dir, DirOffset, ValuetoInterpolate)

最佳答案

在从 stackoverflow 搜索答案后,我得到了上述问题的解决方案,然后我做了一些修改以根据我的要求获得解决方案。该解决方案可能对需要它的人有用。

我使用以下函数对每个方向箱(0,30,60....360)插入度数,直到360(360和0度将相同)并将它们存储在字典中以创建DataFrame(pandas DataFrame)并将其与主 DataFrame 连接并进一步处理。

enter image description here

def InterpolateDegrees(109.6085,77.5099)

将返回 DirectionOffset 0 到 30 度的插值数组,间隔为 0.1 (0.0, 0.1, 0.2, 0.3......28.7, 29.8, 29.9)

import numpy as np
from math import fabs

def InterpolateDegrees(start, end, BinSector=12):
BinAngle = 360/BinSector
amount = np.arange(0,1,(1/(BinAngle/0.1)))
dif = fabs(end-start)
if dif >180:
if end>start:
start+=360
else:
end+=360

#Interpolate it
value = (start + ((end-start)*amount))

#Wrap it
rzero = 360

Arr = np.where((value>=0) & (value<=360), (value), (value % rzero))
return Arr

关于python - 角度插补,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42696118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com