Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_torus.py
- from tkinter import *
- from math import sqrt,sin, cos,pi
- def torus(x,y,z,angle):
- angle=float(angle)*pi/180
- x1=cos(angle)*x-sin(angle)*y
- y1=sin(angle)*x+cos(angle)*y
- z1=z
- return x1,y1,z1
- def project(x,y,z):
- X=xA+(y-x)*sqrt(3)/2
- Y=yA-z+(x+y)/2
- return X,Y
- def mix (a,d,g,b,e,h,c,f,i):
- return a*e*i+b*f*g+c*d*h-c*e*g-f*h*a-i*b*d
- def normal(a,b,c):
- return sqrt(a*a+b*b+c*c)
- def circle(slices):
- delta= 360/slices
- for j in range(0,slices):
- for i in range(0,slices):
- angle=float(i*delta)*pi/180
- x=200+50*cos(angle)
- y=0
- z=0+50*sin(angle)
- x,y,z=torus(x,y,z,delta*j)
- X,Y=project(x,y,z)
- x2,y2,z2=torus(x,y,z,delta)
- X2,Y2=project(x2,y2,z2)
- angle=float((i+1)*delta)*pi/180
- x1=200+50*cos(angle)
- y1=0
- z1=0+50*sin(angle)
- x1,y1,z1=torus(x1,y1,z1,delta*j)
- X1,Y1=project(x1,y1,z1)
- x3,y3,z3=torus(x1,y1,z1,delta)
- X3,Y3=project(x3,y3,z3)
- u,v,w=0,1,1
- level=mix(u,x2-x,x1-x,v,y2-y,y1-y,w,z2-z,z1-z)/(normal(u,v,w)*normal(x2-x,y2-y,z2-z)*normal(x1-x,y1-y,z1-z))*255
- if level<0:
- color="#000000"
- else:
- level=int(level)
- if level<16:
- color="#00000"+hex(level)[-1:]
- else:
- color="#0000"+hex(level)[-2:]
- level1=mix(u,x1-x3,x2-x3,v,y1-y3,y2-y3,w,z1-z3,z2-z3)/(normal(u,v,w)*normal(x2-x3,y2-y3,z2-z3)*normal(x1-x3,y1-y3,z1-z3))*255
- if level1<0:
- color1="#000000"
- else:
- level1=int(level1)
- if level1<16:
- color1="#00000"+hex(level1)[-1:]
- else:
- color1="#0000"+hex(level1)[-2:]
- if mix(1,x2-x,x1-x,1,y2-y,y1-y,1,z2-z,z1-z)>=0:
- #canvas.create_line(X,Y,X1,Y1,width=2, fill="darkgrey")
- #canvas.create_line(X,Y,X2,Y2,width=2, fill="red")
- canvas.create_polygon(X,Y,X2,Y2,X1,Y1,fill=color)
- canvas.create_polygon(X2,Y2,X3,Y3,X1,Y1,fill=color1)
- def axis():
- X,Y=project(0,0,0)
- X1,Y1=project(size*1.5,0,0)
- canvas.create_line(X,Y,X1,Y1,width=2, fill="blue")
- canvas.create_text(X1,Y1-15,text='X',fill='white',font='Arial 15')
- X1,Y1=project(0,size*1.5,0)
- canvas.create_line(X,Y,X1,Y1,width=2, fill="blue")
- canvas.create_text(X1,Y1-15,text='Y',fill='white',font='Arial 15')
- X1,Y1=project(0,0,size*1.5)
- canvas.create_line(X,Y,X1,Y1,width=2, fill="blue")
- canvas.create_text(X1+15,Y1,text='Z',fill='white',font='Arial 15')
- xA,yA=450,350
- size=200
- root= Tk()
- root.title("TORUS" )
- canvas=Canvas(root, bg="grey", width=900, height=700)
- canvas.grid(row=1,column=1)
- canvas.create_text(200,100,text='TORUS',fill='white',font='Arial 60 bold')
- btn = Button(root,text='Quitt', width =8, command=root.destroy).grid(row=2,column=1)
- axis()
- circle(180)
- X,Y=project(0,0,0)
- X1,Y1=project(0,0,size*1.5)
- canvas.create_line(X,Y,X1,Y1,width=2, fill="red")
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement