Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys, math
- from time import sleep
- import os
- linux = "linux" in os.sys.platform
- if not linux:
- l = [[" "]]
- l_len_y = len(l)
- l_len_x = len(l[0])
- class point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def out():
- for i in l:
- print(''.join(i))
- def plot(p, c):
- x = int(p.x); y = int(p.y)
- if linux:
- print("\033[%s;%sH%s%s" % (y, 2*x, c, c))
- else:
- if x < 0 or y < 0: return
- global l_len_y
- global l_len_x
- if y > l_len_y:
- for i in range(l_len_y, y):
- l.append([" " for n in range(l_len_x)])
- l_len_y = y
- if x > l_len_x:
- for i in range(l_len_y):
- l[i].extend([" " for n in range(x-l_len_x)])
- l_len_x = x
- l[y-1][x-1] = c
- def draw_line(p0, p1, c):
- x0 = p0.x; y0 = p0.y
- x1 = p1.x; y1 = p1.y
- del p0, p1
- if x0 < 0 or x1 < 0 or y0 < 0 or y1 < 0:
- print("negative coordinate")
- return
- if x1 == x0:
- for y in range(int(y0), int(y1)):
- plot(point(x0, y), c)
- return
- 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)
- plot(point(x, y), c)
- 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)
- plot(point(x, y), c)
- if linux: outchar = "\033[47m \033[0m"
- else: outchar = "*"
- plot(point(x0, y0), outchar)
- plot(point(x1, y1), outchar)
- 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)
- class Simulation:
- def __init__(self):
- self.vertices = [
- [-1, 1,-1], [ 1, 1,-1], [ 1,-1,-1], [-1,-1,-1],
- [-1, 1, 1], [ 1, 1, 1], [ 1,-1, 1], [-1,-1, 1]
- ]
- for n, i in enumerate(self.vertices):
- self.vertices[n] = Point3D(i[0], i[1], i[2])
- self.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]
- ]
- self.triangles = [
- [4, 5, 6], [4, 6, 7], [0, 1, 2], [0, 2, 3]
- ]
- self.angleX, self.angleY, self.angleZ = 0, 0, 0
- def run(self):
- from operator import itemgetter
- while 1:
- sleep(0.1)
- if linux:
- os.system("clear")
- else:
- global l
- for na, a in enumerate(l):
- for nb, b in enumerate(l[na]):
- l[na][nb] = " "
- if linux: sx = 45; sy = 50
- else: sx = l_len_x; sy = l_len_y
- self.plots = []
- self.vd = {}
- for n, v in enumerate(self.vertices):
- r = v.rotateX(self.angleX).rotateY(self.angleY).rotateZ(self.angleZ)
- p = r.project(sx, sy, 90, 10)
- self.plots.append(point(int(p.x)+3, int(p.y)+3))
- self.vd[n] = math.sqrt(r.x**2+r.y**2+(r.z+10)**2)
- self.vds = [i[0] for i in sorted(self.vd.items(), key=itemgetter(1))][:7]
- for b in self.bones:
- p0 = self.plots[b[0]]
- p1 = self.plots[b[1]]
- if b[0] in self.vds and b[1] in self.vds:
- if linux: outchar = "*"
- else: outchar = "."
- draw_line(p0, p1, outchar)
- self.angleX += 4
- self.angleY += 4
- self.angleZ += 4
- if not linux:
- os.system("CLS")
- out()
- Simulation().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement