%get samples from path structure

function [x_samples,x_time,y_samples,y_time] = process_group(path,number_of_samples,random)
close all;

global_i_x = 1;
global_i_y = 1;
global_i = 1;

for k = 1:length(path)

    if random
        x_component = path(k).x;
        y_component = path(k).y;

        random_samples_x = randsample(x_component,number_of_samples);
        random_samples_y = randsample(y_component,number_of_samples);

        for i = 1:number_of_samples
            % x component
            %find time that corresponds to a certain x
            j = find(x_component == random_samples_x(i));
            x_samples(global_i_x) = random_samples_x(i);
            x_time(global_i_x) = path(k).time_normalized(j(1));
            global_i_x = global_i_x + 1;

            % y component
            j = find(y_component == random_samples_y(i));
            y_samples(global_i_y) = random_samples_y(i);
            y_time(global_i_y) = path(k).time_normalized(j(1));
            global_i_y = global_i_y + 1;
        end

    else
        % old style, got samples from exact time instants
        time_vector = linspace(0,5,number_of_samples);
        for i = 1:length(time_vector)
            %find closest path index for a given time
            n_time = path(k).time_normalized;
            z = abs(n_time - time_vector(i));
            j = find(min(z) == z);

            %store time and x value
            x_time(global_i) = time_vector(i);
            y_time(global_i) = time_vector(i);
            x_samples(global_i) = path(k).x(j);
            y_samples(global_i) = path(k).y(j);
            global_i = global_i + 1;
        end
    end
end

%drawing part
x_figure = figure; hold all;
xlabel('normalized time'), ylabel('x position');
y_figure = figure; hold all;
xlabel('normalized time'), ylabel('y position');

for k = 1:length(path);
    figure(x_figure), plot(path(k).time_normalized,path(k).x);
    figure(y_figure), plot(path(k).time_normalized,path(k).y);
end

figure(x_figure), plot(x_time,x_samples,'ro');
figure(y_figure), plot(y_time,y_samples,'ro');
