您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關python如何實現一個點繞另一個點旋轉后的坐標,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
如下所示:
(x,y)為要轉的點,(pointx,pointy)為中心點,如果順時針角度為angle
srx = (x-pointx)*cos(angle) + (y-pointy)*sin(angle)+pointx
sry = (y-pointy)*cos(angle) - (x-pointx)*sin(angle)+pointy
(x,y)為要轉的點,(pointx,pointy)為中心點,如果逆時針角度為angle
nrx = (x-pointx)*cos(angle) - (y-pointy)*sin(angle)+pointx
nry = (x-pointx)*sin(angle) + (y-pointy)*cos(angle)+pointy
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 = 1 pointy = 1 sPointx ,sPointy = Nrotate(math.radians(45),pointx,pointy,0,0) print(sPointx,sPointy) plt.plot([0,pointx],[0,pointy]) plt.plot([0,sPointx],[0,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()
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 x1 = np.array([1,2,3]) y1 = np.array([1,2,3]) x2 = np.array([3,6]) y2 = np.array([2,1]) # x2往x1上拼 disx = x2[0]-x1[0] disy = y2[0]-y1[0] removeX2 = x2-disx removeY2 = y2-disy tany1 = y1[-1]-y1[0] tanx1 = x1[-1]-x1[0] angle1 = math.degrees(np.arctan(tany1/tanx1)) tany2 = y2[-1]-y2[0] tanx2 = x2[-1]-x2[0] angle2 = math.degrees(np.arctan(tany2/tanx2)) disAngle = angle2-angle1 print(angle1) print(angle2) print(disAngle) if disAngle<0: rotateX,rotateY = Nrotate(math.radians(abs(disAngle)),removeX2,removeY2,x1[0],y1[0]) else: rotateX,rotateY = Srotate(math.radians(abs(disAngle)),removeX2,removeY2,x1[0],y1[0]) plt.plot(x1,y1,color='red') plt.plot(removeX2,removeY2,color='green') plt.scatter(rotateX,rotateY,color='yellow') plt.xlim(0.,10.) plt.ylim(0.,10.) plt.xticks(np.arange(0.,10.,1)) plt.yticks(np.arange(0.,10.,1)) plt.show()
關于“python如何實現一個點繞另一個點旋轉后的坐標”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。