-
Notifications
You must be signed in to change notification settings - Fork 0
/
3dreflection.py
82 lines (68 loc) · 2.16 KB
/
3dreflection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from tkinter import *
from math import sqrt
points=[]
def convert3Dto2D(x,y,z):
z=z/sqrt(2)
x=ox+(x-z)
y=oy-(y-z)
return x,y
def drawcube(points3d,outline='blue'):
for i in range (len(points3d)):
points.insert(i,convert3Dto2D(points3d[i][0],points3d[i][1],points3d[i][2]))
win.create_text(points[i],text=str(i))
win.create_rectangle(points[0],points[2],outline=outline)
win.create_rectangle(points[4],points[6],outline=outline)
win.create_line(points[0],points[4],fill=outline)
win.create_line(points[1],points[5],fill=outline)
win.create_line(points[2],points[6],fill=outline)
win.create_line(points[3],points[7],fill=outline)
def reflection(x,y,z):
Refy= [[1, 0, 0, 0],
[0, -1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]
P = [[x],
[y],
[z],
[1]]
resulty= [[0],
[0],
[0],
[0]]
for i in range(len(Refy)):
# iterate through columns of P
for j in range(len(P[0])):
# iterate through rows of P
for k in range(len(P)):
resulty[i][j] += Refy[i][k] * P[k][j]
return resulty[0][0],resulty[1][0],resulty[2][0]
print("Please Enter the width,height and depth of cube")
w,h,d=map(int,input().split())
print ("Please Enter the center coordinates xc yc")
xc,yc=map(int,input().split())
zc=0
master=Tk()
canvas_width=master.winfo_screenwidth()
canvas_height=master.winfo_screenheight()
win=Canvas(master,width=canvas_width,height=canvas_height)
win.pack()
#origin
ox,oy=canvas_width/2,canvas_height/2
#axes
win.create_line(ox,0,ox,oy)
win.create_line(ox,oy,ox*2,oy)
win.create_line(ox,oy,0,oy*2)
#cube points
points3d=[(0,0,0),(w,0,0),(w,h,0),(0,h,0),(0,0,d),(w,0,d),(w,h,d),(0,h,d)]
points3dnew=[]
for i in range(len(points3d)):
points3dnew.insert(i,(points3d[i][0]+xc,points3d[i][1]+yc,points3d[i][2]+zc))
drawcube(points3dnew)
win.create_text(points[1],text="Original cube")
#calculating points after reflection about y axis
pointsx=[]
for i in range(len(points)):
pointsx.insert(i,reflection(points3dnew[i][0],points3dnew[i][1],points3dnew[i][2]))
drawcube(pointsx,'blue')
win.create_text(points[1],text="cube after reflection about y axis")
mainloop()