我想用一种颜色绘制2个矩形的重叠区域。我知道我可以使用矩形命令来绘制矩形。使用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;
图像:
假设矩形重叠,则绘制部分可以像这样完成:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句