Commit a9ade070 authored by Thomas Roy's avatar Thomas Roy
Browse files

it's alive

parent 06cce78c
% check if line segment AB intersects line segment CD
%
function isInSegment = checkCrossedLines(A,B,C,D)
function [isInSegment] = checkCrossedLines(A,B,C,D)
% A = [1,1]; C = [2,2]; B = [2,1]; D = [1,2];
......
n = 100;
k = 110;
dists = zeros(k,1);
cputime = zeros(k,1);
j=1;
for i=1:k
tic;
[A,x] = shortest_path( n );
[C,i_traverse,z] = traverse_graph(A,x);
m = size(C,1);
[B,y] = legal_paths(C,z ,i_traverse);
[dist, path] = graphshortestpath(sparse(B+B'), m+1, m+2);
cputime(i) = toc;
dists(i) = dist;
if dist ==Inf
A_inf{j} = A;
x_inf{j} = x;
y_inf{j} = y;
B_inf{j} = B;
j = j+1;
end
end
disp(['Mean distance is ', num2str(mean(dists))])
disp(['Standard deviation is ',num2str(std(dists))])
disp(['Mean cpu time is ', num2str(mean(cputime))])
\ No newline at end of file
function [ A2,x2,J ] = hollow_graph( A,x)
C = A'+A;
n = size(A,1);
delta = 1e-12;
% nodes of degree more
I = [];
for i=1:n
nonz = nnz(C(i,:));
if nonz==1
I = [I,i];
end
end
n1 = length(I);
J = setdiff(1:n,I);
n2 = n-n1;
x2 = x(J,:);
x2 = x2 + [delta,0];
AI = A(J,I);
AJ = A(J,J);
A2 = zeros(n+n2);
A2(1:n,1:n) = A;
A2(n+1:n+n2,n+1:n+n2) = AJ;
A2(n+1:n+n2,I) = AI;
x3 = [x;x2];
\ No newline at end of file
n = 100;
[A,x] = shortest_path( n );
[C,i_traverse,z] = traverse_graph(A,x);
m = size(C,1);
[B,y] = legal_paths(C,z ,i_traverse);
% [B,y] = legal_path(C,z);
[dist, path] = graphshortestpath(sparse(B+B'), m+1, m+2);
%%
B2 = tril(B)+tril(B)';
[dist, path] = graphshortestpath(sparse(B2), n+length(x2)+1, n+length(x2)+2);
function [B,y] = legal_paths(C,z,i_traverse)
%UNTITLED19 Summary of this function goes here
% Detailed explanation goes here
m = size(C,1);
y = z;
y(m+1,:) = [0,0];
y(m+2,:) = [0.5,0.5];
B = zeros(m+2);
C = C'+C;
for i = 1:m+2
if i<m+1
neighbours_i = find((i_traverse==i_traverse(i)));
else
neighbours_i = i;
end
%Connect to possible non-neighbours
for j = setdiff(i:m+2,neighbours_i,'stable')
if j<m+1
neighbours_j = find((i_traverse==i_traverse(j)));
else
neighbours_j = j;
end
[I,J] = find(C);
I1 = I(I~=i & I~=j & J~=i & J~=j);
J = J(I~=i & I~=j & J~=i & J~=j);
I = I1;
% [I,indices] = setdiff(I,j,'stable');
% J = J(indices);
% [J,indices] = setdiff(J,i,'stable');
% I = I(indices);
% [J,indices] = setdiff(J,j,'stable');
% I = I(indices);
crosses = 0;
for k = 1:length(I)
crosses = checkCrossedLines(y(i,:),y(j,:),y(I(k),:),y(J(k),:));
if crosses >0
break
end
end
if crosses == 0
B(i,j) = sqrt(sum((y(i,:)-y(j,:)).^2));
end
end
% % Make neighbours obstacles i.e. Inf paths
% for j= neighbours_i(neighbours_i~=i)
% B(i,j) = Inf;
% end
%
end
end
......@@ -12,10 +12,45 @@ ylim([0,1])
figure
plot(G2, 'XData',x(:,1),'YData',x(:,2));
A_path = zeros(n+2);
A_path = zeros(m+2);
for i=1:length(path)-1
A_path(path(i),path(i+1)) = 1;
end
hold on
G3 = graph(A_path+A_path');
plot(G3, 'XData',y(:,1),'YData',y(:,2),'LineWidth',1.5,'EdgeColor','g');
%%
figure
G4 = graph(A2+A2');
x3 = [x;x2];
plot(G4, 'XData',x3(:,1),'YData',x3(:,2));
%%
figure
G5 = graph(C+C');
plot(G5, 'XData',z(:,1),'YData',z(:,2));
%%
figure
G5 = graph(C+C');
plot(G5, 'XData',z(:,1),'YData',z(:,2));
hold on
G2 = graph(A+A');
plot(G2, 'XData',x(:,1),'YData',x(:,2))
xlim([0,1])
ylim([0,1])
%%
figure
G5 = graph(C+C');
plot(G5, 'XData',z(:,1),'YData',z(:,2));
A_path = zeros(m+2);
for i=1:length(path)-1
A_path(path(i),path(i+1)) = 1;
end
hold on
G3 = graph(A_path+A_path');
plot(G3, 'XData',y(:,1),'YData',y(:,2),'LineWidth',1.5,'EdgeColor','g');
\ No newline at end of file
function [ i_next,angle] = minimum_angle(i0,i1,A,x)
%
D = A'+A;
% going from x0 to x1
[~,nodes] = find(D(i1,:));
if length(nodes) >1
nodes = nodes(nodes ~=i0);
end
j = 1;
theta = zeros(length(nodes),1);
for k=nodes
theta0 = acos(dot(x(i0,:)-x(i1,:), x(k,:)-x(i1,:))/(sqrt(sum(abs(x(i0,:)-x(i1,:)).^2))*D(k,i1)));
thecross = cross([x(i0,:)-x(i1,:),0], [x(k,:)-x(i1,:),0]);
thesign = sign(thecross(3));
if thesign<0
theta(j) = 2*pi-theta0;
else
theta(j) = theta0;
end
j=j+1;
end
[angle,i_next] = min(theta);
i_next = nodes(i_next);
end
function [B, i_traverse,y] = traverse_graph( A,x )
delta = 1e-5;
C = A'+A;
nonz=2;
i=1;
while nonz>1
nonz = nnz(C(i,:));
if nonz==1
current_node = i;
end
i=i+1;
end
i_traverse(1) = current_node;
y(1,:) = x(current_node,:);
next_node = find(C(current_node,:));
j = 2;
while next_node ~= i_traverse(1)
[node_temp,angle] = minimum_angle(current_node,next_node,A,x);
if node_temp == current_node
y(j,:) = x(next_node,:);
elseif angle<pi
y(j,:) = x(next_node,:) + delta*(x(current_node,:) + x(node_temp,:) - 2*x(next_node,:));
elseif angle>=pi
y(j,:) = x(next_node,:) - delta*(x(current_node,:) + x(node_temp,:) - 2*x(next_node,:));
end
current_node = next_node;
i_traverse(j) = current_node;
next_node = node_temp;
j = j+1;
end
m = length(i_traverse);
B = zeros(m);
for i=1:m-1
B(i,i+1) = C(i_traverse(i),i_traverse(i+1));
% Make blocks between neighbours
for j=find(i_traverse==i_traverse(i))
if (j~=i)
B(i,j) =Inf;
end
end
% Fill in the Polygon
for j=find(i_traverse==i_traverse(i+1))
if (j~=i+1)
B(i,j) =Inf;
end
end
end
for i=2:m
% Fill in the Polygon
for j=find(i_traverse==i_traverse(i-1))
if (j~=i-1)
B(i,j) =Inf;
end
end
end
B(m,1) = C(i_traverse(m),i_traverse(1));
% y = x(i_traverse,:);
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment