首页 > 解决方案 > 根据应用于源数组的公式创建数组

问题描述

我正在尝试将四个公式应用于数组中的一组坐标,以生成两个额外的数组,然后我可以绘制它们。

这两个新数组称为“internal_edge”和“external_edge”。

在下面的代码中,我打印了需要应用于“internal_edge”和“external_edge”的 X 和 Y 的四个方程。

import numpy as np
import math as m
from matplotlib import pyplot as plt

track_width = 0.25

centre_line = np.array([
    [5.2838386568469105, 0.5533114231405133],[5.366471208948866, 0.5138588293370626],[5.449968630730311, 0.47627584038467463],[5.534812288152224, 0.4418872118882958],[5.621950766713397, 0.4139674411266211],[5.711536576482786, 0.3955402988007556],[5.80273779150609, 0.38945006681201744],[5.893859166442468, 0.3968403768909704],[5.983006824666859, 0.41711137467093196],
])

x1, y1 = centre_line.T
#Internal X[0] Value:
print(track_width*m.cos(m.radians(m.degrees(m.atan2(y1[1]-y1[0],x1[1]-x1[0]))+90))+x1[1])
#Internal Y[0] Value:
print(track_width*m.sin(m.radians(m.degrees(m.atan2(y1[1]-y1[0],x1[1]-x1[0]))+90))+y1[1])
#External X[0] Value:
print(track_width*m.cos(m.radians(m.degrees(m.atan2(y1[1]-y1[0],x1[1]-x1[0]))-90))+x1[1])
#External Y[0] Value:
print(track_width*m.sin(m.radians(m.degrees(m.atan2(y1[1]-y1[0],x1[1]-x1[0]))-90))+y1[1])

internal_edge = np.array([
])
external_edge = np.array([
])

x2, y2 = internal_edge.T
x3, y3 = external_edge.T

plt.scatter(x1,y1,color='blue')
plt.scatter(x2,y2,color='green')
plt.scatter(x3,y3,color='red')
plt.show()

我现在需要弄清楚如何将这些应用到我的数组中的每个数据点以生成我的新数组。

完美输出 不完美输出

标签: pythonnumpy

解决方案


这是你如何做到的。

import numpy as np
from matplotlib import pyplot as plt

track_width = 0.25

centre_line = np.array([
    [5.2838386568469105, 0.5533114231405133],[5.366471208948866, 0.5138588293370626],[5.449968630730311, 0.47627584038467463],[5.534812288152224, 0.4418872118882958],[5.621950766713397, 0.4139674411266211],[5.711536576482786, 0.3955402988007556],[5.80273779150609, 0.38945006681201744],[5.893859166442468, 0.3968403768909704],[5.983006824666859, 0.41711137467093196],
])

x1, y1 = centre_line.T

# adjust for full dataset maybe?
x1 = np.append(x1, x1[0])
y1 = np.append(y1, y1[0])

angle = np.arctan2(y1[1:] - y1[:-1], x1[1:] - x1[:-1])


internal_edge = np.array([
    track_width*np.cos(angle + np.deg2rad(90)) + x1[1:],
    track_width*np.sin(angle + np.deg2rad(90)) + y1[1:],
])

external_edge = np.array([
    track_width*np.cos(angle - np.deg2rad(90)) + x1[1:],
    track_width*np.sin(angle - np.deg2rad(90)) + y1[1:],
])

x2, y2 = internal_edge
x3, y3 = external_edge

plt.scatter(x1,y1,color='blue')
plt.scatter(x2,y2,color='green')
plt.scatter(x3,y3,color='red')
plt.show()


推荐阅读