使用多个角度旋转 xy 点 - python 绕原点旋转围绕给定枢轴旋转示例

如何解决使用多个角度旋转 xy 点 - python 绕原点旋转围绕给定枢轴旋转示例

我目前正在使用参考点和固定点之间的角度旋转 xy 点。我希望包含两个固定点来旋转点。我只是不确定如何实现这一点。

使用下面,旋转的点显示在 xy 中。 X_RefY_RefX_FixedY_Fixed 之间的角度用于旋转点(在初始分布图中显示为黑色矢量)。

当前的过程是将固定/参考点转换为复数以计算旋转。我最终将旋转的点重新居中,使 X_Ref,Y_Ref 以 0,0 为中心(如旋转后的图中所示)。

但是,我希望包含 X_RefY_RefX2_FixedY2_Fixed 之间的角度。旋转后的图显示了旋转点时如何只考虑一个角度。我希望能兼顾两者以返回预期的输出。欢迎提出任何建议。

df = pd.DataFrame({  
    'Period' : ['1','1','1'],'Label' : ['A','B','C','D'],'x' : [0.0,-1.0,3.0,2.0],'y' : [0.0,0.0],'X_Ref' : [1,1,1],'Y_Ref' : [1,'X_Fixed' : [-2,-2,-2],'Y_Fixed' : [-2,'X2_Fixed' : [4,4,4],'Y2_Fixed' : [-2,})

fig,ax = plt.subplots(figsize = (6,6))
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
ax.grid(False)

初始分发:

enter image description here

# Rotate points using _Ref and _Fixed
# Re-center resulting distribution with _Ref as 0,0
for f in ['Ref','Fixed']:
    df[f] = df['X_'+f] + 1j*df['Y_'+f]
    df.drop(['X_'+f,'Y_'+f],axis=1,inplace=True)

df['angle'] = - np.angle(df['Ref'] - df['Fixed'])

df['rotated'] = (df['x'] + 1j*df["y"]) * np.exp(1j*df['angle'])
for f in ['Ref','Fixed']:
    df[f+'_Rotated'] = df[f] * np.exp(1j*df['angle'])

df['translation'] = - df['Ref_Rotated']
df['NewPoint'] = df['rotated'] + df['translation']
for f in ['Ref','Fixed']:
    df[f+'_Transformed'] = df[f+'_Rotated'] + df['translation']

df['x2'] = np.real(df['NewPoint'])
df['y2'] = np.imag(df['NewPoint'])
for f in ['Ref','Fixed']:
    df['NewX_'+f] = np.real(df[f+'_Transformed'])
    df['NewY_'+f] = np.imag(df[f+'_Transformed'])

output = df[['Label','x2','y2','NewX_Ref','NewY_Ref','NewX_Fixed','NewY_Fixed']]

旋转后:

ax.scatter(output['NewX_Ref'],output['NewY_Ref'],marker = 'x',zorder = 5,color = 'black')
ax.scatter(output['NewX_Fixed'],output['NewY_Fixed'],marker = '+',color = 'red')
ax.scatter(output['x2'],output['y2'],marker = 'o')

enter image description here

预期轮换:

enter image description here

解决方法

要解决的问题不明确。根据我的理解,问题是使一个点围绕一个枢轴点旋转一定的角度θ。 pandas 的数据框不太适合这个问题,让我们改用 numpy。为方便起见,我们首先定义一个类来表示平面中的点和向量。

class Coord:
    def __init__(self,x,y):
        x,y = np.array(x),np.array(y)
        assert x.shape == y.shape
        self.x,self.y = x,y
        self.shape = x.shape

    @property
    def radius(self):
        return np.sqrt(self.x ** 2 + self.y ** 2)

    def __sub__(self,other):
        return Coord(self.x - other.x,self.y - other.y)

    def __add__(self,other):
        return Coord(self.x + other.x,self.y + other.y)

    def __repr__(self):
        return f"Coord({self.x},{self.y})"

例如 Coord(2,3) 只是表示一个点或坐标为 (2,3) 的向量。可以执行加法和减法:Coord(2,3) + Coord(1,-2) 返回 Coord(3,1)。注意 xy 属性可以是 numpy 数组。在这种情况下,Coord 实例可以作为一组点。例如 Coord([1,2,0],[2,-1,3]) 表示点/向量 (1,2),(2,-1) and (0,3)

绕原点旋转

让我们在类 _rotate_from_origin 中添加一个方法 Coord,用于输出旋转后的点。您可以在 here 中找到有关如何旋转点的说明。

    def _rotate_from_origin(self,theta):
        return Coord(
            self.x * np.cos(theta) - self.y * np.sin(theta),self.x * np.sin(theta) + self.y * np.cos(theta))

围绕给定枢轴旋转

您似乎定义了坐标 x_refy_ref,其想法是不是围绕原点而是围绕该参考点执行旋转。我们可以使用以下方法简单地实现这一点:

    def rotate(self,theta,pivot=None):
        if not pivot:
            pivot = Coord(0,0)
        return (self - pivot)._rotate_from_origin(theta) + pivot

就是这样!现在您可以对任何点或一组点使用此方法。

示例

让我们试试你的例子(或者至少是我对它的理解)。

startA = Coord([0,-1],[ 0,-1])  # points that will be rotated with an angle of thetaA
startB = Coord([3,2],[-1,0])  # will be rotated with an angle of thetaB
pivot = Coord(1,1)
thetaA = -np.pi / 4
thetaB = np.pi / 4

## Rotate each set of points with the corresponding angle
rotatedA = startA.rotate(thetaA,pivot=pivot)
rotatedB = startB.rotate(thetaB,pivot=pivot)

## Display the result
plt.plot(pivot.x,pivot.y,'ko')
plt.plot(startA.x,startA.y,'o',c='gold')
plt.plot(rotatedA.x,rotatedA.y,c='gold')
plt.plot(startB.x,startB.y,c='lightblue')
plt.plot(rotatedB.x,rotatedB.y,c='royalblue')

results

注意这里的 theta 参数是一个整数。在这种情况下,每个点都将使用相同的 theta 值旋转。但是可以使用多个 theta 值来使每个点旋转不同。例如,我们可以这样做:

starts = Coord([0,3,[0,0])
pivot = Coord(1,1)
thetas = (np.pi / 4) * np.array([-1,1,1])

rotated = starts.rotate(thetas,pivot=pivot)

这同样适用于 pivot 参数。基本上这是将函数与 numpy 一起使用时的预期行为。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res