jgtrosh

cubstuff.py

Sep 8th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 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.     vertices = [
  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.     for n, i in enumerate(vertices):
  54.         vertices[n] = Point3D(i[0], i[1], i[2])
  55.         bones = [
  56.             [1, 5,], [4, 5,], [5, 6,], [0, 1,],
  57.             [1, 2,], [2, 3,], [3, 0,], [6, 7,],
  58.             [7, 4,], [0, 4,], [2, 6,], [3, 7,],
  59.             [8, 9, curses.COLOR_RED], [8, 10, curses.COLOR_RED], [8, 11, curses.COLOR_RED]
  60.         ]
  61.         triangles = [
  62.             [4, 5, 6], [4, 6, 7], [0, 1, 2], [0, 2, 3]
  63.         ]
  64.         angleX, angleY, angleZ = 0, 0, 0
  65.     from operator import itemgetter
  66.     while True:
  67.         scr.erase()
  68.         sx = 40
  69.         sy = 40
  70.         plots = []
  71.         vd = {}
  72.         for n, v in enumerate(vertices):
  73.             r = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ)
  74.             p = r.project(sx, sy, 90, 10)
  75.             plots.append(point(round(p.x), round(p.y)))
  76.             vd[n] = math.sqrt(r.x**2+r.y**2+(r.z+10)**2)
  77.         vds = [i[0] for i in sorted(vd.items(), key=itemgetter(1))][:7]
  78.         c = "*"
  79.         for b in bones:
  80.             p0 = plots[b[0]]
  81.             p1 = plots[b[1]]
  82.             if b[0] in vds and b[1] in vds:
  83.                 x0 = p0.x; y0 = p0.y
  84.                 x1 = p1.x; y1 = p1.y
  85.                 if x0 < 0 or x1 < 0 or y0 < 0 or y1 < 0:
  86.                     return
  87.                 if x1 == x0:
  88.                     for y in range(int(min(y0, y1)), int(max(y0, y1))):
  89.                         if len(b) == 3:
  90.                             scr.addstr(y, int(x0*2), c*2, b[2])
  91.                         else:
  92.                             scr.addstr(y, int(x0*2), c*2)
  93.                     continue
  94.                 s = float(y1-y0)/(x1-x0)
  95.                 if s < -1 or s > 1:
  96.                     if y0 > y1:
  97.                         x0, x1 = x1, x0
  98.                         y0, y1 = y1, y0
  99.                     s = 1/s
  100.                     for y in range(int(y0), int(y1)+1):
  101.                         x = x0+s*(y-y0)
  102.                         if len(b) == 3:
  103.                             scr.addstr(y, int(x*2), c*2, b[2])
  104.                         else:
  105.                             scr.addstr(y, int(x*2), c*2)
  106.                 else:
  107.                     if x0 > x1:
  108.                         x0, x1 = x1, x0
  109.                         y0, y1 = y1, y0
  110.                     for x in range(int(x0), int(x1+1)):
  111.                         y = y0+s*(x-x0)
  112.                         if len(b) == 3:
  113.                             scr.addstr(y, int(x*2), c*2, b[2])
  114.                         else:
  115.                             scr.addstr(y, int(x*2), c*2)
  116.                 scr.addstr(y0, x0*2, "  ", curses.A_REVERSE)
  117.                 scr.addstr(y1, x1*2, "  ", curses.A_REVERSE)
  118.         c = scr.getch()
  119.         if c == 258:
  120.             angleX -= 5
  121.         elif c == 259:
  122.             angleX += 5
  123.         elif c == 260:
  124.             angleY += 5
  125.         elif c == 261:
  126.             angleY -= 5
  127.         elif c == 48:
  128.             angleX, angleY, angleZ = 0, 0, 0
  129.         elif c == 114:
  130.             angleZ -= 5
  131.         #print(c)
  132.         scr.refresh()
  133.  
  134. curses.wrapper(run)
Advertisement
Add Comment
Please, Sign In to add comment