【发布时间】:2013-01-28 07:44:27
【问题描述】:
我一直在尝试将一堆线旋转 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])
【问题讨论】:
-
p2y公式对于第二个减号是错误的。 -
很好,谢谢。是一个错字!
-
您使用的是什么图形/几何模块?
-
@martineau Matplotlib 当时,我相信。
标签: python rotation trigonometry