import numpy as np
import cmath
from matplotlib import pyplot as plt
from matplotlib import colors
from cycler import cycler
# plt_location_advanced中散点图点的大小
size = 75
# 角度计算函数
def angle(ver, tra1, tra2):
a = distance(tra1, tra2)
b = distance(tra1, ver)
c = distance(tra2, ver)
d = 2 * b * c
return np.degrees(np.arccos((b ** 2 + c ** 2 - a ** 2) / d))
def cost_sq(angle1, angle2):
return np.sum((np.array(angle1) - np.array(angle2)) ** 2)
def distance(p1, p2):
x = p1[0] - p2[0]
y = p1[1] - p2[1]
return np.sqrt(x ** 2 + y ** 2)
# 极坐标转直角坐标
def polar_to_cartesian(loc): # 极坐标转换为直角坐标
kid = np.radians(loc[1]) # 角度转化为弧度
return [loc[0] * np.cos(kid), loc[0] * np.sin(kid)]
# Input array in radians, output narray.
# 直角坐标转极坐标
def cartesian_to_polar(loc):
if loc[1] < 0:
loc[1] = loc[1] + 360
elif loc[1] > 360:
loc[1] = loc[1] - 360
cn = complex(loc[0], loc[1])
return cmath.polar(cn)
# 画出坐标
def plt_location(loc):
x = []
y = []
for i in loc:
x.append(i[0])
y.append(i[1])
plt.scatter(x, y)
plt.show()
# 显示坐标,并且用颜色代表差异
def plt_location_advanced(loc):
x = []
y = []
t = []
j = 0
for i in loc:
x.append(i[0])
y.append(i[1])
t.append(distance(i, location_ideal_cartesian[j]))
j += 1
# 美化
plt.style.use('bmh')
plt.rcParams['axes.prop_cycle'] = cycler(color='bgrcmyk')
plt.rcParams['image.cmap'] = 'jet'
change_color = colors.Normalize(vmin=0, vmax=16)
plt.scatter(x, y, s=size, c=t, alpha=0.8, norm=change_color)
plt.xlim((-150, 150))
plt.ylim((-150, 150))
plt.colorbar()
plt.show()
# 无人机实际极坐标
location_real_polar = [
[0, 0],
[100, 0],
[98, 40.10],
[112, 80.21],
[105, 119.75],
[98, 159.86],
[112, 199.96],
[105, 240.07],
[98, 280.17],
[112, 320.28],
]
# 无人机理想极坐标
location_ideal_polar = [
[0, 0],
[100, 0],
[100, 40],
[100, 80],
[100, 120],
[100, 160],
[100, 200],
[100, 240],
[100, 280],
[100, 320],
]
# 无人机真实直角坐标
location_real_cartesian = []
# 无人机理想直角坐标
location_ideal_cartesian = []
for location in location_real_polar:
location_real_cartesian.append(polar_to_cartesian(location))
for location in location_ideal_polar:
location_ideal_cartesian.append(polar_to_cartesian(location))
print(location_real_cartesian)
plt_location_advanced(location_real_cartesian)
# 用角度确定坐标
def location(fy_id, loc_real, sender1, sender2, sender3, mode):
if mode == 0:
angle_real = [angle(loc_real, sender1, sender2), angle(loc_real, sender2, sender3)]
elif mode == 1:
angle_real = loc_real
# 获取理想位置
loc_ideal = location_ideal_cartesian[fy_id]
# 生成初始位置表
distance_max = 8
distance_num = 160
x = np.linspace(-distance_max, distance_max, distance_num, endpoint=True)
y = np.linspace(-distance_max, distance_max, distance_num, endpoint=True)
# 生成调整后位置表
x += loc_ideal[0]
y += loc_ideal[1]
# 计算理想角度:注意,这个功能也许会在adjust中用到
# angle_ideal = [angle(loc, sender1, sender2), angle(loc, sender2, sender3)]
result = 999999
x_result = 0
y_result = 0
# 计算角度差值
for i in x:
for j in y:
loc_calculate = [i, j]
angle_calculate = [angle(loc_calculate, sender1, sender2), angle(loc_calculate, sender2, sender3)]
cost_result = cost_sq(angle_calculate, angle_real)
if result >= cost_result:
result = cost_result
x_result = i
y_result = j
return [x_result, y_result]
print(
location(
3,
location_real_cartesian[3],
location_real_cartesian[0],
location_real_cartesian[1],
location_real_cartesian[2],
0
)
)
def adjust(fy_id, sender_id):
loc = location_ideal_cartesian[fy_id]
sender1_r = location_real_cartesian[0]
sender2_r = location_real_cartesian[1]
sender3_r = location_real_cartesian[sender_id]
sender1 = location_ideal_cartesian[0]
sender2 = location_ideal_cartesian[1]
sender3 = location_ideal_cartesian[sender_id]
angle_ideal = [angle(loc, sender1, sender2), angle(loc, sender2, sender3)]
return location(fy_id, angle_ideal, sender1_r, sender2_r, sender3_r, 1)
for i in range(5):
location_real_cartesian[3] = (adjust(3, 2))
location_real_cartesian[4] = (adjust(4, 3))
location_real_cartesian[5] = (adjust(5, 4))
location_real_cartesian[6] = (adjust(6, 5))
location_real_cartesian[7] = (adjust(7, 6))
location_real_cartesian[8] = (adjust(8, 7))
location_real_cartesian[9] = (adjust(9, 8))
location_real_cartesian[2] = (adjust(2, 9))
plt_location_advanced(location_real_cartesian)
dis = []
j = 0
for i in location_real_cartesian:
dis.append(distance(i, location_ideal_cartesian[j]))
j += 1
print(sum(dis))
plt_location_advanced(location_real_cartesian)
为什么我无法运行?
D:\develop\Python\python.exe D:\develop\Python代码\新生赛\new.py
[[np.float64(0.0), np.float64(0.0)], [np.float64(100.0), np.float64(0.0)], [np.float64(74.96229729000632), np.float64(63.124115716615876)], [np.float64(19.044201170560154), np.float64(110.36901015128855)], [np.float64(-52.10273288589687), np.float64(91.16087552135994)], [np.float64(-92.00770214648452), np.float64(33.74289178069639)], [np.float64(-105.2722906898315), np.float64(-38.23277145219291)], [np.float64(-52.38886563516721), np.float64(-90.99674036722632)], [np.float64(17.30380046043861), np.float64(-96.46024305186735)], [np.float64(86.14777204829524), np.float64(-71.57207116686621)]]
Traceback (most recent call last):
File "D:\develop\Python代码\新生赛\new.py", line 118, in <module>
plt_location_advanced(location_real_cartesian)
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\develop\Python代码\新生赛\new.py", line 79, in plt_location_advanced
plt.show()
~~~~~~~~^^
File "D:\develop\Python\Lib\site-packages\matplotlib\pyplot.py", line 614, in show
return _get_backend_mod().show(*args, **kwargs)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "D:\develop\pycharm2024.1.7\PyCharm 2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 41, in __call__
manager.show(**kwargs)
~~~~~~~~~~~~^^^^^^^^^^
File "D:\develop\pycharm2024.1.7\PyCharm 2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 144, in show
self.canvas.show()
~~~~~~~~~~~~~~~~^^
File "D:\develop\pycharm2024.1.7\PyCharm 2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 85, in show
buffer = self.tostring_rgb()
^^^^^^^^^^^^^^^^^
AttributeError: 'FigureCanvasInterAgg' object has no attribute 'tostring_rgb'. Did you mean: 'tostring_argb'?
进程已结束,退出代码为 1