% track the four borders to find the correct and incorrect areas
% put the headstage on four spots for each area

% track the correct and error areas
Ang_zones=[];

% Zone1
fprintf('left border of left error area1\r');
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x1_error_left = double(locationArray(end - 1)-x_center);
y1_error_left = (-1)*double(locationArray(end)-y_center);
if x1_error_left>=0
    if x1_error_left==0 x1_error_left=0.1; end
    ang1_error_left=mod(atan(y1_error_left./x1_error_left),2*pi);
else
    ang1_error_left=mod(atan(y1_error_left/x1_error_left)+pi,2*pi);
end

fprintf('left border of correct area1\r');
sound([beep0;beep0],fs_sin);
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x1_correct_left = double(locationArray(end - 1)-x_center);
y1_correct_left = (-1)*double(locationArray(end)-y_center);
if x1_correct_left>=0
    if x1_correct_left==0 x1_correct_left=0.1; end
    ang1_correct_left=mod(atan(y1_correct_left/x1_correct_left),2*pi);
else
    ang1_correct_left=mod(atan(y1_correct_left/x1_correct_left)+pi,2*pi);
end

fprintf('right border of correct area1\r');
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x1_correct_right = double(locationArray(end - 1)-x_center);
y1_correct_right = (-1)*double(locationArray(end)-y_center);
if x1_correct_right>=0
    if x1_correct_right==0 x1_correct_right=0.1; end
    ang1_correct_right=mod(atan(y1_correct_right/x1_correct_right),2*pi);
else
    ang1_correct_right=mod(atan(y1_correct_right/x1_correct_right)+pi,2*pi);
end

fprintf('right border of right error area1\r');
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x1_error_right = double(locationArray(end - 1)-x_center);
y1_error_right = (-1)*double(locationArray(end)-y_center);
if x1_error_right>=0
    if x1_error_right==0 x1_error_right=0.1; end
    ang1_error_right=mod(atan(y1_error_right/x1_error_right),2*pi);
else
    ang1_error_right=mod(atan(y1_error_right/x1_error_right)+pi,2*pi);
end

Ang_zones=[Ang_zones;[ang1_error_left,ang1_correct_left,ang1_correct_right,ang1_error_right]];

% Zone2
fprintf('left border of left error area2\r');
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x2_error_left = double(locationArray(end - 1)-x_center);
y2_error_left = (-1)*double(locationArray(end)-y_center);
if x2_error_left>=0
    if x2_error_left==0 x2_error_left=0.1; end
    ang2_error_left=mod(atan(y2_error_left/x2_error_left),2*pi);
else
    ang2_error_left=mod(atan(y2_error_left/x2_error_left)+pi,2*pi);
end

fprintf('left border of correct area2\r');
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x2_correct_left = double(locationArray(end - 1)-x_center);
y2_correct_left = (-1)*double(locationArray(end)-y_center);
if x2_correct_left>=0
    if x2_correct_left==0 x2_correct_left=0.1; end
    ang2_correct_left=mod(atan(y2_correct_left/x2_correct_left),2*pi);
else
    ang2_correct_left=mod(atan(y2_correct_left/x2_correct_left)+pi,2*pi);
end

fprintf('right border of correct area2\r');
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x2_correct_right = double(locationArray(end - 1)-x_center);
y2_correct_right = (-1)*double(locationArray(end)-y_center);
if x2_correct_right>=0
    if x2_correct_right==0 x2_correct_right=0.1; end
    ang2_correct_right=mod(atan(y2_correct_right/x2_correct_right),2*pi);
else
    ang2_correct_right=mod(atan(y2_correct_right/x2_correct_right)+pi,2*pi);
end

fprintf('right border of right error area2\r');
pause(5);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
x2_error_right = double(locationArray(end - 1)-x_center);
y2_error_right = (-1)*double(locationArray(end)-y_center);
if x2_error_right>=0
    if x2_error_right==0 x2_error_right=0.1; end
    ang2_error_right=mod(atan(y2_error_right/x2_error_right),2*pi);
else
    ang2_error_right=mod(atan(y2_error_right/x2_error_right)+pi,2*pi);
end

Ang_zones=[Ang_zones;[ang2_error_left,ang2_correct_left,ang2_correct_right,ang2_error_right]];
