#!/usr/bin/env python
import Tix,math,tkFont
class Punkt:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def text(self):
return "[%9.4f,%9.4f]"%(self.x,self.y)
class Appn(Tix.Frame):
def in_range_angle(self,sw,ew,w):
if w<0: w+=360
if sw <= w <= ew: return True
sw,ew=sw-360,ew-360
if sw <= w <= ew: return True
return False
def search_int(self,x1,y1,r1,sw1,ew1,x2,y2,r2,sw2,ew2):
distsq = float(( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ))
sc1 = ( distsq - ( r2 * r2 - r1 * r1 ) ) / distsq
if not distsq: return []
root = float(2*( r1**2 + r2**2 ) * distsq - distsq**2 - ( r1 - r2 )**2 * ( r1 + r2 )**2)
sc1 = ( distsq - ( r2 * r2 - r1 * r1 ) ) / distsq
ret=[]
sqd=math.sqrt(distsq)
if sqd==(r1+r2) or sqd==abs(r1-r2):
x=x1+sc1*(x2-x1)/2
y=y1+sc1*(y2-y1)/2
if self.in_range_angle(sw1,ew1,math.atan2(y1-y,x-x1)) or \
self.in_range_angle(sw2,ew2,math.atan2(y-y2,x-x2)): ret.append([x,y])
elif sqd<(r1+r2) and sqd>abs(r1-r2):
sc2=math.sqrt(root)/distsq
x=x1+sc1*(x2-x1)/2-sc2*(y2-y1)/2
y=y1+sc1*(y2-y1)/2+sc2*(x2-x1)/2
if self.in_range_angle(sw1,ew1,math.degrees(math.atan2(y1-y,x-x1))) and \
self.in_range_angle(sw2,ew2,math.degrees(math.atan2(y2-y,x-x2))): ret.append([x,y])
x=x1+sc1*(x2-x1)/2+sc2*(y2-y1)/2
y=y1+sc1*(y2-y1)/2-sc2*(x2-x1)/2
if self.in_range_angle(sw1,ew1,math.degrees(math.atan2(y1-y,x-x1))) and \
self.in_range_angle(sw2,ew2,math.degrees(math.atan2(y2-y,x-x2))): ret.append([x,y])
return ret
def PushPop(self, event, v, b):
self.move[v]=b
def MouseMove(self, event):
if self.move[0]:
n=self.c.coords('arc1')
mx=n[0]+75;my=n[1]+75
w=math.atan2(event.y-my,event.x-mx)
newposy=math.sin(w)*75+my
newposx=math.cos(w)*75+mx
self.c.coords('a11',newposx-4,newposy-4,newposx+4,newposy+4)
self.c.itemconfig('arc1',start=-math.degrees(w))
w2=w-math.radians(float(self.c.itemcget('arc1','extent')))
newposy=math.sin(w2)*75+my
newposx=math.cos(w2)*75+mx
self.c.coords('a12',newposx-4,newposy-4,newposx+4,newposy+4)
if self.move[1]:
n=self.c.coords('arc1')
mx=n[0]+75;my=n[1]+75
w=math.atan2(event.y-my,event.x-mx)
newposy=math.sin(w)*75+my
newposx=math.cos(w)*75+mx
self.c.coords('a12',newposx-4,newposy-4,newposx+4,newposy+4)
w=360-(float(self.c.itemcget('arc1','start'))-math.degrees(-w))%360
self.c.itemconfig('arc1',extent=w)
if self.move[2]:
n=self.c.coords('arc2')
mx=n[0]+100;my=n[1]+100
w=math.atan2(event.y-my,event.x-mx)
newposy=math.sin(w)*100+my
newposx=math.cos(w)*100+mx
self.c.coords('a21',newposx-4,newposy-4,newposx+4,newposy+4)
self.c.itemconfig('arc2',start=-math.degrees(w))
w2=w-math.radians(float(self.c.itemcget('arc2','extent')))
newposy=math.sin(w2)*100+my
newposx=math.cos(w2)*100+mx
self.c.coords('a22',newposx-4,newposy-4,newposx+4,newposy+4)
if self.move[3]:
n=self.c.coords('arc2')
mx=n[0]+100;my=n[1]+100
w=math.atan2(event.y-my,event.x-mx)
newposy=math.sin(w)*100+my
newposx=math.cos(w)*100+mx
self.c.coords('a22',newposx-4,newposy-4,newposx+4,newposy+4)
w=360-(float(self.c.itemcget('arc2','start'))-math.degrees(-w))%360
self.c.itemconfig('arc2',extent=w)
if self.move[4]:
nx=event.x;ny=event.y
m=self.c.coords('mi1')
p1=self.c.coords('a11')
p2=self.c.coords('a12')
w=math.radians(float(self.c.itemcget('arc1','start')))
w1=w-math.radians(float(self.c.itemcget('arc1','extent')))
p1x=p1[0]-m[0]+nx
p1y=p1[1]-m[1]+ny
p2x=p2[0]-m[0]+nx
p2y=p2[1]-m[1]+ny
self.c.coords('arc1',nx-75,ny-75,nx+75,ny+75)
self.c.coords('mi1',nx-4,ny-4,nx+4,ny+4)
self.c.coords('a11',p1x-4,p1y-4,p1x+4,p1y+4)
self.c.coords('a12',p2x-4,p2y-4,p2x+4,p2y+4)
if self.move[5]:
nx=event.x;ny=event.y
m=self.c.coords('mi2')
p1=self.c.coords('a21')
p2=self.c.coords('a22')
w=math.radians(float(self.c.itemcget('arc2','start')))
w1=w-math.radians(float(self.c.itemcget('arc2','extent')))
p1x=p1[0]-m[0]+nx
p1y=p1[1]-m[1]+ny
p2x=p2[0]-m[0]+nx
p2y=p2[1]-m[1]+ny
self.c.coords('arc2',nx-100,ny-100,nx+100,ny+100)
self.c.coords('mi2',nx-4,ny-4,nx+4,ny+4)
self.c.coords('a21',p1x-4,p1y-4,p1x+4,p1y+4)
self.c.coords('a22',p2x-4,p2y-4,p2x+4,p2y+4)
sw1=float(self.c.itemcget('arc1','start'))
ew1=sw1+float(self.c.itemcget('arc1','extent'))
sw2=float(self.c.itemcget('arc2','start'))
ew2=sw2+float(self.c.itemcget('arc2','extent'))
n=self.c.coords('arc1')
n2=self.c.coords('arc2')
w=self.search_int(n[0]+75,n[1]+75,75,sw1,ew1,n2[0]+100,n2[1]+100,100,sw2,ew2)
if len(w)==1:
self.c.itemconfig('c1',state=Tix.NORMAL)
self.c.coords('c1',w[0][0]-4,w[0][1]-4,w[0][0]+4,w[0][1]+4)
self.c.itemconfig('c2',state=Tix.HIDDEN)
elif len(w)==2:
self.c.itemconfig('c1',state=Tix.NORMAL)
self.c.coords('c1',w[0][0]-4,w[0][1]-4,w[0][0]+4,w[0][1]+4)
self.c.itemconfig('c2',state=Tix.NORMAL)
self.c.coords('c2',w[1][0]-4,w[1][1]-4,w[1][0]+4,w[1][1]+4)
else:
self.c.itemconfig('c1',state=Tix.HIDDEN)
self.c.itemconfig('c2',state=Tix.HIDDEN)
def __init__(self,master=None):
Tix.Frame.__init__(self,master)
self.pack()
self.c=Tix.Canvas(width=400,height=400,bg="white")
self.c.create_arc(80,125,230,275,tag='arc1',style=Tix.ARC,start=90,extent=180)
self.c.create_arc(160,100,360,300,tag='arc2',style=Tix.ARC,start=90,extent=180)
self.c.create_oval(155-4,125-4,155+4,125+4,fill="green",tag="a11")
self.c.create_oval(155-4,275-4,155+4,275+4,fill="green",tag="a12")
self.c.create_oval(155-4,200-4,155+4,200+4,fill="cyan",tag="mi1")
self.c.create_oval(260-4,200-4,260+4,200+4,fill="cyan",tag="mi2")
self.c.create_oval(260-4,100-4,260+4,100+4,fill="yellow",tag="a21")
self.c.create_oval(260-4,300-4,260+4,300+4,fill="yellow",tag="a22")
self.c.create_oval(299-4,101-4,299+4,101+4,fill="red",tag="c1",state=Tix.HIDDEN)
self.c.create_oval(210-4,200-4,210+4,200+4,fill="red",tag="c2",state=Tix.HIDDEN)
self.c.tag_bind('a11', '<ButtonRelease-1>', lambda e,s=self:s.PushPop(e,0,False))
self.c.tag_bind('a11', '<Button-1>', lambda e,s=self:s.PushPop(e,0,True))
self.c.bind("<Motion>",self.MouseMove)
self.c.tag_bind('a12', '<ButtonRelease-1>', lambda e,s=self:s.PushPop(e,1,False))
self.c.tag_bind('a12', '<Button-1>', lambda e,s=self:s.PushPop(e,1,True))
self.c.tag_bind('a21', '<ButtonRelease-1>', lambda e,s=self:s.PushPop(e,2,False))
self.c.tag_bind('a21', '<Button-1>', lambda e,s=self:s.PushPop(e,2,True))
self.c.tag_bind('a22', '<ButtonRelease-1>', lambda e,s=self:s.PushPop(e,3,False))
self.c.tag_bind('a22', '<Button-1>', lambda e,s=self:s.PushPop(e,3,True))
self.c.tag_bind('mi1', '<ButtonRelease-1>', lambda e,s=self:s.PushPop(e,4,False))
self.c.tag_bind('mi1', '<Button-1>', lambda e,s=self:s.PushPop(e,4,True))
self.c.tag_bind('mi2', '<ButtonRelease-1>', lambda e,s=self:s.PushPop(e,5,False))
self.c.tag_bind('mi2', '<Button-1>', lambda e,s=self:s.PushPop(e,5,True))
self.c.pack()
self.move=[False,False,False,False,False,False]
root=Tix.Tk()
app=Appn(master=root)
app.master.title("TEST")
app.mainloop()