In [2]:
%load_ext sympyprinting

import itertools as it

from pylab import *

import coordinates
reload(coordinates)
from coordinates import *

import notebookutils
reload(notebookutils)
from notebookutils import *

set_printoptions(precision=3)

In [3]:
def testpoly(p, sides=False):
    if type(p) == ndarray and len(p.shape) < 2: p = [p]
    elif type(p) == ndarray and len(p.shape) > 1:    
        p = list(array(r) for r in p)
    
    n = len(p)    # number of points
    d = len(p[0]) # number of sides
    
    # q-points are bary. coords for the projection of points in p along each edge.
    q = concatenate([baryedges(pi) for pi in p]) if sides else np.array([])
    
    # Which point each projected point references.
    qindices = (arange(n) * ones((d, n), dtype=int)).T.flatten() if sides else np.array([])
    
    # Labels for every point.
    plabels = ['$p_{%d}$' % i for i in range(n)]
    qlabels = [
        '$q_{%d,%d%d}$' % (a, b, c) 
        for (a, (b, c)) in list(it.product(
            range(n), 
            zip(
                range(d), 
                [i % d for i in range(1, d + 1)]
            )
        ))
    ] if sides else []
    
    # Colors for every point.
    cm = get_cmap("hsv")
    pcolors = [cm(float(i) / n)[0:3] + (.75,) for i in range(n)]
    qcolors = [pcolors[i][0:3] + (.5,) for i in qindices] if sides else []
    
    # Cartesian coordinates for points and closest points along the sides.
    pcart = [bary2cart(pi) for pi in p]
    qcart = bary2cart(q) if sides else np.array([])
    
    # Lines that connect each q-point to its reference point.
    lines = [
        Line2D(
            (pcart[i / len(p[0])][0], qcart[i, 0] + .00001),
            (pcart[i / len(p[0])][1], qcart[i, 1]),
            color = qcolors[i]
        ) for i in range(len(qcart))
    ] if sides else []
    
    # Create the view.
    f = polyshow(
        p + list(q),
        color=pcolors + qcolors,
        label=plabels + qlabels,
        labelvertices=True,
        lines=lines
    )
    
    # Show it.
    svgfig(f)

In [4]:
testpoly(rand(3), sides=True)



In [6]:
bary = lattice(3)
testpoly(bary, sides=True)