如何旋转线不在其终点?

时间:2015-03-14 18:18:21

标签: python tkinter trigonometry image-rotation

我正在开发的应用程序中使用Pillow(PIL)进行一些图像旋转。不幸的是,我无法获得我想要旋转图像的性能。我将回到绘图板并尝试绘制线条并旋转它们。昨晚我根据Python 2.5 Graphics Cookbook一书中的一些代码将它放在一起。我希望有人可以告诉我如何旋转这条线不是在它的终点,而是在线的指定点。在这种情况下,圆圈的中心这是我的代码。

# rotate_line_1.py
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
from Tkinter import *
import math

class RotateLine(object):
    def __init__(self, master):

        self.cw = 430
        self.ch = 480

        self.chart_1 = Canvas(root, width=self.cw, height=self.ch, background='white' )
        self.chart_1.grid(row=0, column=0)

        self.counter = StringVar()
        self.counter.set('0')

        self.linewidth = 10

        self.o1 = self.chart_1.create_oval(1, 1, 430, 430, width=2, fill='#555555')
        self.o2 = self.chart_1.create_oval(15, 15, 420, 420, width=2, fill='black')
        self.a = self.chart_1.create_line(215, 20, 215, 215, width=self.linewidth, fill='red')


        self.p1_x = 214.9 # the pivot point
        self.p1_y = 20.0 # the pivot point,
        self.p2_x = 215.0 # the specific point to be rotated
        self.p2_y = 215.0 # the specific point to be rotated.

        self.a_radian = math.atan((self.p2_y - self.p1_y)/(self.p2_x - self.p1_x))
        self.a_length = math.sqrt((self.p2_y - self.p1_y)*(self.p2_y - self.p1_y) + (self.p2_x - self.p1_x)*(self.p2_x - self.p1_x))

        self.cycle_period = 0 # pause duration (milliseconds).
        self.x = 0

        self.buttonFw = Button(root, text="1 >>", command=lambda: self.rotate_line(1, 1))
        self.buttonBk = Button(root, text="<< 1", command=lambda: self.rotate_line(-1, 1))
        self.button45B = Button(root, text="<< 45", command=lambda: self.rotate_line(-1, 45))
        self.button45F = Button(root, text="45 >>", command=lambda: self.rotate_line(1, 45))
        self.button0 = Button(root, text="0", command=lambda: self.rotate_line(-1, 0))


        self.buttonBk.place(x=10, y=440)
        self.button45B.place(x=75, y=440)
        self.button45F.place(x=290, y=440)
        self.buttonFw.place(x=363, y=440)

        self.button0.place(x=198, y=440)

        self.clicks = 0
        print self.a_radian

    def rotate_line(self, direction, degree):
        self.chart_1.delete(self.a)
        if degree == 0:
            print 'Hello zero'
            self.chart_1.delete(self.a)
            self.a = self.chart_1.create_line(215, 20, 215, 215, width=self.linewidth, fill='red')
            self.a_radian = 1.57028350633
        else:
            self.a_radian += (.01747 * degree) * direction # incremental rotation with direction
            print self.a_radian
            self.p1_x = self.p2_x - self.a_length * math.cos(self.a_radian)
            self.p1_y = self.p2_y - self.a_length * math.sin(self.a_radian)
            self.i = self.chart_1.create_line(self.p1_x, self.p1_y, self.p2_x, self.p2_y, width=self.linewidth, fill='red')
            self.chart_1.update()
            self.chart_1.after(self.cycle_period)
            self.chart_1.delete(self.i)
            self.a = self.chart_1.create_line(self.p1_x, self.p1_y, self.p2_x, self.p2_y, width=self.linewidth, fill='red')

root = Tk()
root.title("Rotating line")
app = RotateLine(root)
root.mainloop()

如果你运行这个,你会在几个圆圈背景上得到一条线。按按钮可围绕圆心移动线条。

我想要做的是让线条更长但仍然在圆心处旋转。将代码中这两行中的行坐标更新为325。

self.a = self.chart_1.create_line(215, 20, 215, 325, width=self.linewidth,
                                  fill='red')

一旦线条更长,我怎样才能将旋转点保持在圆圈的中心并相应地重新绘制线条?

提前致谢,是的,我的三角技能很臭。

0 个答案:

没有答案