jgtrosh

curse_cube.py

Sep 9th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import curses
  4. from curses import wrapper
  5. import sys, math, os
  6. from time import sleep
  7.  
  8. class point:
  9.     def __init__(self, x, y):
  10.         self.x = x
  11.         self.y = y
  12.  
  13. class Point3D:
  14.     def __init__(self, x = 0, y = 0, z = 0):
  15.         self.x, self.y, self.z = float(x), float(y), float(z)
  16.  
  17.     def rotateX(self, angle):
  18.         rad = angle * math.pi / 180
  19.         cosa = math.cos(rad)
  20.         sina = math.sin(rad)
  21.         y = self.y * cosa - self.z * sina
  22.         z = self.y * sina + self.z * cosa
  23.         return Point3D(self.x, y, z)
  24.  
  25.     def rotateY(self, angle):
  26.         rad = angle * math.pi / 180
  27.         cosa = math.cos(rad)
  28.         sina = math.sin(rad)
  29.         z = self.z * cosa - self.x * sina
  30.         x = self.z * sina + self.x * cosa
  31.         return Point3D(x, self.y, z)
  32.  
  33.     def rotateZ(self, angle):
  34.         rad = angle * math.pi / 180
  35.         cosa = math.cos(rad)
  36.         sina = math.sin(rad)
  37.         x = self.x * cosa - self.y * sina
  38.         y = self.x * sina + self.y * cosa
  39.         return Point3D(x, y, self.z)
  40.  
  41.     def project(self, win_width, win_height, fov, viewer_distance):
  42.         factor = fov / (viewer_distance + self.z)
  43.         x = self.x * factor + win_width / 2
  44.         y = -self.y * factor + win_height / 2
  45.         return Point3D(x, y, 1)
  46.  
  47. def run(scr):
  48.     verticeso = [
  49.         [-1, 1,-1], [ 1, 1,-1], [ 1,-1,-1], [-1,-1,-1],
  50.         [-1, 1, 1], [ 1, 1, 1], [ 1,-1, 1], [-1,-1, 1],
  51.         [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]
  52.     ]
  53.     vertices = verticeso[:]
  54.     for n, i in enumerate(vertices):
  55.         vertices[n] = Point3D(i[0], i[1], i[2])
  56.     bones = [
  57.         [1, 5,], [4, 5,], [5, 6,], [0, 1,],
  58.         [1, 2,], [2, 3,], [3, 0,], [6, 7,],
  59.         [7, 4,], [0, 4,], [2, 6,], [3, 7,],
  60.         [8, 9, "."], [8, 10, "."], [8, 11, "."]
  61.     ]
  62.     triangles = [
  63.         [4, 5, 6], [4, 6, 7], [0, 1, 2], [0, 2, 3]
  64.     ]
  65.     angleX, angleY, angleZ = 0, 0, 0
  66.     from operator import itemgetter
  67.     while True:
  68.         scr.erase()
  69.         sx = 35
  70.         sy = 35
  71.         plots = []
  72.         vd = {}
  73.         for n, v in enumerate(vertices):
  74. #           r = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ)
  75.             p = v.project(sx, sy, 90, 10)
  76.             plots.append(point(round(p.x), round(p.y)))
  77.             vd[n] = math.sqrt(v.x**2+v.y**2+(v.z+10)**2)
  78.         vds = [i[0] for i in sorted(vd.items(), key=itemgetter(1))][:-1]
  79.         for b in bones:
  80.             p0 = plots[b[0]]
  81.             p1 = plots[b[1]]
  82.             c = "."
  83.             if b[0] in vds and b[1] in vds:
  84.                 c = "*"
  85.             x0 = p0.x; y0 = p0.y
  86.             x1 = p1.x; y1 = p1.y
  87.             if x0 < 0 or x1 < 0 or y0 < 0 or y1 < 0:
  88.                 continue
  89.             if x1 == x0:
  90.                 for y in range(int(min(y0, y1)), int(max(y0, y1))):
  91.                     if len(b) == 3:
  92.                         scr.addstr(y, int(x0*2), b[2]*2)
  93.                     else:
  94.                         scr.addstr(y, int(x0*2), c*2)
  95.                 continue
  96.             s = float(y1-y0)/(x1-x0)
  97.             if s < -1 or s > 1:
  98.                 if y0 > y1:
  99.                     x0, x1 = x1, x0
  100.                     y0, y1 = y1, y0
  101.                 s = 1/s
  102.                 for y in range(int(y0), int(y1)+1):
  103.                     x = x0+s*(y-y0)
  104.                     if len(b) == 3:
  105.                         scr.addstr(y, int(x*2), b[2]*2)
  106.                     else:
  107.                         scr.addstr(y, int(x*2), c*2)
  108.             else:
  109.                 if x0 > x1:
  110.                     x0, x1 = x1, x0
  111.                     y0, y1 = y1, y0
  112.                 for x in range(int(x0), int(x1+1)):
  113.                     y = y0+s*(x-x0)
  114.                     if len(b) == 3:
  115.                         scr.addstr(y, int(x*2), b[2]*2)
  116.                     else:
  117.                         scr.addstr(y, int(x*2), c*2)
  118.             scr.addstr(y0, x0*2, "  ", curses.A_REVERSE)
  119.             scr.addstr(y1, x1*2, "  ", curses.A_REVERSE)
  120.         c = scr.getch()
  121.         if c == 258:
  122.             for n, v in enumerate(vertices):
  123.                 vertices[n] = v.rotateX(-5)
  124.             angleX -= 5
  125.         elif c == 259:
  126.             for n, v in enumerate(vertices):
  127.                 vertices[n] = v.rotateX(5)
  128.             angleX += 5
  129.         elif c == 260:
  130.             for n, v in enumerate(vertices):
  131.                 vertices[n] = v.rotateY(5)
  132.             angleY += 5
  133.         elif c == 261:
  134.             for n, v in enumerate(vertices):
  135.                 vertices[n] = v.rotateY(-5)
  136.             angleY -= 5
  137.         elif c == 48:
  138.             vertices = verticeso[:]
  139.             for n, i in enumerate(vertices):
  140.                 vertices[n] = Point3D(i[0], i[1], i[2])
  141.             angleX, angleY, angleZ = 0, 0, 0
  142.         elif c == 114:
  143.             for n, v in enumerate(vertices):
  144.                 vertices[n] = v.rotateZ(-5)
  145.             angleZ -= 5
  146.         #print(c)
  147.         scr.refresh()
  148.  
  149. curses.wrapper(run)
Advertisement
Add Comment
Please, Sign In to add comment