对点数组进行排序以在MATLAB中生成包络

时间:2016-07-29 22:25:55

标签: matlab sorting shape point

我有下一个载体:

A = [0;100;100;2100;100;2000;2100;2100;0;100;2000;2100;0];
B = [0;0;1450;1450;1550;1550;1550;1550;2500;2500;3000;3000;0]

如果我们绘制A和B,我们将获得以下图形:

enter image description here

然后,我想知道如何缩短积分以获得下一个阴谋:

enter image description here

正如你所看到的,还有一些条件:所有条件形成直角;线之间没有交叉点。

预先感谢您的回复!

2 个答案:

答案 0 :(得分:2)

这可以通过传统的递归迷宫来解决。在墙上爬行的解决方案':

%%% In file solveMaze.m
function Out = solveMaze (Pts,Accum)

  if isempty (Pts); Out = Accum; return; end   % base case

  x = Accum(1, end);  y = Accum(2, end);    % current point under consideration
  X = Pts(1,:);       Y = Pts(2,:);         % remaining points to choose from

  % Solve 'maze' by wall-crawling (priority: right, up, left, down)
  if     find (X > x  & Y == y); Ind = find (X > x  & Y == y); Ind = Ind(1);
  elseif find (X == x & Y > y ); Ind = find (X == x & Y > y ); Ind = Ind(1);
  elseif find (X < x  & Y == y); Ind = find (X < x  & Y == y); Ind = Ind(1);
  elseif find (X == x & Y < y ); Ind = find (X == x & Y < y ); Ind = Ind(1);
  else error('Incompatible maze');
  end

  Accum(:,end+1) = Pts(:,Ind);    % Add successor to accumulator
  Pts(:,Ind) = [];                % ... and remove from Pts

  Out = solveMaze (Pts, Accum);
end

如上所述给出A和B,如下呼叫;

Pts = [A.'; B.']; Pts = unique (Pts.', 'rows').'; % remove duplicates
Out = solveMaze (Pts, Pts(:,1));                  % first point as starting point
plot(Out(1,:), Out(2,:),'-o');                    % gives expected plot

答案 1 :(得分:0)

如果所有交叉点必须是90度角,我们可以形成可能的连接图。

# in pseudo-code, my matlab is very rusty
points = zip(A, B) 
graph = zeros(points.length, points.length)  # Generate an adjacency matrix
for i in [0, points.length): 
  for j in [i + 1, points.length): 
    if points[i].x == points[j].x or points[i].y == points[j].y: 
      graph[i][j] = graph[j][i] = 1
    else 
      graph[i][j] = graph[j][i] = 0 

注意:断开共线点可能很重要/提高性能。

在此之后,解决方案减少到找到Hamiltonian Cycle。不幸的是,这是一个NP难题,但幸运的是,MATLAB解决方案已经存在:https://www.mathworks.com/matlabcentral/fileexchange/51610-hamiltonian-graph--source--destination-(虽然我还没有测试过)。