Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- from Tkinter import Tk, Canvas
- from math import cos, sin, sqrt, radians, pi
- from time import sleep
- import copy
- from Point import Point
- NUM = 8
- # RADIUS = 50
- FACE_LEN = 7
- HEIGHT = 225
- DISTANCE = 500
- # VIEWPOINT = Point(0.001,0.001, 500)
- VIEWPOINT = Point(45,45, 90)
- PERSPECTIVE = 0
- class App:
- def __init__(self, master):
- self.master = master
- self.width = {"start": 0, "end": 1366}
- self.height = {"start": 0, "end": 768}
- self.canvas = Canvas(master)
- self.canvas.configure({"bg": "white"})
- self.canvas.pack({"fill": "both", "expand": True})
- self.__generate_cylinder()
- # for i in self.obj:
- # print i
- # print
- self.__generate_multipl_matrix()
- self.rotate()
- if PERSPECTIVE:
- self.__is_perspective()
- self.__mooving()
- self.draw()
- self.__bindings()
- def rotate(self):
- obj = self.obj
- for i in xrange(0, len(obj)):
- obj[i] = obj[i].mult(self.RotationMatrix)
- for i in xrange(0, len(self.axes)):
- self.axes[i] = self.axes[i].mult(self.RotationMatrix)
- def draw(self):
- N = len(self.obj)/2
- for i in xrange(0,N-1):
- # bottom lines
- self.canvas.create_line(self.obj[i].x, self.obj[i].y,
- self.obj[i+1].x, self.obj[i+1].y,
- tag="line")
- # cross lines
- self.canvas.create_line(self.obj[i].x, self.obj[i].y,
- self.obj[i+N].x, self.obj[i+N].y,
- tag="line")
- for i in xrange((len(self.obj)/2), len(self.obj)-1):
- # top lines
- self.canvas.create_line(self.obj[i].x, self.obj[i].y,
- self.obj[i+1].x, self.obj[i+1].y,
- tag="line")
- # drawing axes =)
- for i in xrange(0,3):
- self.canvas.create_line(self.width['end']/2, self.height['end']/2,
- self.axes[i].x, self.axes[i].y,
- tag="axe")
- def __mooving(self):
- w = self.width["end"]
- h = self.height["end"]
- for i in xrange(0,len(self.obj)):
- self.obj[i].SetX(self.obj[i].x + w/2)
- self.obj[i].SetY(h/2 - self.obj[i].y)
- for i in xrange(0,len(self.axes)):
- self.axes[i].SetX(self.axes[i].x + w/2)
- self.axes[i].SetY(h/2 - self.axes[i].y)
- def __is_perspective(self):
- for i in xrange(0, len(self.obj)):
- self.obj[i].SetX(self.obj[i].x * (DISTANCE / self.obj[i].z))
- self.obj[i].SetY(self.obj[i].y * (DISTANCE / self.obj[i].z))
- for i in xrange(0, len(self.axes)):
- self.axes[i].SetX(self.axes[i].x * (DISTANCE / self.axes[i].z))
- self.axes[i].SetY(self.axes[i].y * (DISTANCE / self.axes[i].z))
- def __generate_cylinder(self):
- self.obj = []
- center = 0
- RADIUS = (FACE_LEN*NUM)/(2/pi)
- for i in xrange(0, NUM+1):
- self.obj.append(
- Point(
- RADIUS*sin((i - 1) * 2 * pi / NUM), RADIUS*cos((i - 1) * 2 * pi / NUM), 0
- )
- )
- for i in xrange(0, NUM+1):
- self.obj.append(
- Point(
- RADIUS*sin((i - 1) * 2 * pi / NUM), RADIUS*cos((i - 1) * 2 * pi / NUM), HEIGHT
- )
- )
- self.__generate_axes()
- def __generate_axes(self):
- self.axes = []
- self.axes.append(Point(500,0,0))
- self.axes.append(Point(0,500,0))
- self.axes.append(Point(0,0,500))
- def __generate_multipl_matrix(self):
- absV = sqrt(VIEWPOINT.x**2 + VIEWPOINT.y**2)
- absE = sqrt(VIEWPOINT.x**2 + VIEWPOINT.y**2 + VIEWPOINT.z**2)
- cosTheta = VIEWPOINT.x / absV
- sinTheta = VIEWPOINT.y / absV
- cosPhi = VIEWPOINT.z / absE
- sinPhi = absV / absE
- self.RotationMatrix = [
- [-sinTheta, cosTheta * cosPhi, -cosTheta * sinPhi, 0],
- [ cosTheta, sinTheta * cosPhi, -sinTheta * sinPhi, 0],
- [ 0, sinPhi, cosPhi, 0],
- [ 0, 0, absE, 1]
- ]
- def __update_coords(self, event):
- pass
- def __bindings(self):
- # binding master window resize
- self.master.bind("<Configure>", self.__update_coords)
- # self.canvas.bind("<Button-1>", self.__draw_loop)
- root = Tk()
- root.title("MachineGraphics. Lab3. Variant 2")
- root.geometry("1366x768+300+200")
- if not PERSPECTIVE:
- VIEWPOINT.SetZ(-VIEWPOINT.z)
- app = App(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement