0

Knowing center of the two rectangles and their angle by x axis (horizontal axis), how can one recognize if their intersection is zero or not in Matlab? Any answers containing this information is highly appreciated. Width and length of rectangles are also known

Oliver Range
  • 365
  • 3
  • 10
  • 2
    This is a math question not a MATLAB question. – Ander Biguri May 26 '17 at 11:16
  • But I want to solve it by matlab @AnderBiguri Do you know what can I do? – Oliver Range May 26 '17 at 11:19
  • 1
    First figure out the math problem, then come here, describe it and tell us what are you having the programming problems with it. I write my maths in paper, but I do not ask a paper company for solutions because the fact that I am doing it in paper doesn't mean it is a paper problem. Replace paper by MATLAB. – Ander Biguri May 26 '17 at 11:26
  • You are right. THhank you for your help @AnderBiguri – Oliver Range May 26 '17 at 11:30
  • Could anyone answer this question? – Oliver Range May 26 '17 at 11:45
  • I'm voting to close this question as off-topic because it is a maths problem, not a programming problem – Dan May 26 '17 at 11:49
  • Possible duplicate [https://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles](https://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles) – Wolfie May 27 '17 at 07:44

1 Answers1

0

This is a programming problem if you want to solve it numerically. For exact solutions, you could use geometrical equations.


The first problem: defining a rectangle's corners from its width, height and centre:

C1 = [0, 0];    % Centre of rectangle 1 (x,y)
C2 = [1, 1];    % Centre of rectangle 2 (x,y)
W1 = 5; W2 = 3; % Widths of rectangles 1 and 2
H1 = 2; H2 = 3; % Heights of rectangles 1 and 2
% Define the corner points of the rectangles using the above
R1 = [C1(1) + [W1; W1; -W1; -W1]/2, C1(2) + [H1; -H1; -H1; H1]/2];
R2 = [C2(1) + [W2; W2; -W2; -W2]/2, C2(2) + [H2; -H2; -H2; H2]/2];

Next problem is to create many points which represent the edges of the rectangles. You could instead generate many points within the rectangles if you wanted to look at intersecting areas.

n = 1000;       % Define some number of points to use
% Use interp1 to interpolate around the rectangles
R1points = interp1(1:5, [R1; R1(1,:)], linspace(1,5,n));
R2points = interp1(1:5, [R2; R2(1,:)], linspace(1,5,n));

Then rotate the rectangles:

a1 = deg2rad(0); a2 = deg2rad(30); % angles of rotation for rectangle 1 and 2 respectively
R1rotated(:,1) = (R1points(:,1)-C1(1))*cos(a1) - (R1points(:,2)-C1(2))*sin(a1) + C1(1);
R1rotated(:,2) = (R1points(:,1)-C1(1))*sin(a1) + (R1points(:,2)-C1(2))*cos(a1) + C1(2);
R2rotated(:,1) = (R2points(:,1)-C2(1))*cos(a2) - (R2points(:,2)-C2(2))*sin(a2) + C2(1);
R2rotated(:,2) = (R2points(:,1)-C2(1))*sin(a2) + (R2points(:,2)-C2(2))*cos(a2) + C2(2);

Finally, check intersection with inpolygon:

in1 = inpolygon(R1rotated(:,1), R1rotated(:,2), R2rotated(:,1), R2rotated(:,2));
in2 = inpolygon(R2rotated(:,1), R2rotated(:,2), R1rotated(:,1), R1rotated(:,2));

If nnz(in1)>0 or nnz(in2)>0 then you have an intersection! Visualise it using scatter:

hold on
scatter(R2rotated(:,1),   R2rotated(:,2),   '.b')
scatter(R2rotated(in2,1), R2rotated(in2,2), 'xc')
scatter(R1rotated(:,1),   R1rotated(:,2),   '.r')
scatter(R1rotated(in1,1), R1rotated(in1,2), 'xg')

Result:

enter image description here

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • A really nice answer, but is a there simpler way to do that without rotation? – Oliver Range May 26 '17 at 12:08
  • You say in the question that you wanted rotation? You can leave out the rotation lines or set `a1` and `a2` to 0 if you want to use rectangles which are orthogonal to the axes – Wolfie May 26 '17 at 13:07
  • There are simpler ways even with rotation. – Jed May 26 '17 at 19:50
  • Feel free to add your own answer @Jed if you can improve on this, this is just the first way which came to my mind – Wolfie May 26 '17 at 20:20
  • I posted my matlab implementation here: https://stackoverflow.com/a/44210241/5535270 – Jed May 26 '17 at 21:19
  • How about this method? Posted the link:https://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles/44216173#44216173 – Siva Srinivas Kolukula May 27 '17 at 11:15