python实现一个点绕另一个点旋转后的坐标

yipeiwu_com5年前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实现一个点绕另一个点旋转后的坐标就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现的数据结构与算法之队列详解

Python实现的数据结构与算法之队列详解

本文实例讲述了Python实现的数据结构与算法之队列。分享给大家供大家参考。具体分析如下: 一、概述 队列(Queue)是一种先进先出(FIFO)的线性数据结构,插入操作在队尾(rear...

tensor和numpy的互相转换的实现示例

要对tensor进行操作,需要先启动一个Session,否则,我们无法对一个tensor比如一个tensor常量重新赋值或是做一些判断操作,所以如果将它转化为numpy数组就好处理了。下...

Python使用迭代器打印螺旋矩阵的思路及代码示例

Python使用迭代器打印螺旋矩阵的思路及代码示例

思路 螺旋矩阵是指一个呈螺旋状的矩阵,它的数字由第一行开始到右边不断变大,向下变大, 向左变大,向上变大,如此循环。 螺旋矩阵用二维数组表示,坐标(x,y),即(x轴坐标,y轴坐标)。...

python在openstreetmap地图上绘制路线图的实现

python在openstreetmap地图上绘制路线图的实现

利用python进行经纬度轨迹展示 嘿!各位好久不见,距离第一次发博客已经过去两年多了,本人也从本科生变成了研究生,好了书归正传,最近在做一个关于航班滑行路径轨迹的项目,目的是将航班的经...

python opencv读mp4视频的实例

如下所示: #获得视频的格式 videoCapture = cv2.VideoCapture('/home/lw/3661.mp4') #获得码率及尺寸 fps = videoC...