【问题标题】:How to plot the overlapping area of figures in Matlab?如何在matlab中绘制图形的重叠区域?
【发布时间】:2014-05-07 14:09:21
【问题描述】:

我想用一种颜色绘制 2 个矩形的重叠区域。我知道我可以使用 rectangle 命令绘制矩形。使用 rectint 我可以找出它们是否重叠。

是否有特定的命令或者有谁知道我该怎么做?正如您所注意到的,我对 Matlab 没有太多经验。

代码:

     A = [0 0 3 3];
     B = [2 2 2 2];

     hold on;
     rectangle('Position',A) %plot rectangle A
     rectangle('Position',B) %plot rectangle B
     if (rectint(A,B) > 0)
          %plot overlapping
     end
     hold off;

图片:

【问题讨论】:

  • 半透明矩形不是一个好的解决方案吗?
  • 因为我将使用它来构建本地化地图,所以我宁愿用一个正方形来代表信念。

标签: matlab plot overlap figure


【解决方案1】:

假设矩形重叠,绘图部分可以这样完成:

    if (A(1)<=B(1))
        intersection(1)=B(1);
        intersection(3)=A(1)+A(3)-B(1);
    else
        intersection(1)=A(1);
        intersection(3)=B(1)+B(3)-A(1);
    end

    if (A(2)<=B(2))
        intersection(2)=B(2);
        intersection(4)=A(2)+A(4)-B(2);
    else
        intersection(2)=A(2);
        intersection(4)=B(2)+B(4)-A(2);
    end

    intersectionPlot=rectangle('Position', intersection);
    set(intersectionPlot, 'FaceColor', 'r'); % r stands for red, you can choose any other color

【讨论】:

  • 显然正如 Dennis Jaheruddin 所说,这没有任何功能。这个自写的代码完美运行。非常感谢你
【解决方案2】:

所以你只想绘制中间的小矩形? 我不认为有一个内置函数,这可能是要走的路:

  1. 确定交点的坐标(只要是矩形就可以了,这不应该太难)
  2. 绘制结果

【讨论】:

  • 我注意到Matlab中有很多函数,但就是找不到这个。当您说没有此功能时,我认为您是对的。对于那些想要矩形示例的人来说,nbanic 的解决方案非常完美!
猜你喜欢
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多