Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It seems that there is no implemented solution to the issue. No problem, there is a quick solution using bare hands. (Although we can plot the polygon by the polygon command, there will be in the following a parallel plot of the edges...)

def plotLabelledPolygon( vertices, labels, space=0.1 ):
    """for two lists of same length, 
    of vertices in the real plane, A, B, C, ...
    and of labels a, b, ... for AB, BC, ...
    we associate a plot object consisting of the segments and the labels.
    The labels are positioned corresponding to the middle of the segments,
    there is some space left in between.
    """

    p = plot( [] )
    n = len( vertices )
    if n < 3 or n != len( labels ):
        return p

    zVertices = []
    for A in vertices:
        if   type(A) in ( tuple, list ):    zA = A[0] + i*A[1]
        elif      A  in CC             :    zA = A
        else                           :    zA = None    # feel free to extend for vectors & Co...
        zVertices.append( zA )

    for ind in range(n):
        j = ind+1 if ind+1 < n else 0
        A, zA = vertices[ ind ], zVertices[ ind ]
        B, zB = vertices[ j   ], zVertices[ j   ]

        p += line( [ A, B ] )
        zBA = zB - zA

        if zBA == 0:
            print "zA = %s and zB = %s coincide :: IGNORED" % ( zA, zB )            
            continue

        zT = CC( zA + zBA / 2 - space * I * zBA / abs( zBA ) )
        label = labels[ ind ]
        p += text( '$%s$' % label, ( zT.real(), zT.imag() ) )

    return p

Examples of usage:

vertices = [ (-1,0), (1,0), (0,1) ]
labels   = [ 'a', 'b', 'c' ]    # or simply abc
p = plotLabelledPolygon( vertices, labels )
p.axes_color( 'gray' )
p.show()

vertices = [ (0,0), (3,-3), (1,0), (3,3) ]
labels   = 'abcd'
q = plotLabelledPolygon( vertices, labels, space=0.1 )
q.axes_color( 'gray' )
q.show()

(The font, the aspect, the axes, ... can be controlled in the plot object.)