打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

我一直试图将一堆线旋转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, radiansdef 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])
来源:https://www.icode9.com/content-1-463701.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
java applet clock的实例代码
百度坐标坐标系之间的转换(JS版代码)
三维空间中直角坐标与球坐标的相互转换
双线性插值(Bilinear interpolation)的图像旋转在mobile上面的C++实现
机械制图教程第19讲-轴测图的基本知识
网页CAD开发引线标注的代码如何写?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服