Exemple #1
0
def get_bounding_box(arg):
    '''Returns the pair of coordinate pairs outlining the bounding box of
    the given HPGL drawing.'''
    if not isinstance(arg, (list, tuple)):
        raise TypeError('arg must be list or tuple')

    min_x = min_y = 1000000.0
    max_x = max_y = -1000000.0

    coords = get_all_coordinates(arg)

    if len(coords) == 0:
        return None

    for c in coords:
        ## x...
        if c.x > max_x:
            max_x = c.x
        if c.x < min_x:
            min_x = c.x
        ## y...
        if c.y > max_y:
            max_y = c.y
        if c.y < min_y:
            min_y = c.y

    return (Coordinate(min_x, min_y), Coordinate(max_x, max_y))
Exemple #2
0
def get_centroid(arg):
    '''Returns the centroid of the given Chiplotle-HPGL shapes.'''
    arg = get_all_coordinates(arg)
    ## convert into a set to remove duplicate coordinates and to
    ## avoid giving more weight to these duplicate points...
    arg = set(arg)
    result = Coordinate(0, 0)
    for c in arg:
        result += c
    return result / len(arg)
def pupd_to_paths(hpgl):
    # slurp in PUs and PDs, and convert to a group of simple paths
    # probably fragile, but works fine on the output of pstoedit -f hpgl
    results = Group()
    builder = []
    
    for command in hpgl:
        if isinstance(command, PU):
            if builder:
                # must be starting a new path, so stash the last
                coords = get_all_coordinates(builder)
                results.append(Path(coords))
                builder = []
            builder.append(command)
        elif isinstance(command, PD):
            builder.append(command)
            
    return results