python - Calculate the rotation angle of a vector python
问题描述
I am trying to find the rotation angle
of a 2D vector
. I have found a few questions that use 3D vectors
. The following df
represents a single vector
with the first row
as the origin.
d = ({
'X' : [10,12.5,17,20,16,14,13,8,7],
'Y' : [10,12,13,8,6,7,8,8,9],
})
df = pd.DataFrame(data = d)
I can rotate a vector using the following equation:
angle = x
theta = (x/180) * numpy.pi
rotMatrix = numpy.array([[numpy.cos(theta), -numpy.sin(theta)],
[numpy.sin(theta), numpy.cos(theta)]])
But I'm not sure how I would find the angle
at each point of time using the coordinates listed above. Apologies for using a df
. It replicates my actual dataset
解决方案
首先你应该将原点移动到(0, 0)
,然后你可以使用np.arctan2()
它来计算角度并正确定义象限。结果已经以弧度 (theta) 为单位,因此您不需要以度 (alpha) 为单位。
d = {'X' : [10,12.5,17,20,16,14,13,8,7],
'Y' : [10.,12,13,8,6,7,8,8,9]}
df = pd.DataFrame(data = d)
# move the origin
x = df["X"] - df["X"][0]
y = df["Y"] - df["Y"][0]
df["theta"] = np.arctan2(y, x)
df["aplha"] = np.degrees(df["theta"])
df
推荐阅读
- javascript - 在不同的数组中查找匹配项(Google Apps 脚本)
- go - 如何返回结构实例,惯用方式
- visual-studio-code - 开发自定义扩展时如何使用内置的Javascript扩展在JS文件中自动查找符号?
- python - Jupyter notebook:有没有办法在导出到 html 时折叠部分
- reactjs - 无法从我的商店中删除元素并升级我的组件
- configuration - NLog 系统日志配置文件
- graphql - 如何将查询与 apollo 的 refetchQuery 匹配
- java - 如何在添加对象之前为对象设置薪水
- javascript - 此 ChartJS 脚本的自动更新
- python - Pycharm warns package requirement not satisfied when using pipenv to install package