如何在Matlab中绘制图形的重叠区域?

时间:2014-05-07 14:09:22

标签: matlab plot overlap figure

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

是否有针对此的特定命令或有人知道我该怎么做吗?正如您所注意到的,我对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;

图像:

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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