python绕点旋转

数学理论公式推导,基于相同的半径,然后旋转,利用三角函数的拆分方法得到旋转后的坐标

具体实现,

import numpy as np
import math
import matplotlib.pyplot as plt
# 绕pointx,pointy逆时针旋转
def Nrotate(angle,valuex,valuey,pointx,pointy):
  valuex = np.array(valuex)
  valuey = np.array(valuey)
  nRotatex = (valuex-pointx)*math.cos(angle) - (valuey-pointy)*math.sin(angle) + pointx
  nRotatey = (valuex-pointx)*math.sin(angle) + (valuey-pointy)*math.cos(angle) + pointy
  return nRotatex, nRotatey
# 绕pointx,pointy顺时针旋转
def Srotate(angle,valuex,valuey,pointx,pointy):
  valuex = np.array(valuex)
  valuey = np.array(valuey)
  sRotatex = (valuex-pointx)*math.cos(angle) + (valuey-pointy)*math.sin(angle) + pointx
  sRotatey = (valuey-pointy)*math.cos(angle) - (valuex-pointx)*math.sin(angle) + pointy
  return sRotatex,sRotatey

## 绕点坐标
pointx = 7.352847966
pointy = 210

## 旋转前坐标
targetX=6.074863636
targetY=40

## 旋转后坐标   0.5代表°, 注意如果x和y差距较大,则角度相应较小些。
sPointx ,sPointy = Srotate(math.radians(0.5),targetX,targetY,pointx,pointy)
print(sPointx,sPointy)
plt.plot([pointx,targetX],[pointy,targetY])
plt.plot([pointx,sPointx],[pointy,sPointy])
# plt.xlim(-3.,3.)
# plt.ylim(-3.,3.)
# plt.xticks(np.arange(-3.,3.,1))
# plt.yticks(np.arange(-3.,3.,1))
plt.show()
Related
叶昭良
叶昭良
Engineer of offshore wind turbine technique research

My research interests include distributed energy, wind turbine power generation technique , Computational fluid dynamic and programmable matter.