Commit 76e13bb9 authored by Thomas Roy's avatar Thomas Roy
Browse files

legal path and corners

parent f8deb196
function corner = check_corner(i0,i1,A,y)
m = size(y,1);
C = zeros(m);
C(1:m-2,1:m-2) = A;
D = C'+C;
% going from x0 to x1
[~,nodes] = find(D(i1,:));
nodes = nodes(nodes~=i0); %remove existing edge if it exists
j = 1;
theta = zeros(length(nodes),1);
for k=nodes
theta0 = acos(dot(y(i0,:)-y(i1,:), y(k,:)-y(i1,:))/(sqrt(sum(abs(y(i0,:)-y(i1,:)).^2))*D(k,i1)));
thecross = cross([y(i0,:)-y(i1,:),0], [y(k,:)-y(i1,:),0]);
thesign = sign(thecross(3));
if thesign<0
theta(j) = 2*pi-theta0;
else
theta(j) = theta0;
end
j=j+1;
end
if isempty(nodes)
corner = 0;
else
corner = (max(theta) - min(theta))>pi;
end
end
function [B,x] = legal_path(A,x )
% We generate n uniformly random points in [0,1]x[0,1]
% x = rand(n,2);
n = size(x,1);
% Initialise Adjacency mantrix
B = zeros(n+2);
% B(1:n,1:n) = A; % Only do upper triangular since symmetric
x(n+1,:) = [0,0];
x(n+2,:) = [0.5,0.5];
% A = A+A';
% First Line
% B(1,2) = norm(x(1,:)-x(2,:));
% Construct the graph
for i=1:n+2
distances = sqrt(sum(abs(x-x(i,:)).^2,2));
[distances, nodes] = sort(distances);
crosses = 1;
j = 1;
for l=setdiff(1:n+2,i)
node = nodes(j);
d_min = distances(j);
[I,J] = find(A);
% Remove node included in current segment
J = J(I ~= node & I~=i);
I = I(I ~= node & I~=i);
I = I(J ~= node & J~=i);
J = J(J ~= node & J~=i);
for k=1:length(I)
crosses = checkCrossedLines(x(i,:),x(node,:),x(I(k),:),x(J(k),:));
if crosses==1
break
end
end
if crosses == 1
corners = 1;
else
% check if outgoing nodes are corners
node_corner = check_corner(i,node,A+A',x);
outgoing_corner = check_corner(node,i,A+A',x);
corners = node_corner | outgoing_corner;
end
if (crosses==0 && corners==0) || isempty(I)
B(node,i) = d_min;
end
j = j+1;
end
end
end
G = graph(A+A');
plot(G, 'XData',x(:,1),'YData',x(:,2))
figure
G = graph(B+B');
plot(G, 'XData',y(:,1),'YData',y(:,2))
hold on
G2 = graph(A+A');
plot(G2, 'XData',x(:,1),'YData',x(:,2))
xlim([0,1])
ylim([0,1])
\ No newline at end of file
......@@ -10,11 +10,11 @@ A(1,2) = norm(x(1,:)-x(2,:));
for i=3:n
distances = sqrt(sum(abs(x(1:i-1,:)-x(i,:)).^2,2));
[distances, nodes] = sort(distances);
cross = 1;
crosses = 1;
node = nodes(1);
d_min = distances(1);
j = 1;
while cross == 1
while crosses == 1
[I,J] = find(A(1:i-1,1:i-1));
% Remove node included in current segment
......@@ -23,12 +23,12 @@ for i=3:n
I = I(J ~= node);
J = J(J ~= node);
for k=1:length(I)
cross = checkCrossedLines(x(i,:),x(node,:),x(I(k),:),x(J(k),:));
if cross==1
crosses = checkCrossedLines(x(i,:),x(node,:),x(I(k),:),x(J(k),:));
if crosses==1
break
end
end
if cross==1 && ~isempty(I)
if crosses==1 && ~isempty(I)
j = j+1;
node = nodes(j);
d_min = distances(j);
......
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