def initUI(self): self.parent.title("Shapes") self.pack(fill=BOTH, expand=1) canvas = Canvas(self) canvas.create_oval(10, 10, 80, 80, outline="red", fill="green", width=2) #Here the create_oval() method is used to create a circle item. #The first four parameters are the bounding box coordinates of the circle. #In other words, they are x and y coordinates of the top-left and bottom-right points of the box, #in which the circle is drawn. canvas.create_oval(110, 10, 210, 80, outline="#f11", fill="#1f1", width=2) canvas.create_rectangle(230, 10, 290, 60, outline="#f11", fill="#1f1", width=2) #We create a rectangle item. #The coordinates are again the bounding box of the rectangle to be drawn. canvas.create_arc(30, 200, 90, 100, start=0, extent=210, outline="#f11", fill="#1f1", width=2) #This code line creates an arc. #An arc is a part of the circumference of the circle. #We provide the bounding box. #The start parameter is the start angle of the arc. #The extent is the angle size. points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200] canvas.create_polygon(points, outline='red', fill='green', width=2) #A polygon is created. #It is a shape with multiple corners. #To create a polygon in Tkinter, we provide the list of polygon coordinates to the create_polygon() method. canvas.pack(fill=BOTH, expand=1)
def initUI(self): self.parent.title("Shapes") self.pack(fill=BOTH, expand=1) canvas = Canvas(self) canvas.create_oval(10, 10, 80, 80, outline="green", fill="red", width=2) canvas.create_oval(110, 10, 210, 80, outline="green", fill="red", width=2) canvas.create_rectangle(230, 10, 290, 60, outline="green", fill="red", width=2) canvas.create_arc(30, 200, 90, 100, start=0, extent=210, outline="green", fill="red", width=2) points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200] canvas.create_polygon(points, outline="green", fill="red", width=2) canvas.pack(fill=BOTH, expand=1)
def initUI(self): self.parent.title("Shapes") self.pack(fill=BOTH, expand=1) canvas = Canvas(self) canvas.create_oval(10, 10, 80, 80, outline="gray", fill="gray", width=2) canvas.create_oval(110, 10, 210, 80, outline="gray", fill="gray", width=2) canvas.create_rectangle(230, 10, 290, 60, outline="gray", fill="gray", width=2) canvas.create_arc(30, 200, 90, 100, start=0, extent=210, outline="gray", fill="gray", width=2) points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200] canvas.create_polygon(points, outline='gray', fill='gray', width=2) canvas.pack(fill=BOTH, expand=1)
class Board: def __init__(self, width, height, title = "Boids"): """ A board for boids to explore """ from Tkinter import Tk, Canvas, Toplevel self.colors = ["white", "black", "red", "yellow", "blue", "green", "purple", "pink", "cyan", "turquoise", "gray"] self.width = width self.height = height self.distance = 10 self.boids = [] self.oldVector = [] self.title = title self.app = Tk() self.app.withdraw() self.win = Toplevel() self.win.wm_title(title) self.canvas = Canvas(self.win, width=self.width, height=self.height) self.canvas.pack(side = 'bottom', expand = "yes", anchor = "n", fill = 'both') self.win.winfo_toplevel().protocol('WM_DELETE_WINDOW',self.close) #self.canvas.bind("<Configure>", self.changeSize) self.draw() def close(self): """ close the window """ self.app.destroy() def draw(self): """ Initialize the board. You must do this if you ever want to see the path """ print "Drawing...", self.canvas.delete("boid") for boid in self.boids: self.drawBoid( boid ) self.canvas.update() print "Done!" def drawBoid(self, boid): size = 40 angle = 80 x = boid.x - size/2 y = boid.y - size/2 start = ((boid.dir + 180 + angle/2) - 55) % 360 color = self.colors[boid.color] self.canvas.create_arc(x, y, x + size, y + size, start = start, extent = angle/2, fill = color, outline = color, tag = "boid") def addBoid(self, boid): self.boids.append( boid ) self.oldVector.append( 0 ) self.draw() def dist(self, x1, y1, x2, y2): return math.sqrt( (x1 - x2) ** 2 + (y1 - y2) ** 2) def angleTo(self, b1, b2): # figure out angle from boid1 to boid2 # this is just a test! if self.boids[b1].x < self.boids[b2].x: return -10 # return - if to the right, + if to the left else: return 10 def avoid(self, num, radius): for i in range(len(self.boids)): if i != num: if self.dist(self.boids[i].x, self.boids[i].y, self.boids[num].x, self.boids[num].y ) < radius: return -self.angleTo( num, i) return 0.0 def copy(self, num, radius): # if within radius, get closer to their angle # return - if to the right, + if to the left return 0 def center(self, num, radius): # make yoru angle head toward center # return - if to the right, + if to the left return 0 def view(self, num, radius): # try to have a clear view # return - if to the right, + if to the left return 0 def adjustDirections(self, weights): radius = 40 for boidNum in range(len(self.boids)): avoidVector = self.avoid( boidNum, radius ) copyVector = self.copy( boidNum, radius ) centerVector = self.center( boidNum, radius ) viewVector = self.view( boidNum, radius ) newVector = int(weights[0] * avoidVector + weights[1] * copyVector + weights[2] * centerVector + weights[3] * viewVector) self.boids[boidNum].dir += newVector self.oldVector[boidNum] = newVector def move(self): """ Make the boids move """ self.adjustDirections([1., 1., 1., 1.]) # pass in weights: avoid, copy, center, view for boid in self.boids: boid.x += self.distance * math.cos( boid.dir / 180.0 * math.pi) boid.y -= self.distance * math.sin( boid.dir / 180.0 * math.pi) if boid.x > self.width: boid.x = 0 if boid.x < 0: boid.x = self.width - 1 if boid.y > self.height: boid.y = 0 if boid.y < 0: boid.y = self.height - 1 self.draw()
def gates(inp): l=[] l=inp.split('+') print(l) n=len(l) orr=n-1 nd=0 temp=[0]*n d=OrderedDict() for i in range(n): if(len(l[i])>1): nd+=len(l[i])-1 d[i+1]=len(l[i])-1 s='' for j in l[i]: if(j!=l[i][-1:]): s+=j+'AND' s+=l[i][-1:] temp[i]=s else: temp[i]=l[i] # print(d) print dictionary with index->no.of ands needed! val=temp[-1:][0] s='' for i in temp: # if(i!=val): s+='('+i+')'+' OR ' s=s[:len(s)-4] print('OR--'+str(orr)+' AND--'+str(nd)) print(s) data,data1,data2,data3=[],[],[],[] for j in l: if(len(j)==1): data.append(j) if(len(j)==2): data1.append(j) if(len(j)==3): data2.append(j) if(len(j)==4): data3.append(j) root=Tk() c = Canvas(master=root, width=500, height=450, bd=0, highlightthickness=0) c.master.title('Gates Representation') temp=orr x1,p1=165,70 y1,q1=100,100 x2,p2=195,100 y2,q2=100,100 # c=Canvas(root) # t=Text(root) c.create_rectangle(20,20,33,45,fill='#3b5998') c.create_text(60,30,text=': AND') c.create_line(80,35,100,35) c.create_line(100,20,100,50) c.create_arc(100-4,28-8,100+19,30+20,start=220,extent=270,fill='black',style=tk.ARC) c.create_text(140,30,text=': OR ') while(orr>0): if(temp==orr): u=0 if(nd==0): y=data[u] u+=1 c.create_text(x1-20,y1,text=y,font=('Purisa',10)) data.remove(y) c.create_line(x1-15,y1,x2,y2) if(nd==0 or len(d)<2): y=data[u] u+=1 c.create_text(x1-20,y1+15,text=y,font=('Purisa',10)) data.remove(y) c.create_line(x1-15,y1+15,x2,y2+15) c.create_line(x2,y1-8,x2,y2+24) c.create_arc(x2-4,y1-8,x2+19,y2+20,start=220,extent=270,fill='black',style=tk.ARC) c.create_line(x2+22,y1+10,x2+40,y1+10) if(orr!=1): c.create_line(x2+40,y1+10,x2+40,y1+50) y1+=50 y2+=50 orr-=1 u+=1 c.pack() else: x2+=50 x1+=50 u=0 c.create_line(x1-10,y1,x2,y2) if(len(data)!=0 and orr==len(data)): print(data) y=data[u] c.create_text(x1-15,y1+15,text=y,font=('Purisa',10)) data.remove(y) elif(nd==0): y=data[u] c.create_text(x1-15,y1+15,text=y,font=('Purisa',10)) data.remove(y) u+=1 c.create_line(x1-10,y1+15,x2,y2+15) c.create_line(x2,y1-8,x2,y2+24) c.create_arc(x2-4,y1-8,x2+19,y2+20,start=220,extent=270,fill='black',style=tk.ARC) c.create_line(x2+22,y1+10,x2+40,y1+10) if(orr!=1): c.create_line(x2+40,y1+10,x2+40,y1+50) y1+=50 y2+=50 orr-=1 c.pack() temp1=nd cnt=1 for i in list(d.keys()): if(d[i]==1): u,v=0,0 y=data1[u] c.create_text(p1-5,q1,text=y[v],font=('Purisa',10)) v+=1 c.create_line(p1,q1,p2,q2) c.create_text(p1-5,q1+15,text=y[v],font=('Purisa',10)) c.create_line(p1,q1+15,p2,q2+15) c.create_line(p2,q1-8,p2,q2+24) c.create_rectangle(p2-1,q1-8,p2+20,q2+23,fill='#3b5998') if(temp1==nd): c.create_line(p2+20,q1,p2+65,q1) else: c.create_line(p2+20,q1,p2+(42*cnt),q1) c.create_line(p2+(42*cnt),q1,p2+(42*cnt),q1-36) q1+=50 q2+=50 nd-=1 cnt+=1 u+=1 data1.remove(y) c.pack() elif(d[i]==2): u,v=0,0 y=data2[u] c.create_text(p1-5,q1,text=y[v],font=('Purisa',10)) v+=1 c.create_line(p1,q1,p2,q2) c.create_text(p1-5,q1+9,text=y[v],font=('Purisa',10)) v+=1 print(y[v]) c.create_line(p1,q1+9,p2,q2+9) c.create_text(p1-5,q1+15,text=y[v],font=('Purisa',10)) c.create_line(p1,q1+18,p2,q2+18) c.create_line(p2,q1-8,p2,q2+24) c.create_rectangle(p2-1,q1-8,p2+20,q2+23,fill='#3b5998') if(temp1==nd): c.create_line(p2+20,q1,p2+50,q1) else: c.create_line(p2+20,q1,p2+(42*cnt),q1) c.create_line(p2+(42*cnt),q1,p2+(42*cnt),q1-36) q1+=50 q2+=50 nd-=1 cnt+=1 u+=1 data2.remove(y) c.pack() elif(d[i]==3): u,v=0,0 y=data3[u] c.create_text(p1-5,q1,text=y[v],font=('Purisa',5)) v+=1 c.create_line(p1,q1,p2,q2) c.create_text(p1-5,q1+5,text=y[v],font=('Purisa',5)) v+=1 c.create_line(p1,q1+5,p2,q2+5) c.create_text(p1-5,q1+10,text=y[v],font=('Purisa',5)) v+=1 c.create_line(p1,q1+10,p2,q2+10) c.create_text(p1-5,q1+15,text=y[v],font=('Purisa',5)) c.create_line(p1,q1+15,p2,q2+15) c.create_line(p2,q1-8,p2,q2+24) c.create_rectangle(p2-1,q1-8,p2+20,q2+23,fill='#3b5998') if(temp1==nd): c.create_line(p2+20,q1,p2+50,q1) else: c.create_line(p2+20,q1,p2+(42*cnt),q1) c.create_line(p2+(42*cnt),q1,p2+(42*cnt),q1-36) q1+=50 q2+=50 nd-=1 u+=1 data3.remove(y) cnt+=1 c.pack() c.create_text(215,430,text='Fig. Probable gate diagram',anchor='s') root.mainloop()
class Board: def __init__(self, width, height, title="Boids"): """ A board for boids to explore """ from Tkinter import Tk, Canvas, Toplevel self.colors = [ "white", "black", "red", "yellow", "blue", "green", "purple", "pink", "cyan", "turquoise", "gray" ] self.width = width self.height = height self.distance = 10 self.boids = [] self.oldVector = [] self.title = title self.app = Tk() self.app.withdraw() self.win = Toplevel() self.win.wm_title(title) self.canvas = Canvas(self.win, width=self.width, height=self.height) self.canvas.pack(side='bottom', expand="yes", anchor="n", fill='both') self.win.winfo_toplevel().protocol('WM_DELETE_WINDOW', self.close) #self.canvas.bind("<Configure>", self.changeSize) self.draw() def close(self): """ close the window """ self.app.destroy() def draw(self): """ Initialize the board. You must do this if you ever want to see the path """ print "Drawing...", self.canvas.delete("boid") for boid in self.boids: self.drawBoid(boid) self.canvas.update() print "Done!" def drawBoid(self, boid): size = 40 angle = 80 x = boid.x - size / 2 y = boid.y - size / 2 start = ((boid.dir + 180 + angle / 2) - 55) % 360 color = self.colors[boid.color] self.canvas.create_arc(x, y, x + size, y + size, start=start, extent=angle / 2, fill=color, outline=color, tag="boid") def addBoid(self, boid): self.boids.append(boid) self.oldVector.append(0) self.draw() def dist(self, x1, y1, x2, y2): return math.sqrt((x1 - x2)**2 + (y1 - y2)**2) def angleTo(self, b1, b2): # figure out angle from boid1 to boid2 # this is just a test! if self.boids[b1].x < self.boids[b2].x: return -10 # return - if to the right, + if to the left else: return 10 def avoid(self, num, radius): for i in range(len(self.boids)): if i != num: if self.dist(self.boids[i].x, self.boids[i].y, self.boids[num].x, self.boids[num].y) < radius: return -self.angleTo(num, i) return 0.0 def copy(self, num, radius): # if within radius, get closer to their angle # return - if to the right, + if to the left return 0 def center(self, num, radius): # make yoru angle head toward center # return - if to the right, + if to the left return 0 def view(self, num, radius): # try to have a clear view # return - if to the right, + if to the left return 0 def adjustDirections(self, weights): radius = 40 for boidNum in range(len(self.boids)): avoidVector = self.avoid(boidNum, radius) copyVector = self.copy(boidNum, radius) centerVector = self.center(boidNum, radius) viewVector = self.view(boidNum, radius) newVector = int(weights[0] * avoidVector + weights[1] * copyVector + weights[2] * centerVector + weights[3] * viewVector) self.boids[boidNum].dir += newVector self.oldVector[boidNum] = newVector def move(self): """ Make the boids move """ self.adjustDirections([1., 1., 1., 1. ]) # pass in weights: avoid, copy, center, view for boid in self.boids: boid.x += self.distance * math.cos(boid.dir / 180.0 * math.pi) boid.y -= self.distance * math.sin(boid.dir / 180.0 * math.pi) if boid.x > self.width: boid.x = 0 if boid.x < 0: boid.x = self.width - 1 if boid.y > self.height: boid.y = 0 if boid.y < 0: boid.y = self.height - 1 self.draw()