def tracks_to_lines(tracks, dims=3, return_trackid=False):
    """Return sequence of trajectory lines.

    Each array in the returned sequence contains the points of a single trajectory. The array will be a N x 3
    array of (x, y, t) points if `dims` is 3 or a N x 2 array of (x, y) points if `dims` is 2.

    Parameters
    ----------
    tracks : pandas.DataFrame
        Trajectory data. Must have columns x, y, t, and trackid.
    dims : {3, 2}
        Return (x, y) coordinates if `dims` is 2 and return (x, y, t) coordinates if `dims` is 3 (default is 3).
    return_trackid : bool, optional
        If True, also return the trackid of each trajectory (default is False).

    Returns
    -------
    lines : list of N x 3 or N x 2 arrays
        Trajectories as lines.
    trackids : array, optional
        The trackids of the trajectories returned in `lines`. Only provided if `return_trackid` is True.
    """
    if dims not in (2, 3):
        raise ValueError('dims must be either 2 or 3')
    cols = ('x', 'y') if dims == 2 else ('x', 'y', 't')

    trackids, lines = zip(*[(trackid, df.loc[:, cols].values) for trackid, df in tracks.groupby('trackid')])
    trackids = list(trackids)
    lines = list(lines)

    if return_trackid:
        return lines, trackids
    return lines