Advertisement
Bkmz

Untitled

Sep 26th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3.  
  4. from Tkinter import Tk, Canvas
  5. from math import cos, sin, sqrt, radians
  6. from time import sleep
  7.  
  8.  
  9. class App:
  10.     def __init__(self, master):
  11.        
  12.         self.master = master
  13.        
  14.         self.width  = {"start": 0, "end": 640}
  15.         self.height = {"start": 0, "end": 480}
  16.        
  17.         self.canvas = Canvas(master)
  18.        
  19.         self.canvas.configure({"bg": "white"})
  20.         self.canvas.pack({"fill": "both", "expand": True})
  21.        
  22.         # widget sizes
  23.         w = self.width['end']
  24.         h = self.height['end']
  25.        
  26.         # define section =) #define Yes 1 #define No 0 ;-)
  27.         self.offset = 70
  28.         self.center = {"x": 0+self.offset, "y": h/2}
  29.        
  30.         self.radius      = 50
  31.         self.line_length = 23
  32.        
  33.        
  34.        
  35.         self.truck = [
  36.                  (-self.line_length,-self.radius),
  37.                  (+self.line_length,-self.radius),
  38.                  
  39.                  (+self.radius,-self.line_length),
  40.                  (+self.radius,+self.line_length),
  41.                  
  42.                  (+self.line_length,+self.radius),
  43.                  (-self.line_length,+self.radius),
  44.                  
  45.                  (-self.radius,+self.line_length),
  46.                  (-self.radius,-self.line_length),
  47.                  
  48.                  # double this point to close circuit
  49.                  (-self.line_length,-self.radius)
  50.                  
  51.                 ]
  52.        
  53.        
  54.         # Tk bindings
  55.         self.__bindings(master)
  56.        
  57.        
  58.         # need first run to redraw
  59.         self.__draw()
  60. #        self.__draw_loop()
  61.        
  62.        
  63.        
  64.        
  65.     """SLOT to update when window is resizing"""    
  66.     def __update_coords(self, event):
  67.         # bind event, to update coordinates
  68.         self.width['end']  = event.width
  69.         self.height['end'] = event.height
  70.        
  71. #        self.canvas.update_idletasks()
  72.        
  73.         self.__draw()
  74.        
  75.        
  76.     def __draw(self, event=None):
  77.         self.canvas.delete("all")
  78.         # widget sizes
  79.         w = self.width['end']
  80.         h = self.height['end']
  81.        
  82.         truck = self.truck
  83.         radius = self.radius
  84.         line_length = self.line_length
  85.         center = self.center
  86.         offset = self.offset
  87.        
  88.         for i in xrange(len(truck)):
  89.             # check the last item
  90.             try:
  91.                 if i == 0:
  92.                     color = "blue"
  93.                 else:
  94.                     color = "black"
  95.                 self.canvas.create_line(center['x']+truck[i][0], center['y']+truck[i][1],
  96.                                         center['x']+truck[i+1][0], center['y']+truck[i+1][1],
  97.                                         tag="border")
  98.                
  99.                 self.canvas.create_line(center['x'], center['y'],
  100.                                         center['x']+truck[i][0], center['y']+truck[i][1],
  101.                                         fill=color, tag="diagonal")
  102.             except:
  103.                 pass
  104.        
  105.                
  106.         # draw dash line.
  107.         self.canvas.create_line(
  108.                                 0+offset, h/2,
  109.                                 w-offset, h/2,
  110.                                 tags="line",
  111.                                 fill="blue",
  112.                                 width="2.0",
  113.                                 dash="5",
  114.                                 )
  115.        
  116.        
  117.     def __rotate_truck(self, degree=10):
  118.         for i in xrange(len(self.truck)):
  119.             self.truck[i] = self.__matrix_multiplication(self.truck[i], radians(degree))
  120.            
  121.     def __move_point(self, points=1):
  122.         self.center['x'] += points
  123.        
  124.        
  125.            
  126.            
  127.  
  128.    
  129.     def __draw_loop(self, event=None):
  130. #        sleep(5.50)
  131.         print "Run loop"
  132.        
  133.         while 1:
  134.            
  135.             self.canvas.delete("all || !line")
  136.             self.__rotate_truck(1.8)
  137.             self.__move_point(2)
  138.             self.__draw()
  139.             self.canvas.update_idletasks()
  140.             self.master.update_idletasks()
  141. #            sleep(0.01)
  142.            
  143.             if self.center['x']+self.offset >= self.width['end']:
  144. #                self.center['x'] = 0+self.offset
  145.                 break
  146.            
  147.            
  148.        
  149.        
  150.  
  151.        
  152.        
  153.        
  154.        
  155.        
  156.        
  157.     def __bindings(self, master):
  158.         # binding master window resize
  159. #        master.bind("<Configure>", self.__update_coords)
  160. #        master.bind("<Configure>", self.__draw)
  161.         self.canvas.bind("<Button-1>", self.__draw_loop)
  162.    
  163.     def __matrix_multiplication(self, point, degree):
  164.         a11 = point[0]
  165.         a12 = point[1]
  166.         a13 = 1
  167.        
  168.         b11 = cos(degree)
  169.         b12 = sin(degree)
  170.         b13 = 0
  171.        
  172.         b21 = -sin(degree)
  173.         b22 = cos(degree)
  174.         b23 = 0
  175.        
  176.         b31 = 0
  177.         b32 = 0
  178.         b33 = 1
  179.        
  180.         return a11*b11+a12*b21+a13*b31 ,  a11*b12+a12*b22+a13*b32  
  181.        
  182.  
  183. root = Tk()
  184. root.title("MachineGraphics. Lab2. Variant 2")
  185. root.geometry("640x480-0+20")
  186.  
  187. app = App(root)
  188.  
  189. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement