def split_vector_equidistantly(vector, count):
    '''Splits a vector/coordinate into `count` parts.
    Returns a CoordinateArray with the new Coordinate segments.

    >>> split_vector_equidistantly(Coordinate(-10, 12))
    CoordinateArray(<0,0>, <-2.5,3.0>, <-5.0,6.0>, <-7.5,9.0>, <-10,12>)
    '''
    xs = [interpolate_linear(0, vector.x, i / count) for i in range(1, count)]
    ys = [interpolate_linear(0, vector.y, i / count) for i in range(1, count)]
    coords = [(0, 0)] + zip(xs, ys) + [vector]
    return CoordinateArray(coords)
def split_vector_equidistantly(vector, count) :
    '''Splits a vector/coordinate into `count` parts.
    Returns a CoordinateArray with the new Coordinate segments.

    >>> split_vector_equidistantly(Coordinate(-10, 12))
    CoordinateArray(<0,0>, <-2.5,3.0>, <-5.0,6.0>, <-7.5,9.0>, <-10,12>)
    '''
    xs = [interpolate_linear(0, vector.x, i / count) for i in range(1, count)]
    ys = [interpolate_linear(0, vector.y, i / count) for i in range(1, count)]
    coords = [(0, 0)] + zip(xs, ys) + [vector]
    return CoordinateArray(coords)
Beispiel #3
0
def path_linear(coords, interpolation_unit):
    '''Returns a path with linearly interpolated segments.
    Visually the result is the same as a plain path, but this is
    useful as an intermediate step in constructing more interesting
    shapes by deforming this one.

    - `coords` is a CoordinateArray.
    - `interpolation_unit` is the magnitude of the path segments created.
        Think of it as the sampling period in digital recording.
        If `interpolation_unit` > coord[i] - coord[i-1], the
        coord[i] - coord[i-1] segment is not further segmented.
    '''
    coords = CoordinateArray(coords)

    def units_per_path_segment(coords, interpolation_unit):
        result = [ ]
        diffs = coords.difference
        for i in range(len(diffs)):
            segs = diffs[i].magnitude / float(interpolation_unit)
            coord_pair = (coords[i], coords[i + 1])
            if segs < 1:
                result.append((coord_pair, 1))
            else:
                result.append((coord_pair, int(round(segs))))
        return result


    divs = units_per_path_segment(coords, interpolation_unit)
    result = [ ]
    for cp, n in divs:
        newcoords = [interpolate_linear(cp[0], cp[1], float(i) / n) for i in range(n)]
        result.extend(newcoords)

    return Path(result)