function [xnew, ynew] = StayWithinBounds(ScriptVar,...
    frame_i, x_try, y_try, particle_index)
    % Checks if sampled coords (x_try, y_try) lie within bounds else mirror image rule applied
    % Output - 
    %    xnew, ynew - new set of x and y coordinates for the particle
    %
    % Inputs - 
    %    ScriptVar - Parameters container map
    %    frame_i - Current frame
    %    x_try - sampled x-coordinate for the particle during frame = frame_i
    %    y_try - sampled y-coordinate for the particle during frame = frame_i
    %    particle_index - particle index 
    
    N_FramesStartBoundaries = ScriptVar('N_FramesStartBoundaries');
    xyStartFigure = ScriptVar('xyStartFigure');
    ParticleType = ParticleTypeCalculator(ScriptVar, particle_index);
    xyStartFigure = xyStartFigure(ParticleType);
    xLimit = ScriptVar('xLimit');
    yLimit = ScriptVar('yLimit');
    
    xyBounds = xyStartFigure;
    if frame_i <= N_FramesStartBoundaries % That is, particle within the
        % rectangular start zone
        x_left = cellfun(@(v)v(1),xyBounds);
        x_right = cellfun(@(v)v(1),xyBounds) + cellfun(@(v)v(3),xyBounds);
        y_down = cellfun(@(v)v(2),xyBounds);
        y_up = cellfun(@(v)v(2),xyBounds)+cellfun(@(v)v(4),xyBounds);
    else
        % Start boundaries are gone, only the rectangular box
        % boundaries matter.
        x_left = 0*ones(size(x_try));
        x_right = xLimit*ones(size(x_try));
        y_down = 0*ones(size(x_try));
        y_up = yLimit*ones(size(x_try));
    end
    xnew = zeros(size(x_try));
    ynew = zeros(size(y_try));
    xnew(x_try < x_left) = 2*x_left(x_try < x_left) - x_try(x_try < x_left);
    xnew(x_try > x_left & x_try > x_right) = 2*x_right(x_try > x_left & x_try > x_right)...
        - x_try(x_try > x_left & x_try > x_right);
    xnew(x_try > x_left & x_try < x_right) = x_try(x_try > x_left & x_try < x_right);
    
    ynew(y_try < y_down) = 2*y_down(y_try < y_down) - y_try(y_try < y_down);
    ynew(y_try > y_down & y_try > y_up) = 2*y_up(y_try > y_down & y_try > y_up)...
        - y_try(y_try > y_down & y_try > y_up);
    ynew(y_try > y_down & y_try < y_up) = y_try(y_try > y_down & y_try < y_up);
end