Advertisement
here2share

# Tk_torus.py

Nov 16th, 2022
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. # Tk_torus.py
  2.  
  3. from tkinter import *
  4. from math import sqrt,sin, cos,pi
  5.  
  6. def torus(x,y,z,angle):
  7.     angle=float(angle)*pi/180
  8.     x1=cos(angle)*x-sin(angle)*y
  9.     y1=sin(angle)*x+cos(angle)*y
  10.     z1=z
  11.     return x1,y1,z1
  12.  
  13. def project(x,y,z):
  14.     X=xA+(y-x)*sqrt(3)/2
  15.     Y=yA-z+(x+y)/2
  16.     return X,Y
  17.  
  18. def mix (a,d,g,b,e,h,c,f,i):
  19.     return a*e*i+b*f*g+c*d*h-c*e*g-f*h*a-i*b*d
  20.  
  21. def normal(a,b,c):
  22.     return sqrt(a*a+b*b+c*c)
  23.  
  24. def circle(slices):
  25.     delta= 360/slices
  26.     for j in range(0,slices):
  27.         for i in range(0,slices):
  28.             angle=float(i*delta)*pi/180
  29.             x=200+50*cos(angle)
  30.             y=0
  31.             z=0+50*sin(angle)
  32.             x,y,z=torus(x,y,z,delta*j)
  33.             X,Y=project(x,y,z)
  34.            
  35.             x2,y2,z2=torus(x,y,z,delta)
  36.             X2,Y2=project(x2,y2,z2)
  37.            
  38.             angle=float((i+1)*delta)*pi/180
  39.             x1=200+50*cos(angle)
  40.             y1=0
  41.             z1=0+50*sin(angle)
  42.             x1,y1,z1=torus(x1,y1,z1,delta*j)
  43.             X1,Y1=project(x1,y1,z1)
  44.  
  45.             x3,y3,z3=torus(x1,y1,z1,delta)
  46.             X3,Y3=project(x3,y3,z3)
  47.  
  48.             u,v,w=0,1,1
  49.  
  50.             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
  51.             if level<0:
  52.                 color="#000000"
  53.             else:
  54.                 level=int(level)
  55.                 if level<16:
  56.                     color="#00000"+hex(level)[-1:]
  57.                 else:
  58.                     color="#0000"+hex(level)[-2:]
  59.                    
  60.             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
  61.             if level1<0:
  62.                 color1="#000000"
  63.             else:
  64.                 level1=int(level1)
  65.                 if level1<16:
  66.                     color1="#00000"+hex(level1)[-1:]
  67.                 else:
  68.                     color1="#0000"+hex(level1)[-2:]
  69.                    
  70.             if mix(1,x2-x,x1-x,1,y2-y,y1-y,1,z2-z,z1-z)>=0:
  71.                 #canvas.create_line(X,Y,X1,Y1,width=2, fill="darkgrey")
  72.                 #canvas.create_line(X,Y,X2,Y2,width=2, fill="red")
  73.                 canvas.create_polygon(X,Y,X2,Y2,X1,Y1,fill=color)
  74.                 canvas.create_polygon(X2,Y2,X3,Y3,X1,Y1,fill=color1)
  75.                
  76. def axis():
  77.     X,Y=project(0,0,0)
  78.     X1,Y1=project(size*1.5,0,0)
  79.     canvas.create_line(X,Y,X1,Y1,width=2, fill="blue")
  80.     canvas.create_text(X1,Y1-15,text='X',fill='white',font='Arial 15')
  81.     X1,Y1=project(0,size*1.5,0)
  82.     canvas.create_line(X,Y,X1,Y1,width=2, fill="blue")
  83.     canvas.create_text(X1,Y1-15,text='Y',fill='white',font='Arial 15')
  84.     X1,Y1=project(0,0,size*1.5)
  85.     canvas.create_line(X,Y,X1,Y1,width=2, fill="blue")
  86.     canvas.create_text(X1+15,Y1,text='Z',fill='white',font='Arial 15')
  87.    
  88.          
  89. xA,yA=450,350
  90. size=200
  91.  
  92. root= Tk()
  93. root.title("TORUS" )
  94. canvas=Canvas(root, bg="grey", width=900, height=700)
  95. canvas.grid(row=1,column=1)
  96. canvas.create_text(200,100,text='TORUS',fill='white',font='Arial 60 bold')
  97. btn = Button(root,text='Quitt', width =8, command=root.destroy).grid(row=2,column=1)
  98. axis()
  99. circle(180)
  100. X,Y=project(0,0,0)
  101. X1,Y1=project(0,0,size*1.5)
  102. canvas.create_line(X,Y,X1,Y1,width=2, fill="red")
  103. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement