import math

def normalise(x, y, z):
    d = math.sqrt(x*x + y*y + z*z)
    return x/d, y/d, z/d

def normalise4(x, y, z, w):
    d = math.sqrt(x*x + y*y + z*z)
    return x/d, y/d, z/d, w/d

def normal(p1, p2, p3):
    '''    p1
           /\
        p2 -- p3
    '''
    ax, ay, az = p2[0]-p1[0], p2[1]-p1[1], p2[2]-p1[2]
    bx, by, bz = p3[0]-p1[0], p3[1]-p1[1], p3[2]-p1[2]
    return normalise(ay*bz-az*by, az*bx-ax*bz, ax*by-ay*bx)

def average_normal(*normals):
    x, y, z = 0, 0, 0
    for nx, ny, nz in normals:
        x = x + nx
        y = y + ny
        z = z + nz
    return normalise(x, y, z)

try:
    from _util import *
except:
    print "not using C util functions"
    pass

