Report abuse

cube.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python

import os, sys, matrix
import time
from math import *

def clear():
	sys.stdout.write("")

def poke(x,y):
	sys.stdout.write("[%d;%dHx"%(y+13,x+25))


def vecmat(v,m):
	x,y,z=v
	x1,y1,z1=m[0]
	x2,y2,z2=m[1]
	x3,y3,z3=m[2]
	return (x1*x+y1*y+z1*z,x2*x+y2*y+z2*z,x3*x+y3*y+z3*z)

nmat=((1,0,0),(0,1,0),(0,0,1))

def irange(start, finish):
     if start == finish:
         return [start]
     if start < finish:
         return range(start, finish + 1)
     if start > finish:
         return range(start, finish - 1, -1)
 

def line((x0,y0),(x1,y1)):
     points = []
     orig_x0 = x0
     orig_y0 = y0

     if abs(y1 - y0) > abs(x1 - x0):
         steep = True
     else:
         steep = False

     if steep:
         x0, y0 = y0, x0
         x1, y1 = y1, x1
     if x0 > x1:
         x0, x1 = x1, x0
         y0, y1 = y1, y0

     deltax = x1 - x0
     deltay = abs(y1 - y0)
     error = deltax / 2
     y = y0
     if y0 < y1:
         ystep = 1
     else:
          ystep = -1
     for x in irange(x0,x1):
         if steep:
             points.append((y,x))
         else:
             points.append((x,y))
         error = error - deltay
         if error < 0:
             y = y + ystep
             error = error + deltax
     # If the points go in the wrong direction, reverse them.
     if len(points) and points[0] != (orig_x0, orig_y0):
         points.reverse()

     return points
 

def rot(x,y):
	rx=((1,0,0),(0,cos(x),-sin(x)),(0,sin(x),cos(x)))
	ry=((cos(y),0,sin(y)),(0,1,0),(-sin(y),0,cos(y)))
	m=matrix.mult(nmat,rx)
	return matrix.mult(m,ry)

map=[24,12,0]

def transform(v):
	return tuple([i*map[o] for o,i in enumerate(vecmat(v,omat))])

def mov(v,m):
	return tuple([i+m for i in v])

cube=[
	((0,0,0),(0,0,1)),
	((0,0,0),(0,1,0)),
	((0,0,0),(1,0,0)),
	((0,0,1),(0,1,1)),
	((0,0,1),(1,0,1)),
	((0,1,0),(1,1,0)),
	((0,1,0),(0,1,1)),
	((1,0,0),(1,1,0)),
	((1,0,0),(1,0,1)),
	((1,1,1),(1,1,0)),
	((1,1,1),(1,0,1)),
	((1,1,1),(0,1,1)),
]

def clear():
	sys.stdout.write("")

rx=0
ry=0
for frame in range(10000):
	clear()
	omat=rot(rx,ry)
	for f,t in cube:
		f = mov(f,-0.5)
		t = mov(t,-0.5)
		x,y,z=transform(f)
		x2,y2,z2=transform(t)
		for lx,ly in line((x,y),(x2,y2)):
			poke(lx,ly)
		#poke(x2,y2)
		#print x,y
	sys.stdout.flush()
	time.sleep(0.1)
	rx += 0.07
	ry += 0.05

matrix.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def zero(m,n):
    # Create zero matrix
    new_matrix = [[0 for row in range(n)] for col in range(m)]
    return new_matrix

def mult(matrix1,matrix2):
    # Matrix multiplication
    if len(matrix1[0]) != len(matrix2):
        # Check matrix dimensions
        print 'Matrices must be m*n and n*p to multiply!'
    else:
        # Multiply if correct dimensions
        new_matrix = zero(len(matrix1),len(matrix2[0]))
        for i in range(len(matrix1)):
            for j in range(len(matrix2[0])):
                for k in range(len(matrix2)):
                    new_matrix[i][j] += matrix1[i][k]*matrix2[k][j]
        return new_matrix