Question

I want to plot the overlapping area of 2 rectangles in a color. I know I can plot the rectangles by using the rectangle command. With rectint I can find out whether they overlap or not.

Is there a specific command for this or does anyone know how I can do this? As you have noticed, I do not have much experience with Matlab.

Code:

     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;

Image:

Was it helpful?

Solution

Assuming the rectangles overlap, the part for the plotting could be done like this:

    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

OTHER TIPS

So you only want to plot the little rectangle in the middle? I don't think that there is a builtin function for that, and this is probably the way to go:

  1. Determine the coordinates of the intersection (as long as it are just rectangles this should not be too hard)
  2. Plot the result
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top