#!/usr/bin/python

import math

def add(va,vb):
	return (va[0] + vb[0], va[1] + vb[1], va[2] + vb[2])

def sub(va,vb):
	return (va[0] - vb[0], va[1] - vb[1], va[2] - vb[2])

def mul(va,vb):
	return (va[0] * vb[0], va[1] * vb[1], va[2] * vb[2])

def div(va,vb):
	return (va[0] / vb[0], va[1] / vb[1], va[2] / vb[2])

def dot_product(va,vb=None):
	#va*vb
	if vb == None:
		vb = va
	#tmp = mul(va,vb)
	#return tmp[0] + tmp[1] + tmp[2]
	return (va[0]*vb[0])+(va[1]*vb[1])+(va[2]*vb[2])

def rotate_z(va,rad):
	cos = math.cos(rad)
	sin = math.sin(rad)
	#x = (va[0] * cos) - (va[1] * sin)
	#y = (va[0] * sin) + (va[1] * cos)
	#return (x, y, va[2])
	return ((va[0] * cos) - (va[1] * sin), (va[0] * sin) + (va[1] * cos), va[2])

def rotate_x(va,rad):
	cos = math.cos(rad)
	sin = math.sin(rad)
	#y = (va[1]*cos) - (va[2]*sin)
	#z = (va[1]*sin) + (va[2]*cos)
	#return (va[0], y, z)
	return (va[0], (va[1]*cos) - (va[2]*sin), (va[1]*sin) + (va[2]*cos))

def rotate_y(va,rad):
	cos = math.cos(rad)
	sin = math.sin(rad)
	#z = (va[2] * cos) - (va[0] * sin)
	#x = (va[2] * sin) + (va[0] * cos)
	#return (x, va[1], z)
	return ((va[2] * sin) + (va[0] * cos), va[1], (va[2] * cos) - (va[0] * sin))

def distance(va,vb=None):
	#euclidean distance sqrt((a0-b0)^2 + .. (an-bn)^2)
	if vb:
		return math.sqrt( ((va[0]-vb[0])**2) + ((va[1]-vb[1])**2) + ((va[2]-vb[2])**2) )
	else:
		return math.sqrt( (va[0]**2) + (va[1]**2) + (va[2]**2) )
	#tmp = sub(va,vb)
	#tmp = mul(tmp,tmp)
	#return math.sqrt( tmp[0] + tmp[1] + tmp[2] )

def cross_product(va,vb,vc):
	#plane
	#return mul(sub(vb,va),sub(vc,va))
	return ( (vb[0]-va[0])*(vc[0]-va[0]) , (vb[1]-va[1])*(vc[1]-va[1]) , (vb[2]-va[2])*(vc[2]-va[2]) )

if __name__ == "__main__":
	import spiele

	center = (0,0,1)
	pl = [(-0.5,-0.5,-0.5),(0.5,-0.5,-0.5),(0.5,0.5,-0.5),(-0.5,0.5,-0.5),(-0.5,-0.5,0.5),(0.5,-0.5,0.5),(0.5,0.5,0.5),(-0.5,0.5,0.5)]

	def deuxdee(p):
		global center
		tmp = add(p,center)
		x = tmp[0]/tmp[2]
		y = tmp[1]/tmp[2]
		return spiele.point(x,y)

	def loop():
		for i in range(len(pl)):
			#pl[i] = rotate_z(pl[i],math.pi/90)
			pl[i] = rotate_y(pl[i],math.pi/90)
			#pl[i] = rotate_x(pl[i],math.pi/90)
		spiele.draw([deuxdee(pl[0]), deuxdee(pl[1]), deuxdee(pl[2]), deuxdee(pl[3])])
		spiele.draw([deuxdee(pl[4]), deuxdee(pl[5]), deuxdee(pl[6]), deuxdee(pl[7])])
		spiele.draw([deuxdee(pl[0]), deuxdee(pl[1]), deuxdee(pl[5]), deuxdee(pl[4])])
		spiele.draw([deuxdee(pl[2]), deuxdee(pl[3]), deuxdee(pl[7]), deuxdee(pl[6])])

	spiele.init()
	spiele.loop(loop)
