2016-10-01 172 views
0

我用下面的代码绘制的一组三角形之间相交的最常见的问题:寻找绘制三角形

A=[1, 1; 1, 5; 3, 9; 4, 2;9,9]; 
plot(A(:,1),A(:,2),'oc','LineWidth',2,'MarkerSize',5); 
axis([0 10 0 10]); 
grid on 


for ii = 1:size(A, 1) - 1 
    for jj = ii + 1:size(A, 1) 
     line([A(ii, 1), A(jj, 1)], [A(ii, 2), A(jj, 2)]) 
    end 
end 

的问题是,我会喜欢的情节,表示与路口的数量最多的地区。在这个特定的代码中,该区域是黑色多边形(我必须手动指示该区域)。

enter image description here

请任何人都可以帮忙解决这个问题。谢谢

回答

1

这是一个更多图形方法的变种。

  1. 创建的网格点
  2. 检查三角形数量,一个点 里面
  3. 情节与相交 三角形

代码

% Create the combination of all points that make the triangles 
% This could be used to plot the lines as well 
N = size(A,1); 
comb = []; 
for i = 1:N-2 
    for j = i+1:N-1 
     comb = [comb; repmat([i j], N-j,1) (j+1:N)']; %#ok<AGROW> 
    end 
end 
nComb = size(comb,1); 

% Create a mesh grid 
dg = 0.1; % Resolution - tune this! 
gridEdge = [min(A);max(A)]; 
[X, Y] = meshgrid(gridEdge(1,1):dg:gridEdge(2,1), gridEdge(1,2):dg:gridEdge(2,2)); 

% Check if a point is inside each triangle 
[isInside, onEdge] = deal(zeros(numel(X),nComb)); 
for i = 1:nComb 
    [isInside(:,i), onEdge(:,i)] = inpolygon(X(:),Y(:),A(comb(i,:),1),A(comb(i,:),2)); 
end 
% Remove points on edge 
isInside = isInside - onEdge; 

% Get index of points with most intersection 
inTri = sum(isInside,2); 
idx = find(inTri == max(inTri)); 

% Plot result 
hold on 
plot(X(idx),Y(idx),'.') 
text(mean(X(idx)),mean(Y(:)),num2str(max(inTri)),'FontSize',20) 
的最多的点

enter image description here

+0

非常感谢! – Kola