-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfind_distance.m
66 lines (50 loc) · 2 KB
/
find_distance.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function [shortest_distance, x_found, y_found] = find_distance(max_range,linedata, x3, y3, theta)
% this function compute the intersection of a laser beam with all the line
% segments and find the intersection with shortest distance
% Shoudong Huang, 2016 April
line_size = size(linedata,1); % the number of line segments
shortest_distance = max_range; % Initialise shortest distance;
%hold on;
%plot(x3,y3,'g.-');
%[x3 y3 (theta*(180/pi))]
for j = 1:line_size
x1 = linedata(j,1);
y1 = linedata(j,3);
x2 = linedata(j,2);
y2 = linedata(j,4);
%plot([x1,x2],[y1,y2],'-k'); %Plot line
m3 = tan(theta);
u = [(y3-y1) + m3*(x1-x3)] / [(y2-y1) - m3*(x2-x1)];
c = y3 - m3*x3;
x = x1 + u*(x2-x1);
y = y1 + u*(y2-y1);
%Check if the intersection point is in the wrong direction%
Heading_wrong = wrap((atan2([y-y3],[x-x3])) + pi);
if (Heading_wrong>theta-0.001)&&(Heading_wrong<theta+0.001)
distance = max_range;
else
%Check bound of intecept
if (x>=x1&&x<=x2)||(x<=x1&&x>=x2) % Within X bounds
if (y>=y1&&y<=y2)||(y<=y1&&y>=y2) % Within Y bounds
distance = sqrt((x-x3)^2+(y-y3)^2);
else
distance = max_range;
end
else
distance = max_range;
end
end
% Update shortest distance
if distance < shortest_distance
shortest_distance = distance;
x_found = x;
y_found = y;
end
end
if shortest_distance == max_range
x_found = x3; y_found = y3;
end
% plot([x_found],[y_found],'.r'); %plot intercept
%answer = struct('laser',{[shortest_distance;x_found;y_found]});
% hold off;
end