我使用下面的代码绘制了一组三角形:
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
问题是,我希望情节能够指出交叉点数量最多的地区。在这个特定的代码中,区域是黑色多边形(我必须手动指示该区域)。
任何人都可以帮忙解决这个问题。感谢
答案 0 :(得分:1)
这是一个带有更多图形方法的变体。
代码
% 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)