Advertisement
Bkmz

Untitled

Nov 28th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.06 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. from Tkinter import Tk, Canvas
  4. from math import cos, sin, sqrt, radians, pi
  5. from time import sleep
  6.  
  7. import copy
  8.  
  9. from Point import Point
  10.  
  11.  
  12. NUM         = 8
  13. # RADIUS      = 50
  14. FACE_LEN    = 7
  15. HEIGHT      = 225
  16. DISTANCE    = 500
  17. # VIEWPOINT   = Point(0.001,0.001, 500)
  18. VIEWPOINT   = Point(45,45, 90)
  19. PERSPECTIVE = 0
  20.  
  21.  
  22. class App:
  23.     def __init__(self, master):
  24.         self.master = master
  25.  
  26.         self.width  = {"start": 0, "end": 1366}
  27.         self.height = {"start": 0, "end": 768}
  28.  
  29.  
  30.         self.canvas = Canvas(master)
  31.        
  32.         self.canvas.configure({"bg": "white"})
  33.         self.canvas.pack({"fill": "both", "expand": True})
  34.  
  35.         self.__generate_cylinder()
  36.         # for i in self.obj:
  37.         #     print i
  38.         # print
  39.         self.__generate_multipl_matrix()
  40.         self.rotate()
  41.  
  42.         if PERSPECTIVE:
  43.             self.__is_perspective()
  44.  
  45.         self.__mooving()
  46.  
  47.  
  48.  
  49.         self.draw()
  50.  
  51.  
  52.         self.__bindings()
  53.  
  54.     def rotate(self):
  55.         obj = self.obj
  56.         for i in xrange(0, len(obj)):
  57.             obj[i] = obj[i].mult(self.RotationMatrix)
  58.  
  59.         for i in xrange(0, len(self.axes)):
  60.             self.axes[i] = self.axes[i].mult(self.RotationMatrix)
  61.  
  62.     def draw(self):
  63.         N = len(self.obj)/2
  64.         for i in xrange(0,N-1):
  65.             # bottom lines
  66.             self.canvas.create_line(self.obj[i].x, self.obj[i].y,
  67.                                     self.obj[i+1].x, self.obj[i+1].y,
  68.                                     tag="line")
  69.  
  70.             # cross lines
  71.             self.canvas.create_line(self.obj[i].x, self.obj[i].y,
  72.                                     self.obj[i+N].x, self.obj[i+N].y,
  73.                                     tag="line")
  74.            
  75.  
  76.         for i in xrange((len(self.obj)/2), len(self.obj)-1):
  77.             # top lines
  78.             self.canvas.create_line(self.obj[i].x, self.obj[i].y,
  79.                                     self.obj[i+1].x, self.obj[i+1].y,
  80.                                     tag="line")
  81.  
  82.        
  83.        
  84.            
  85.         # drawing axes =)
  86.         for i in xrange(0,3):
  87.             self.canvas.create_line(self.width['end']/2, self.height['end']/2,
  88.                                     self.axes[i].x, self.axes[i].y,
  89.                                             tag="axe")
  90.  
  91.                
  92.     def __mooving(self):
  93.         w = self.width["end"]
  94.         h = self.height["end"]
  95.         for i in xrange(0,len(self.obj)):
  96.             self.obj[i].SetX(self.obj[i].x + w/2)
  97.             self.obj[i].SetY(h/2 - self.obj[i].y)
  98.  
  99.         for i in xrange(0,len(self.axes)):
  100.             self.axes[i].SetX(self.axes[i].x + w/2)
  101.             self.axes[i].SetY(h/2 - self.axes[i].y)
  102.                        
  103.     def __is_perspective(self):
  104.         for i in xrange(0, len(self.obj)):
  105.             self.obj[i].SetX(self.obj[i].x * (DISTANCE / self.obj[i].z))
  106.             self.obj[i].SetY(self.obj[i].y * (DISTANCE / self.obj[i].z))
  107.        
  108.         for i in xrange(0, len(self.axes)):
  109.             self.axes[i].SetX(self.axes[i].x * (DISTANCE / self.axes[i].z))
  110.             self.axes[i].SetY(self.axes[i].y * (DISTANCE / self.axes[i].z))
  111.  
  112.  
  113.     def __generate_cylinder(self):
  114.         self.obj = []
  115.  
  116.         center = 0
  117.         RADIUS = (FACE_LEN*NUM)/(2/pi)
  118.         for i  in xrange(0, NUM+1):
  119.             self.obj.append(
  120.                 Point(
  121.                         RADIUS*sin((i - 1) * 2 * pi / NUM), RADIUS*cos((i - 1) * 2 * pi / NUM), 0
  122.                      )
  123.              )
  124.        
  125.         for i  in xrange(0, NUM+1):
  126.             self.obj.append(
  127.                 Point(
  128.                         RADIUS*sin((i - 1) * 2 * pi / NUM), RADIUS*cos((i - 1) * 2 * pi / NUM), HEIGHT
  129.                      )
  130.              )
  131.  
  132.         self.__generate_axes()
  133.  
  134.     def __generate_axes(self):
  135.         self.axes = []
  136.         self.axes.append(Point(500,0,0))
  137.         self.axes.append(Point(0,500,0))
  138.         self.axes.append(Point(0,0,500))
  139.  
  140.  
  141.  
  142.  
  143.     def __generate_multipl_matrix(self):
  144.         absV = sqrt(VIEWPOINT.x**2 + VIEWPOINT.y**2)
  145.         absE = sqrt(VIEWPOINT.x**2 + VIEWPOINT.y**2 + VIEWPOINT.z**2)
  146.         cosTheta = VIEWPOINT.x / absV
  147.         sinTheta = VIEWPOINT.y / absV
  148.         cosPhi   = VIEWPOINT.z / absE
  149.         sinPhi   = absV / absE
  150.  
  151.         self.RotationMatrix = [
  152.                 [-sinTheta, cosTheta * cosPhi, -cosTheta * sinPhi, 0],
  153.                 [ cosTheta, sinTheta * cosPhi, -sinTheta * sinPhi, 0],
  154.                 [        0,            sinPhi,             cosPhi, 0],
  155.                 [        0,                 0,               absE, 1]
  156.         ]
  157.  
  158.  
  159.     def __update_coords(self, event):
  160.         pass
  161.  
  162.  
  163.     def __bindings(self):
  164.         # binding master window resize
  165.         self.master.bind("<Configure>", self.__update_coords)
  166.  
  167.         # self.canvas.bind("<Button-1>", self.__draw_loop)
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. root = Tk()
  176. root.title("MachineGraphics. Lab3. Variant 2")
  177. root.geometry("1366x768+300+200")
  178.  
  179. if not PERSPECTIVE:
  180.     VIEWPOINT.SetZ(-VIEWPOINT.z)
  181.  
  182. app = App(root)
  183.  
  184. root.mainloop()
  185.  
  186.  
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement