■
お絵描きプログラム。
左ドラッグで線を描く、右ドラッグで回す。
三角関数ですよ!
…なんかよくわかんないから不思議なプログラムになってる気がするけど。
回転前。
回転後。
#!/usr/bin/env python import Tkinter import math class rollTest: def __init__(self): self.window = Tkinter.Tk() self.width = 800 self.height = 600 self.canvas = Tkinter.Canvas(self.window,bg="white", width=self.width,height=self.height) self.canvas.bind("<ButtonPress-1>",self.pressCanvas1) self.canvas.bind("<B1-Motion>",self.motionCanvas1) self.canvas.bind("<ButtonPress-2>",self.pressCanvas2) self.canvas.bind("<B2-Motion>",self.motionCanvas2) self.canvas.bind("<ButtonPress-3>",self.pressCanvas3) self.canvas.bind("<B3-Motion>",self.motionCanvas3) self.lines = [] self.canvas.pack() self.window.mainloop() def cnvMap(self,x,y): return (x-self.width/2,y*-1+self.height/2) def cnvCanvas(self,x,y): return (x+self.width/2,(y-self.height/2)*-1) def roll(self,x,y,degree): (orgX,orgY) = self.cnvMap(x,y) orgR = math.sqrt(math.pow(orgX,2)+math.pow(orgY,2)) orgTheta = math.atan2(orgY/orgR,orgX/orgR) theta = math.radians(degree)+orgTheta x = math.cos(theta)*orgR y = math.sin(theta)*orgR return self.cnvCanvas(x,y) def pressCanvas1(self,event): self.x = event.x self.y = event.y def pressCanvas2(self,event): pass def pressCanvas3(self,event): x,y = self.cnvMap(event.x,event.y) r = math.sqrt(math.pow(x,2)+math.pow(y,2)) self.rollOrgTheta = math.atan2(x/r,y/r) print self.rollOrgTheta def motionCanvas1(self,event): line = self.canvas.create_line(self.x,self.y,event.x,event.y, fill="red",width=5) self.lines.append((line,self.x,self.y,event.x,event.y)) self.x = event.x self.y = event.y def motionCanvas2(self,event): pass def motionCanvas3(self,event): x,y = self.cnvMap(event.x,event.y) r = math.sqrt(math.pow(x,2)+math.pow(y,2)) theta = self.rollOrgTheta - math.atan2(x/r,y/r) for ii in range(len(self.lines)): line,sx,sy,ex,ey = self.lines[ii] sx,sy = self.roll(sx,sy,math.degrees(theta)) ex,ey = self.roll(ex,ey,math.degrees(theta)) self.canvas.coords(line,sx,sy,ex,ey) self.lines[ii] = (line,sx,sy,ex,ey) self.rollOrgTheta = math.atan2(x/r,y/r) rollTest()