Guest User

Untitled

a guest
Dec 31st, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.44 KB | None | 0 0
  1. # This comment was added automatically to allow this file to save.
  2. # You'll be able to remove it after adding text to the file.
  3. from math import *
  4. from kandinsky import *
  5. from ion import *
  6. # from nwahoo import hsv_to_rgb
  7. xmin=-5
  8. xmax=5
  9. ymin = -3.5
  10. ymax = 3.5
  11.  
  12. #xmin=-4
  13. #xmax=4
  14. #ymin = -3
  15. #ymax = 3
  16.  
  17. rangex = xmax - xmin
  18. rangey = ymax - ymin
  19. xscl = 320 / rangex
  20. yscl = -222 / rangey
  21.  
  22. # fmatrix = [[0,0,0],[1,0,0],[1,2,0],[2,2,0],[2,3,0],[1,3,0],[1,4,0],
  23. #          [3,4,0],[3,5,0],[0,5,0],
  24. #     [0,0,1],[1,0,1],[1,2,1],[2,2,1],[2,3,1],[1,3,1],[1,4,1],
  25. #          [3,4,1],[3,5,1],[0,5,1]]
  26. # edges = [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],
  27. #         [7,8],[8,9],[9,0],
  28. #         [10,11],[11,12],[12,13],[13,14],[14,15],[15,16],[16,17],
  29. #         [17,18],[18,19],[19,10],
  30. #         [0,10],[1,11],[2,12],[3,13],[4,14],[5,15],[6,16],[7,17],
  31. #         [8,18],[9,19]]
  32. Adepth = 0.5
  33. Amatrix=[
  34.     [-2,-2,Adepth],#z=0
  35.     [-1,-2,Adepth],
  36.     [-0.5,-0.5,Adepth],
  37.     [0.5,-0.5,Adepth],
  38.     [1,-2,Adepth],
  39.     [2,-2,Adepth],
  40.     [0.5,2,Adepth],
  41.     [-0.5,2,Adepth],
  42.     [-0.2,1,Adepth],
  43.     [0.2,1,Adepth],
  44.     [0.5,0,Adepth],
  45.     [-0.5,0,Adepth],
  46.     [-2,-2,-Adepth],#z =1
  47.     [-1,-2,-Adepth],
  48.     [-0.5,-0.5,-Adepth],
  49.     [0.5,-0.5,-Adepth],
  50.     [1,-2,-Adepth],
  51.     [2,-2,-Adepth],
  52.     [0.5,2,-Adepth],
  53.     [-0.5,2,-Adepth],
  54.     [-0.2,1,-Adepth],
  55.     [0.2,1,-Adepth],
  56.     [0.5,0,-Adepth],
  57.     [-0.5,0,-Adepth],
  58. ]
  59. Aedges= [
  60.     [0,1],#traits de la face avant
  61.     [1,2],
  62.     [2,3],
  63.     [3,4],
  64.     [4,5],
  65.     [5,6],
  66.     [6,7],
  67.     [7,0],
  68.     [8,9],
  69.     [11,10],
  70.     [8,11],
  71.     [9,10],
  72.    
  73.     [0,12],#entre les z0 et z1
  74.     [1,13],
  75.     [2,14],
  76.     [3,15],
  77.     [4,16],
  78.     [5,17],
  79.     [6,18],
  80.     [7,19],
  81.     [8,20],
  82.     [9,21],
  83.     [10,22],
  84.     [11,23],
  85.    
  86.     #traits de la afce arriere
  87.     [12, 13], [13, 14], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [19, 12], [20, 21], [23, 22], [20, 23], [21, 22]
  88. ]
  89. #
  90. cote = 2
  91.  
  92. cube = [
  93.     [-cote, cote,-cote],
  94.     [-cote,-cote,-cote],
  95.     [ cote, cote,-cote],
  96.     [ cote,-cote,-cote],
  97.     [-cote, cote,cote],
  98.     [-cote,-cote,cote],
  99.     [ cote, cote,cote],
  100.     [ cote,-cote,cote]
  101. ]
  102. edges = [
  103.     [0,1],
  104.     [1,3],
  105.     [0,2],
  106.     [2,3],
  107.     [4,5],
  108.     [4,6],
  109.     [5,7],
  110.     [7,6],
  111.     [4,0],
  112.     [6,2],
  113.     [7,3],
  114.     [1,5]
  115. ]
  116.  
  117.  
  118. def main():
  119.     global xscl, yscl, edges, cube
  120.     # edges = Aedges
  121.     # cube = Amatrix
  122.     c=0#compteur for colour
  123.     col_fond = (0,0,0)
  124.     col_cube = (0,0,255)
  125.     fill_rect(0,0,320,222, col_fond)
  126.     p_z=0.1#coeff for perspective
  127.    
  128.     def graphPoints1(pointList,edges, col):#used for normal drawing
  129.         p_z=0.1
  130.         for pt1,pt2 in edges:
  131.             x1,y1,z1=pointList[pt1]
  132.             x2,y2,z2=pointList[pt2]
  133.             draw_line(
  134.                 int((x1-z1*p_z*x1)*xscl+160),int(111-(y1-z1*p_z*y1)*yscl),
  135.                 int((x2-z2*p_z*x2)*xscl+160),int(111-(y2-z2*p_z*y2)*yscl), col)
  136.             # draw_line(
  137.                 # int((x1-z1*p_z*x1)*xscl+160)+1,int(111-(y1-z1*p_z*y1)*yscl)+1,
  138.                 # int((x2-z2*p_z*x2)*xscl+160)+1,int(111-(y2-z2*p_z*y2)*yscl)+1,col)  
  139.     def yo():
  140.       mini = 10
  141.       for x,y,z in cube:
  142.         if mini>z:mini = z
  143.       if 1<z<2:
  144.         n = 4
  145.       elif z<4:
  146.         n= 2
  147.       else:
  148.         n = 1
  149.       return n
  150.       m = sorted(cube, key = lambda x:x[2])
  151.       m = m[:n+1]
  152.  
  153.     def graphPoints2(pointList,edges, _col):#used for advanced drawing for cube
  154.         #on cherche lindice du point le plus loin/we search the index of the furthest point to make it darker because not visible
  155.         imax,max=0,pointList[0][2]
  156.         for i in range(1,8):
  157.             if pointList[i][2]>max:
  158.                 max,imax=pointList[i][2],i
  159.        
  160.         col2=hsv_to_rgb(c,1.0,0.4)
  161.         #col2=(120,120,120)
  162.         for pt1,pt2 in edges:
  163.             if pt1==imax or pt2==imax:
  164.                 col=col2
  165.             else:
  166.                 col=_col
  167.             x1,y1,z1=pointList[pt1]
  168.             x2,y2,z2=pointList[pt2]
  169.             draw_line(
  170.                 int((x1-z1*p_z*x1)*xscl+160),int(111-(y1-z1*p_z*y1)*yscl),
  171.                 int((x2-z2*p_z*x2)*xscl+160),int(111-(y2-z2*p_z*y2)*yscl), col)
  172.             # draw_line(
  173.             #     int((x1-z1*p_z*x1)*xscl+160)+1,int(111-(y1-z1*p_z*y1)*yscl)+1,
  174.             #     int((x2-z2*p_z*x2)*xscl+160)+1,int(111-(y2-z2*p_z*y2)*yscl)+1, col)    
  175.     def rottilt(rot,tilt):
  176.         '''returns the matrix for rotating a number of degrees.'''
  177.        
  178.         rotmatrix_Y = [[cos(tilt),0.0,sin(tilt)],
  179.         [0.0,1.0,0.0],
  180.         [-sin(tilt),0.0,cos(tilt)]]
  181.        
  182.         rotmatrix_X = [[1.0,0.0,0.0],
  183.         [0.0,cos(rot),sin(rot)],
  184.         [0.0,-sin(rot),cos(rot)]]
  185.         return multmatrix(rotmatrix_X,rotmatrix_Y)
  186.     def multmatrix(a,b):
  187.         n = len(a)
  188.         m = len(b[0])
  189.         newmatrix = [[0]*m for _ in range(n)]
  190.         for i in range(n):
  191.             #for every column in b
  192.             for j in range(m):
  193.                 #for every element in the column
  194.                 for k in range(len(b)):
  195.                     newmatrix[i][j] += a[i][k]*b[k][j]
  196.         return newmatrix
  197.     def deplacement():
  198.         pas_x, pas_z=0.1, 0.1
  199.         if keydown(KEY_SIX):
  200.             for i in range(len(cube)):
  201.                 cube[i][0]-=pas_x
  202.         elif keydown(KEY_FOUR):
  203.             for i in range(len(cube)):
  204.                 cube[i][0]+=pas_x
  205.         elif keydown(KEY_LEFTPARENTHESIS):
  206.             for i in range(len(cube)):
  207.                 cube[i][2]+=pas_z
  208.         elif keydown(KEY_RIGHTPARENTHESIS):
  209.             for i in range(len(cube)):
  210.                 cube[i][2]-=pas_z
  211.         has_changed = True
  212.     def hsv_to_rgb(h, s=1.0, v=1.0):
  213.       h = (h%360)/360
  214.       if s == 0.0: v*=255; return (v, v, v)
  215.       i = int(h*6.) # XXX assume int() truncates!
  216.       f = (h*6.)-i; p,q,t = int(255*(v*(1.-s))), int(255*(v*(1.-s*f))), int(255*(v*(1.-s*(1.-f)))); v*=255; i%=6
  217.       if i == 0: return (v, t, p)
  218.       if i == 1: return (q, v, p)
  219.       if i == 2: return (p, v, t)
  220.       if i == 3: return (p, q, v)
  221.       if i == 4: return (t, p, v)
  222.       if i == 5: return (v, p, q)
  223.     rot, tilt = pi, pi
  224.     rot_matrix = rottilt(rot,tilt)
  225.     newmatrix = multmatrix(cube,rot_matrix)
  226.     graphPoints2(newmatrix,edges, col_cube)
  227.    
  228.     pas = 0.025#pour rot et tilt
  229.     has_changed = False
  230.    
  231.  
  232.     while True:
  233.       if keydown(KEY_DOWN):
  234.         tilt-=pas
  235.         if tilt<=pas:
  236.             tilt = 2*pi
  237.         has_changed = True
  238.       elif keydown(KEY_UP):
  239.         tilt+=pas%(2*pi)
  240.         has_changed = True
  241.       elif keydown(KEY_LEFT):
  242.         rot-=pas
  243.         if rot<=pas:
  244.             rot=2*pi
  245.         has_changed = True
  246.       elif keydown(KEY_RIGHT):
  247.         rot+=pas%(2*pi)
  248.         has_changed = True
  249.      
  250.       if keydown(KEY_HOME):
  251.         mini = 10
  252.         for x,y,z in cube:
  253.           if mini>z:
  254.             mini = z
  255.         print(round(mini, 5))
  256.  
  257.  
  258.  
  259.         #deplacement()
  260.        
  261.         if has_changed:
  262.           graphPoints1(newmatrix,edges, col_fond)
  263.           has_changed=False
  264.           rot_matrix = rottilt(tilt,rot)
  265.           newmatrix = multmatrix(cube,rot_matrix)
  266.        
  267.         #couleur
  268.         c+=1
  269.         col_cube = hsv_to_rgb(c)
  270.         # rot_matrix = rottilt(tilt,rot)
  271.         # newmatrix = multmatrix(cube,rot_matrix)
  272.         graphPoints2(newmatrix,edges, col_cube)
  273.         # graphPoints1(newmatrix,edges, col_cube)
  274.  
  275. main()
  276.  
Advertisement
Add Comment
Please, Sign In to add comment