user5739619
user5739619

Reputation: 1838

How to remove interior edges from objects in Matlab?

Let's say I have an array of objects objects and each object contains vertices

So for example, let's say I have 2 objects, each containing 4 vertices, and I plot the vertices and connect them using a line in Matlab:

size_vertices=size(vertices);

for o=1:length(objects)
    for v=1:size_vertices(1)-1
        plot([vertices(o, v,1) vertices(o, v+1,1)], [vertices(o,v,2) vertices(o,v+1,2)], '-k' )
    end
end

So let's say vertices(1,:,:)=[1 5; 1 1; 5 1; 4 6]

and vertices(2,:,:)=[5 10; 3 8; 1 5; 4 6]

Now, let's say I have an arbitrary number of objects (for example, in order from 1-6 in the diagram below), and each object could contain an arbitrary number of vertices (not just 4 like in the example above). (The ellipses are not part of the edges, they are just there to indicate where the vertices are)

Is there a way to remove the inner edges using Matlab?

Upvotes: 1

Views: 81

Answers (1)

Hunter Jiang
Hunter Jiang

Reputation: 1300

As Cris noticed in comment, what we need is calculate the appearance of each edges, and only plot those appeared for only once. Function tabulate enables us to achieve that:

% Test Data
clc; clear;
vertices{1}=[1 5; 1 1; 5 1; 4 6];
vertices{2}=[5 10; 3 8; 1 5; 4 6];

% To ensure every objects are closed
for ii=1:length(vertices)
  vertices{ii}=[vertices{ii}; vertices{ii}(1,:)];  
end

% Save Egdes
edges{1}=num2str([0 0 0 0]);
for o=1:length(vertices)
    for v=1:size(vertices{o},1)-1
        tmp1=[vertices{o}(v,1) vertices{o}(v+1,1)];
        tmp2=[vertices{o}(v,2) vertices{o}(v+1,2)];
        if tmp1(1) == tmp1(2)
            tmp2=sort(tmp2);
        end
        [~,indi]=sort(tmp1);
        edges{end+1}=num2str([tmp1(indi) tmp2(indi)]);
    end
end

% Calcualte the number of appearence of each edges
sta=tabulate(edges);
figure(1); hold on
for ii=2:size(sta,1)
  if sta{ii,2} > 1
      continue
  end
  tmp=str2num(sta{ii,1});
  plot(tmp(1:2),tmp(3:4), '-k' )
end
axis([0 6 0 11])

Note that I changed vertices into a CellArray, as there is a triangle, not only quadrangles in your final result.

Upvotes: 1

Related Questions