% edited on 04/25/2016
% have different setting for different recording room
% don't distinguish clockwise or couterclockwise running
% in the circle
% the angle of (1,0) is 0
% the angle of (0,-1) is pi/2
% the angle of (-1,0) is pi
% the angle of (0,1) is 3*pi/2

fs_sin=8000;
fs0=800;
t=0 : 1/fs_sin : 0.1;
beep0 = sin(fs0*2*pi*t);

pause(10);
% Starting zone border1
fprintf('Starting zone border 1: ');
sound(beep0,fs_sin);
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x_loc = locationArray(end - 1);
y_loc = locationArray(end);
while ~x_loc && ~y_loc %% if x_loc=0, y_loc=0 means it's a missing data from the video
    pause(0.1)
    [~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
    x_loc = locationArray(end - 1);
    y_loc = locationArray(end);
end
x0_StartZone = x_sign*double(x_loc).*scale_x;
y0_StartZone = y_sign*double(y_loc).*scale_y;
x1_StartZone = x0_StartZone-x_center;
y1_StartZone = y0_StartZone-y_center;
if x1_StartZone>=0
    if x1_StartZone==0 x1_StartZone=0.1; end
    ang1_StartZone=mod(atan(-y1_StartZone./x1_StartZone),2*pi);
else
    ang1_StartZone=mod(atan(-y1_StartZone./x1_StartZone)+pi,2*pi);
end

% Starting zone border2
fprintf('Starting zone border 2: ');
beep2=[beep0,zeros(size(beep0)),beep0];
sound(beep2,fs_sin);
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x_loc = locationArray(end - 1);
y_loc = locationArray(end);
while ~x_loc && ~y_loc %% if x_loc=0, y_loc=0 means it's a missing data from the video
    pause(0.1)
    [~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
    x_loc = locationArray(end - 1);
    y_loc = locationArray(end);
end
x0_StartZone = x_sign*double(x_loc).*scale_x;
y0_StartZone = y_sign*double(y_loc).*scale_y;
x2_StartZone = x0_StartZone-x_center;
y2_StartZone = y0_StartZone-y_center;
if x2_StartZone>=0
    if x2_StartZone==0 x2_StartZone=0.1; end
    ang2_StartZone=mod(atan(-y2_StartZone./x2_StartZone),2*pi);
else
    ang2_StartZone=mod(atan(-y2_StartZone./x2_StartZone)+pi,2*pi);
end

% find the center and the one-side width of start zone
Ang_StartZone=[ang1_StartZone;ang2_StartZone];
if abs(ang1_StartZone-ang2_StartZone)>pi
    % StartZone1 and StartZone2 are at different sides of ang=0
    Ang_StartZone_center=mod((ang1_StartZone+ang2_StartZone)/2-pi,2*pi);
else
    Ang_StartZone_center=mod((ang1_StartZone+ang2_StartZone)/2,2*pi);
end
ang_startzone_width=min(abs(Ang_StartZone-Ang_StartZone_center));

sound(beep0,fs_sin);