ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 在给定两个顶点的中心点周围旋转线

2019-09-23 04:09:41  阅读:333  来源: 互联网

标签:python rotation trigonometry


我一直试图将一堆线旋转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值的对应平均值.平均值只是值的总和除以它们的数量.

原点(0,0)周围的rotate a 2D point(x,y)θ弧度的通式为:

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

要围绕不同的中心(cx,cy)执行旋转,需要通过首先从点的坐标减去所需旋转中心的坐标来调整点的x和y值,这具有移动的效果(已知)在几何中作为translating)它在数学上表达如下:

    tx = x - cx
    ty = y - cy

然后将该中间点旋转所需的角度,最后将旋转点的x和y值加回到每个坐标的x和y.从几何学角度来说,它是以下操作顺序:翻译→旋转→不翻译

这个概念可以扩展到允许围绕任意点旋转整条折线 – 例如它自己的逻辑中心 – 只需将描述的数学应用到其中每个线段的每个点.

为了简化该计算的实现,所有三组计算的数值结果可以组合并用一对数学公式表示,这些公式同时执行它们.因此,通过使用以下方法旋转点(cx,cy)周围的现有点(x,y),θ弧度,可以获得新点(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])

标签:python,rotation,trigonometry
来源: https://codeday.me/bug/20190923/1813708.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有