【问题标题】:Finding the most common point of intersection among plotted triangles在绘制的三角形中找到最常见的交点
【发布时间】:2016-10-01 11:16:04
【问题描述】:

我使用下面的代码绘制了一组三角形:

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

问题是,我希望该图能够指示交叉点数量最多的区域。在这个特定的代码中,该区域是黑色多边形(我必须手动指示该区域)。

请任何人帮助解决这个问题。谢谢

【问题讨论】:

    标签: matlab


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2021-09-14
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      相关资源
      最近更新 更多