Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This comment was added automatically to allow this file to save.
- # You'll be able to remove it after adding text to the file.
- from math import *
- from kandinsky import *
- from ion import *
- # from nwahoo import hsv_to_rgb
- xmin=-5
- xmax=5
- ymin = -3.5
- ymax = 3.5
- #xmin=-4
- #xmax=4
- #ymin = -3
- #ymax = 3
- rangex = xmax - xmin
- rangey = ymax - ymin
- xscl = 320 / rangex
- yscl = -222 / rangey
- # fmatrix = [[0,0,0],[1,0,0],[1,2,0],[2,2,0],[2,3,0],[1,3,0],[1,4,0],
- # [3,4,0],[3,5,0],[0,5,0],
- # [0,0,1],[1,0,1],[1,2,1],[2,2,1],[2,3,1],[1,3,1],[1,4,1],
- # [3,4,1],[3,5,1],[0,5,1]]
- # edges = [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],
- # [7,8],[8,9],[9,0],
- # [10,11],[11,12],[12,13],[13,14],[14,15],[15,16],[16,17],
- # [17,18],[18,19],[19,10],
- # [0,10],[1,11],[2,12],[3,13],[4,14],[5,15],[6,16],[7,17],
- # [8,18],[9,19]]
- Adepth = 0.5
- Amatrix=[
- [-2,-2,Adepth],#z=0
- [-1,-2,Adepth],
- [-0.5,-0.5,Adepth],
- [0.5,-0.5,Adepth],
- [1,-2,Adepth],
- [2,-2,Adepth],
- [0.5,2,Adepth],
- [-0.5,2,Adepth],
- [-0.2,1,Adepth],
- [0.2,1,Adepth],
- [0.5,0,Adepth],
- [-0.5,0,Adepth],
- [-2,-2,-Adepth],#z =1
- [-1,-2,-Adepth],
- [-0.5,-0.5,-Adepth],
- [0.5,-0.5,-Adepth],
- [1,-2,-Adepth],
- [2,-2,-Adepth],
- [0.5,2,-Adepth],
- [-0.5,2,-Adepth],
- [-0.2,1,-Adepth],
- [0.2,1,-Adepth],
- [0.5,0,-Adepth],
- [-0.5,0,-Adepth],
- ]
- Aedges= [
- [0,1],#traits de la face avant
- [1,2],
- [2,3],
- [3,4],
- [4,5],
- [5,6],
- [6,7],
- [7,0],
- [8,9],
- [11,10],
- [8,11],
- [9,10],
- [0,12],#entre les z0 et z1
- [1,13],
- [2,14],
- [3,15],
- [4,16],
- [5,17],
- [6,18],
- [7,19],
- [8,20],
- [9,21],
- [10,22],
- [11,23],
- #traits de la afce arriere
- [12, 13], [13, 14], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [19, 12], [20, 21], [23, 22], [20, 23], [21, 22]
- ]
- #
- cote = 2
- cube = [
- [-cote, cote,-cote],
- [-cote,-cote,-cote],
- [ cote, cote,-cote],
- [ cote,-cote,-cote],
- [-cote, cote,cote],
- [-cote,-cote,cote],
- [ cote, cote,cote],
- [ cote,-cote,cote]
- ]
- edges = [
- [0,1],
- [1,3],
- [0,2],
- [2,3],
- [4,5],
- [4,6],
- [5,7],
- [7,6],
- [4,0],
- [6,2],
- [7,3],
- [1,5]
- ]
- def main():
- global xscl, yscl, edges, cube
- # edges = Aedges
- # cube = Amatrix
- c=0#compteur for colour
- col_fond = (0,0,0)
- col_cube = (0,0,255)
- fill_rect(0,0,320,222, col_fond)
- p_z=0.1#coeff for perspective
- def graphPoints1(pointList,edges, col):#used for normal drawing
- p_z=0.1
- for pt1,pt2 in edges:
- x1,y1,z1=pointList[pt1]
- x2,y2,z2=pointList[pt2]
- draw_line(
- int((x1-z1*p_z*x1)*xscl+160),int(111-(y1-z1*p_z*y1)*yscl),
- int((x2-z2*p_z*x2)*xscl+160),int(111-(y2-z2*p_z*y2)*yscl), col)
- # draw_line(
- # int((x1-z1*p_z*x1)*xscl+160)+1,int(111-(y1-z1*p_z*y1)*yscl)+1,
- # int((x2-z2*p_z*x2)*xscl+160)+1,int(111-(y2-z2*p_z*y2)*yscl)+1,col)
- def yo():
- mini = 10
- for x,y,z in cube:
- if mini>z:mini = z
- if 1<z<2:
- n = 4
- elif z<4:
- n= 2
- else:
- n = 1
- return n
- m = sorted(cube, key = lambda x:x[2])
- m = m[:n+1]
- def graphPoints2(pointList,edges, _col):#used for advanced drawing for cube
- #on cherche lindice du point le plus loin/we search the index of the furthest point to make it darker because not visible
- imax,max=0,pointList[0][2]
- for i in range(1,8):
- if pointList[i][2]>max:
- max,imax=pointList[i][2],i
- col2=hsv_to_rgb(c,1.0,0.4)
- #col2=(120,120,120)
- for pt1,pt2 in edges:
- if pt1==imax or pt2==imax:
- col=col2
- else:
- col=_col
- x1,y1,z1=pointList[pt1]
- x2,y2,z2=pointList[pt2]
- draw_line(
- int((x1-z1*p_z*x1)*xscl+160),int(111-(y1-z1*p_z*y1)*yscl),
- int((x2-z2*p_z*x2)*xscl+160),int(111-(y2-z2*p_z*y2)*yscl), col)
- # draw_line(
- # int((x1-z1*p_z*x1)*xscl+160)+1,int(111-(y1-z1*p_z*y1)*yscl)+1,
- # int((x2-z2*p_z*x2)*xscl+160)+1,int(111-(y2-z2*p_z*y2)*yscl)+1, col)
- def rottilt(rot,tilt):
- '''returns the matrix for rotating a number of degrees.'''
- rotmatrix_Y = [[cos(tilt),0.0,sin(tilt)],
- [0.0,1.0,0.0],
- [-sin(tilt),0.0,cos(tilt)]]
- rotmatrix_X = [[1.0,0.0,0.0],
- [0.0,cos(rot),sin(rot)],
- [0.0,-sin(rot),cos(rot)]]
- return multmatrix(rotmatrix_X,rotmatrix_Y)
- def multmatrix(a,b):
- n = len(a)
- m = len(b[0])
- newmatrix = [[0]*m for _ in range(n)]
- for i in range(n):
- #for every column in b
- for j in range(m):
- #for every element in the column
- for k in range(len(b)):
- newmatrix[i][j] += a[i][k]*b[k][j]
- return newmatrix
- def deplacement():
- pas_x, pas_z=0.1, 0.1
- if keydown(KEY_SIX):
- for i in range(len(cube)):
- cube[i][0]-=pas_x
- elif keydown(KEY_FOUR):
- for i in range(len(cube)):
- cube[i][0]+=pas_x
- elif keydown(KEY_LEFTPARENTHESIS):
- for i in range(len(cube)):
- cube[i][2]+=pas_z
- elif keydown(KEY_RIGHTPARENTHESIS):
- for i in range(len(cube)):
- cube[i][2]-=pas_z
- has_changed = True
- def hsv_to_rgb(h, s=1.0, v=1.0):
- h = (h%360)/360
- if s == 0.0: v*=255; return (v, v, v)
- i = int(h*6.) # XXX assume int() truncates!
- 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
- if i == 0: return (v, t, p)
- if i == 1: return (q, v, p)
- if i == 2: return (p, v, t)
- if i == 3: return (p, q, v)
- if i == 4: return (t, p, v)
- if i == 5: return (v, p, q)
- rot, tilt = pi, pi
- rot_matrix = rottilt(rot,tilt)
- newmatrix = multmatrix(cube,rot_matrix)
- graphPoints2(newmatrix,edges, col_cube)
- pas = 0.025#pour rot et tilt
- has_changed = False
- while True:
- if keydown(KEY_DOWN):
- tilt-=pas
- if tilt<=pas:
- tilt = 2*pi
- has_changed = True
- elif keydown(KEY_UP):
- tilt+=pas%(2*pi)
- has_changed = True
- elif keydown(KEY_LEFT):
- rot-=pas
- if rot<=pas:
- rot=2*pi
- has_changed = True
- elif keydown(KEY_RIGHT):
- rot+=pas%(2*pi)
- has_changed = True
- if keydown(KEY_HOME):
- mini = 10
- for x,y,z in cube:
- if mini>z:
- mini = z
- print(round(mini, 5))
- #deplacement()
- if has_changed:
- graphPoints1(newmatrix,edges, col_fond)
- has_changed=False
- rot_matrix = rottilt(tilt,rot)
- newmatrix = multmatrix(cube,rot_matrix)
- #couleur
- c+=1
- col_cube = hsv_to_rgb(c)
- # rot_matrix = rottilt(tilt,rot)
- # newmatrix = multmatrix(cube,rot_matrix)
- graphPoints2(newmatrix,edges, col_cube)
- # graphPoints1(newmatrix,edges, col_cube)
- main()
Advertisement
Add Comment
Please, Sign In to add comment