How to produce an ordered sequence of vertices of polygon from a discretized cellular representation of polygon

后端 未结 1 1307
终归单人心
终归单人心 2021-01-26 16:32

Here is the cellular representation of polygon shown:-

Here is the extracted polygon shown.

We are given the input as a matrix polM

相关标签:
1条回答
  • 2021-01-26 17:12

    X and Y are both 2xn matrices specifying n unordered vertexes, i.e. a line should be drawn between [X(1, i) Y(1, i)] and [X(2, i) Y(2, i)] for every i=1:n. Because all the vertexes are connected, every point should occur twice in these X and Y matrices. Therefore, one can start at an arbitrary point on the polygon and search the next point iteratively. This can be done as follows:

    % XY_from: a `nx2` matrix containing all starting points 
    % with a third column indicating if this vertex is already used
    XY_from = [X(1, :)' Y(1, :)' zeros(size(X, 2), 1)]; 
    XY_to = [X(2, :)' Y(2, :)' zeros(size(X, 2), 1)]; % similar matrix for destination points
    new_poly = zeros(size(X, 2)+1, 2); % allocate output space
    new_poly(1, :) = XY_from(1, 1:2); % select an arbitrary starting point
    for i=2:size(new_poly, 1)
      index = find(all([new_poly(i-1, :) 0] == XY_from, 2), 1); % search the previous (unused) point in the starting points
      if isempty(index) 
        index = find(all([new_poly(i-1, :) 0] == XY_to, 2), 1); % if no point was found, search it in the destination points
        new_poly(i, :) = XY_from(index, 1:2); % add the new point (corresponding with the found destination point)
      else
        new_poly(i, :) = XY_to(index, 1:2); % add the new point (corresponding with the found start point)
      end
      XY_from(index, 3) = 1; % indicate that this start point is used
      XY_to(index, 3) = 1; % indicate that this destination point is used
    end
    
    hold on
    plot(new_poly(:, 1), new_poly(:, 2), '--k', 'LineWidth', 2); % plot on top of the polygon
    

    0 讨论(0)
提交回复
热议问题