function [ k, R ] = wall(radar_pos, theta, target_pos, target_size)
R = inf; % Return 0 range if no detection is made
scene_x = 30; % Plot x dimension in m
scene_y = 15; % Plot y dimension in m
radar_pos = radar_pos; % x y position of the radar
%M = 1; % Number of bounces considered
%target_pos = [10 7];
theta = theta; % angle of radar beam in degrees
wall1_x = 7; % x coordinate of wall 1
wall1_y = 5; % y coordinate of wall 1
wall2_x = 5; % x coordinate of wall 1
wall2_y = 8; % y coordinate of wall 1
subplot(2,1,1)
hold on;
plot([wall1_x, wall1_x, scene_x], [0, wall1_y, wall1_y], 'k'); % plot wall 1
plot([wall2_x, wall2_x, scene_x], [scene_y, wall2_y, wall2_y], 'k'); % plot wall 2
%% Plot the target
plot([target_pos(1) - target_size / 2, target_pos(1) + target_size / 2], [target_pos(2), target_pos(2)]);
%% Begin painful geometry
k = 0;
if tand(theta) * (wall2_x - radar_pos*(1))+radar_pos(2) > wall2_y % beam overshoots the gap
s = tand(theta) * (wall2_x - radar_pos(1))+radar_pos(2); % y-position where beam hits wall face
plot([radar_pos(1), wall2_x, 0], [radar_pos(2), s, tand(theta) * (wall2_x - radar_pos(1)) + s]);
elseif tand(theta) * (wall1_x - radar_pos(1)) + radar_pos(2) < wall1_y % beam undershoots the gap
s = tand(theta) * (wall1_x - radar_pos(1)) +radar_pos(2); % y-position where beam hits wall face
plot([radar_pos(1), wall1_x, 0], [radar_pos(2), s, tand(theta) * (wall1_x - radar_pos(1)) + s]);
else
s = (wall2_y - radar_pos(2))/tand(theta) + radar_pos(1); % x-position where beam hits upper wall
x_inc = (wall2_y - wall1_y)/tand(theta); % x-value increment for each bounce
[M, hit_status] = target(radar_pos(1), radar_pos(2), target_pos(1), target_pos(2), target_size, wall1_y, wall2_y, theta);
% first segment to the target
k = M;
if M == 0 % beam hits target before walls
s = (target_pos(2) - radar_pos(2))/tand(theta) + radar_pos(1); % x-position where beam hits target
plot_y = [radar_pos(2), target_pos(2)];
else
plot_y = [radar_pos(2), wall2_y];
end
plot_x = [radar_pos(1), s];
while M > 0
if M > 1
plot_x = [plot_x, s + x_inc, s + 2*x_inc];
plot_y = [plot_y, wall1_y, wall2_y];
s = s + 2 * x_inc;
M = M-2;
% stop last reflection at target
if and(~M, hit_status)
plot_y(end) = target_pos(2);
plot_x(end) = ((target_pos(2) - wall1_y)/(wall2_y - wall1_y)) * x_inc + plot_x(end -1);
s = plot_x(end);
end
else
plot_x = [plot_x, s + x_inc];
plot_y = [plot_y, wall1_y];
s = s + x_inc;
M = M - 1;
% stop last reflection at target
if and(~M, hit_status)
plot_y(end) = target_pos(2);
plot_x(end) = ((wall2_y - target_pos(2))/(wall2_y - wall1_y)) * x_inc + plot_x(end -1);
s = plot_x(end);
end
end
end
plot(plot_x, plot_y);
if hit_status
R = (s - radar_pos(1))/cosd(theta);
else
R = inf;
end
end
drawnow
hold off
pause(0.000000001)
scatter(radar_pos(1), radar_pos(2));
% scatter(target_pos(1), target_pos(2)); % Plots a point at center of
% target
xlim([0, scene_x]);
ylim([0, scene_y]);
% saveas(gcf,sprintf('images/fig%d.png', theta))