import math
from scipy import gplt

def dot(a,b):
    l = len(a)
    lb = len(b)
    assert(l == lb)
    r = 0.0
    for i in range(l):
        r += a[i]*b[i]
    return r

def cross(a,b):
    la = len(a)
    lb = len(b)
    assert(la == 3 and lb == 3)
    r1 = a[1]*b[2] - a[2]*b[1]
    r2 = a[0]*b[2] - a[2]*b[0]
    r3 = a[0]*b[1] - a[1]*b[0]
    return [r1, -r2, r3]

def p(lb, le, t):
    l = len(lb)
    r = list()
    for i in range(l):
        r.append(lb[i]*(1-t) + le[i]*t)
    return r

def genxy(l , r, ep=0.005):
    lb = l[0]
    le = l[1]
    t = 0.0
    rx = list()
    ry = list()
    while t<=1.0:
        tp = p(lb, le, t)
        x = dot(tp,r)
        s = cross(tp,r)
        y = d(s)
        rx.append(x)
        ry.append(y)
        t += ep
    return rx,ry

def d(a):
    s = 0.0
    for x in a:
        s +=x**2
    return math.sqrt(s)

def unit(a):
    dd = d(a)
    r = list()
    for x in a:
        r.append(x/dd)
    return r

def elementp(a,b):
    l = len(a)
    r = list()
    for i in range(l):
        r.append(a[i]*b[i])
    return r
def genlines(a, b, c):
    lines = list()
    trans = [[1, 1, 1],
         [-1, -1, 1],
         [1, -1, -1],
         [-1, 1, -1]
        ]
    for t in trans:
        [ta, tb, tc]  = elementp([a,b,c],t)
        tlines = [
            [[ ta, tb, tc], [-ta, tb, tc]],
            [[ ta, tb, tc], [ ta,-tb, tc]],
            [[ ta, tb, tc], [ ta, tb,-tc]]
            ]
        lines = lines + tlines
    return lines

import sys
def plotl(a, b, c, ep=0.005):
    r = unit([a,b,c])
    lines = genlines(r[0],r[1],r[2])
    lis = [(0,0),(0,1)]
    for l in lines:
        rx, ry = genxy(l, r, ep)
        lis.append(rx)
        lis.append(ry)
    apply(gplt.plot,lis)
    gplt.title("l(t)-- a=%f, b=%f, c=%f"%(r[0],r[1],r[2]))
    gplt.xaxis([-1.0,1.0])
    gplt.yaxis([0,1])
    sys.stderr.write("press RETURN to continue:")
    raw_input()

def main():
    plotl(1, 1, 1)
    plotl(1, 0.5, 0.5)
    plotl(1, 1.5, 1.5)
    plotl(1, 2, 3)
    plotl(1, 1.5, 2)
if __name__ == "__main__":
    main()
    
