Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import curses
- from curses import wrapper
- import sys, math, os
- from time import sleep
- class point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- class Point3D:
- def __init__(self, x = 0, y = 0, z = 0):
- self.x, self.y, self.z = float(x), float(y), float(z)
- def rotateX(self, angle):
- rad = angle * math.pi / 180
- cosa = math.cos(rad)
- sina = math.sin(rad)
- y = self.y * cosa - self.z * sina
- z = self.y * sina + self.z * cosa
- return Point3D(self.x, y, z)
- def rotateY(self, angle):
- rad = angle * math.pi / 180
- cosa = math.cos(rad)
- sina = math.sin(rad)
- z = self.z * cosa - self.x * sina
- x = self.z * sina + self.x * cosa
- return Point3D(x, self.y, z)
- def rotateZ(self, angle):
- rad = angle * math.pi / 180
- cosa = math.cos(rad)
- sina = math.sin(rad)
- x = self.x * cosa - self.y * sina
- y = self.x * sina + self.y * cosa
- return Point3D(x, y, self.z)
- def project(self, win_width, win_height, fov, viewer_distance):
- factor = fov / (viewer_distance + self.z)
- x = self.x * factor + win_width / 2
- y = -self.y * factor + win_height / 2
- return Point3D(x, y, 1)
- def run(scr):
- verticeso = [
- [-1, 1,-1], [ 1, 1,-1], [ 1,-1,-1], [-1,-1,-1],
- [-1, 1, 1], [ 1, 1, 1], [ 1,-1, 1], [-1,-1, 1],
- [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]
- ]
- vertices = verticeso[:]
- for n, i in enumerate(vertices):
- vertices[n] = Point3D(i[0], i[1], i[2])
- bones = [
- [1, 5,], [4, 5,], [5, 6,], [0, 1,],
- [1, 2,], [2, 3,], [3, 0,], [6, 7,],
- [7, 4,], [0, 4,], [2, 6,], [3, 7,],
- [8, 9, "."], [8, 10, "."], [8, 11, "."]
- ]
- triangles = [
- [4, 5, 6], [4, 6, 7], [0, 1, 2], [0, 2, 3]
- ]
- angleX, angleY, angleZ = 0, 0, 0
- from operator import itemgetter
- while True:
- scr.erase()
- sx = 35
- sy = 35
- plots = []
- vd = {}
- for n, v in enumerate(vertices):
- # r = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ)
- p = v.project(sx, sy, 90, 10)
- plots.append(point(round(p.x), round(p.y)))
- vd[n] = math.sqrt(v.x**2+v.y**2+(v.z+10)**2)
- vds = [i[0] for i in sorted(vd.items(), key=itemgetter(1))][:-1]
- for b in bones:
- p0 = plots[b[0]]
- p1 = plots[b[1]]
- c = "."
- if b[0] in vds and b[1] in vds:
- c = "*"
- x0 = p0.x; y0 = p0.y
- x1 = p1.x; y1 = p1.y
- if x0 < 0 or x1 < 0 or y0 < 0 or y1 < 0:
- continue
- if x1 == x0:
- for y in range(int(min(y0, y1)), int(max(y0, y1))):
- if len(b) == 3:
- scr.addstr(y, int(x0*2), b[2]*2)
- else:
- scr.addstr(y, int(x0*2), c*2)
- continue
- s = float(y1-y0)/(x1-x0)
- if s < -1 or s > 1:
- if y0 > y1:
- x0, x1 = x1, x0
- y0, y1 = y1, y0
- s = 1/s
- for y in range(int(y0), int(y1)+1):
- x = x0+s*(y-y0)
- if len(b) == 3:
- scr.addstr(y, int(x*2), b[2]*2)
- else:
- scr.addstr(y, int(x*2), c*2)
- else:
- if x0 > x1:
- x0, x1 = x1, x0
- y0, y1 = y1, y0
- for x in range(int(x0), int(x1+1)):
- y = y0+s*(x-x0)
- if len(b) == 3:
- scr.addstr(y, int(x*2), b[2]*2)
- else:
- scr.addstr(y, int(x*2), c*2)
- scr.addstr(y0, x0*2, " ", curses.A_REVERSE)
- scr.addstr(y1, x1*2, " ", curses.A_REVERSE)
- c = scr.getch()
- if c == 258:
- for n, v in enumerate(vertices):
- vertices[n] = v.rotateX(-5)
- angleX -= 5
- elif c == 259:
- for n, v in enumerate(vertices):
- vertices[n] = v.rotateX(5)
- angleX += 5
- elif c == 260:
- for n, v in enumerate(vertices):
- vertices[n] = v.rotateY(5)
- angleY += 5
- elif c == 261:
- for n, v in enumerate(vertices):
- vertices[n] = v.rotateY(-5)
- angleY -= 5
- elif c == 48:
- vertices = verticeso[:]
- for n, i in enumerate(vertices):
- vertices[n] = Point3D(i[0], i[1], i[2])
- angleX, angleY, angleZ = 0, 0, 0
- elif c == 114:
- for n, v in enumerate(vertices):
- vertices[n] = v.rotateZ(-5)
- angleZ -= 5
- #print(c)
- scr.refresh()
- curses.wrapper(run)
Advertisement
Add Comment
Please, Sign In to add comment