给定两个顶点绕中心点旋转线

2023-12-22

我一直在尝试将一堆线旋转 90 度(它们一起形成多段线)。每条线包含两个顶点,例如 (x1, y1) 和 (x2, y2)。我目前正在尝试做的是绕线的中心点旋转,给定中心点 |x1 - x2|和|y1 - y2|。由于某种原因(我不太懂数学)我无法让线条正确旋转。

有人可以验证这里的数学是否正确吗?我认为这可能是正确的,但是,当我将线的顶点设置为新的旋转顶点时,下一行可能不会从上一行获取新的 (x2, y2) 顶点,导致线旋转不正确。

这是我写的:

def rotate_lines(self, deg=-90):
    # Convert from degrees to radians
    theta = math.radians(deg)

    for pl in self.polylines:
        self.curr_pl = pl
        for line in pl.lines:
            # Get the vertices of the line
            # (px, py) = first vertex
            # (ox, oy) = second vertex
            px, ox = line.get_xdata()
            py, oy = line.get_ydata()

            # Get the center of the line
            cx = math.fabs(px-ox)
            cy = math.fabs(py-oy)

            # Rotate line around center point
            p1x = cx - ((px-cx) * math.cos(theta)) - ((py-cy) * math.sin(theta))
            p1y = cy - ((px-cx) * math.sin(theta)) + ((py-cy) * math.cos(theta))

            p2x = cx - ((ox-cx) * math.cos(theta)) - ((oy-cy) * math.sin(theta))
            p2y = cy - ((ox-cx) * math.sin(theta)) + ((oy-cy) * math.cos(theta))

            self.curr_pl.set_line(line, [p1x, p2x], [p1y, p2y])

点 (x1,y1) 和 (x2,y2) 之间的线段的中心点 (cx,cy) 坐标为:

    cx = (x1 + x2) / 2
    cy = (y1 + y2) / 2

换句话说,它只是两对 x 和 y 坐标值的平均值或算术平均值。

对于多段线或多段线,其逻辑中心点的 x 和 y 坐标只是所有点的 x 和 y 值的相应平均值。平均值只是值的总和除以它们的数量。

一般公式为旋转 2D 点 https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions(x,y) θ 弧度围绕原点(0,0) 是:

    x′ = x * cos(θ) - y * sin(θ)
    y′ = x * sin(θ) + y * cos(θ)

要绕不同的中心(cx,cy)执行旋转,需要通过首先从该点的坐标中减去所需旋转中心的坐标来调整该点的x和y值,这具有移动的效果(已知在几何学中为翻译 https://en.wikipedia.org/wiki/Translation_(geometry))它的数学表达如下:

    tx = x - cx
    ty = y - cy

然后将该中间点旋转所需的角度,最后添加旋转点的 x 和 y 值back每个坐标的 x 和 y。用几何术语来说,它是以下操作序列:Tʀᴀɴsʟᴀᴛᴇ ─►Rᴏᴛᴀᴛᴇ ─►Uɴᴛʀᴀɴsʟᴀᴛᴇ。

这个概念可以扩展到允许围绕任何任意点(例如其自己的逻辑中心)旋转整个多段线,只需将描述的数学应用到其中每个线段的每个点即可。

为了简化该计算的实现,可以将所有三组计算的数值结果组合起来并用一对同时执行它们的数学公式来表示。因此,可以通过使用以下方法将现有点 (x,y) 绕点 (cx, cy) 旋转 θ 弧度来获得新点 (x′,y′):

    x′ = (  (x - cx) * cos(θ) + (y - cy) * sin(θ) ) + cx
    y′ = ( -(x - cx) * sin(θ) + (y - cy) * cos(θ) ) + cy

将此数学/几何概念合并到您的函数中会产生以下结果:

from math import sin, cos, radians

def rotate_lines(self, deg=-90):
    """ Rotate self.polylines the given angle about their centers. """
    theta = radians(deg)  # Convert angle from degrees to radians
    cosang, sinang = cos(theta), sin(theta)

    for pl in self.polylines:
        # Find logical center (avg x and avg y) of entire polyline
        n = len(pl.lines)*2  # Total number of points in polyline
        cx = sum(sum(line.get_xdata()) for line in pl.lines) / n
        cy = sum(sum(line.get_ydata()) for line in pl.lines) / n

        for line in pl.lines:
            # Retrieve vertices of the line
            x1, x2 = line.get_xdata()
            y1, y2 = line.get_ydata()

            # Rotate each around whole polyline's center point
            tx1, ty1 = x1-cx, y1-cy
            p1x = ( tx1*cosang + ty1*sinang) + cx
            p1y = (-tx1*sinang + ty1*cosang) + cy
            tx2, ty2 = x2-cx, y2-cy
            p2x = ( tx2*cosang + ty2*sinang) + cx
            p2y = (-tx2*sinang + ty2*cosang) + cy

            # Replace vertices with updated values
            pl.set_line(line, [p1x, p2x], [p1y, p2y])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

给定两个顶点绕中心点旋转线 的相关文章

随机推荐

  • 在 UITableViewCell 中使用 AlamofireImage

    我读了很多answers https stackoverflow com questions 16663618 async image loading from url inside a uitableview cell image cha
  • xml 文件的根目录为 NONE 为什么?

    from elementtree import ElementTree as ET tree ET parse r N myinternwork files xml of bus systems testonieeebus xml r ro
  • __init__ 方法获取一个值,但没有传递任何值[重复]

    这个问题在这里已经有答案了 我有课TextBuffer 它有一个 init 函数可用于传递内容的初始值TextBuffer 如果没有传递任何值 则应使用空列表作为默认值 该类有一个方法add 为了将文本添加到 TextBuffer a Te
  • 如何在启动 Django 服务器后实例化一个类并稍后在views.py 中访问其成员

    我想定义一个类PacketCount在姜戈 class PacketCount def init self count pkts 0 def count pkts logic to count the number of packets 我
  • 元素是块级还是内联级?

    我在某处读到过 img 元素的行为与两者类似 如果正确的话 有人可以举例说明吗 确实 它们都是 或者更准确地说 它们是 内联块 元素 这意味着它们像文本一样内联流动 但也像块元素一样具有宽度和高度 在 CSS 中 您可以将元素设置为disp
  • 对空选择执行命令

    我正在开发一个专门的文本 HTML 编辑器 在 contenteditable div 中使用 Javascript 和 JQuery 我使用 execcommand 实现了不同的文本样式 粗体 斜体 这似乎仅在所选文本不为空时才有效 解决
  • 正则表达式包装报价

    我试图在 CSV 文件中的某些内容部分加上引号 当前布局如下所示 element1 element2 element3 element4 element5 element6 element7 element8 element9 elemen
  • Visual Studio 2012 Express for Desktop 有哪些安装程序选项

    因此 微软发布了用于桌面应用程序的 Visual Studio 2012 Express 这很好 但是如何为 Visual Studio Express 中构建的开源应用程序创建安装程序呢 默认情况下没有可用的安装程序模板 并且它们已禁用浏
  • 当我选择图像时,我的应用程序崩溃了?

    我对 Android Studio 完全陌生 我一直在尝试制作一个使用此链接给出的 scanLibrary 进行扫描的应用程序 并按照此处给出的说明进行操作https github com jhansireddy AndroidScanne
  • 如何创建用于运行时排序的表达式树?

    使用实体框架 4 我尝试根据成员名称集合实现动态排序 基本上 用户可以选择要排序的字段以及排序的顺序 我看过表达式树示例 但无法将其拼凑在一起 以下是一些细节 列名集合 public List
  • NodeJS:这段代码是否可以多核运行?

    我使用这个节点脚本作为我的项目的 运行程序 需要同时启动 停止三个脚本 现在我想知道从节点进程内部生成的 child process 是否会使用我的服务器拥有的多核 我有 90 的信心认为是 但安全总比抱歉好 var CP require
  • 如何在 C 中声明常量函数指针数组?

    我需要声明一个指向函数的指针数组 如下所示 extern void function1 void extern void function2 void void MESSAGE HANDLERS void function1 functio
  • Blob 不可写

    当我尝试使用 azure 进行 azure db 备份时rm command New AzureRmSqlDatabaseExport 我面临以下问题 Error encountered during the service operati
  • 使用 cordova 设置 android project.properties 文件中的值

    TL DR 我有一个针对 iOS 和 Android 的 cordova 应用程序 有没有办法使用config xml设置 cordova 生成的文件中的值platforms android project properties 更多细节
  • 将数据源设置为 MediaPlayer 中的原始 ID

    在 MediaPlayer create 方法中 可以使用原始文件的 id 但如何在 setDataSource 方法中使用它 参考源码android media MediaPlayer http grepcode com file rep
  • 使用 Instagram gem 获取所有用户的照片

    我想使用 Instagram gem 获取我的所有照片 https github com Instagram instagram ruby gem https github com Instagram instagram ruby gem
  • ios 6 uiwebview 使用 jquery scrolltop 滚动页面时显示背景

    我有一个网页显示在 UIWebView 中 该网页使用 jquery scrollTop 使用 1 6 4 版本的 jquery 在页面加载后将窗口滚动到指定位置 这在 4 5 之前的 xcode 上工作得很好 在我使用 xcode 4 5
  • Delphi 中的 System.IsConsole 什么时候为真?

    根据 System pas 它被记录为True if compiled as console app 在System pas中 还有一个赋值 IFDEF LINUX IsConsole True 我有一个奇怪的问题 使用 ShellExec
  • sqlalchemy 中的 python 继承

    所以我对 python 和 sqlalchemy 很陌生 我需要一些继承或混合 而不是继承 方面的帮助 我有一些伪代码 但我还没有真正取得任何进展 Base declarative base class ModelBase Base Bas
  • 给定两个顶点绕中心点旋转线

    我一直在尝试将一堆线旋转 90 度 它们一起形成多段线 每条线包含两个顶点 例如 x1 y1 和 x2 y2 我目前正在尝试做的是绕线的中心点旋转 给定中心点 x1 x2 和 y1 y2 由于某种原因 我不太懂数学 我无法让线条正确旋转 有