2017-05-26 62 views
0

通過x軸(水平軸)瞭解兩個矩形的中心和它們的角度,在Matlab中如何識別它們的交點是否爲零?任何包含這些信息的答案都是非常感謝的矩形的寬度和長度也是已知的如果matlab中兩個矩形的交點爲零

+2

這是一個數學問題不是一個問題MATLAB。 –

+0

但我想通過matlab @AnderBiguri解決它你知道我能做什麼嗎? –

+1

首先找出數學問題,然後來到這裏,描述它並告訴我們你有什麼編程問題。我在紙上寫數學,但是我沒有要求紙業公司尋求解決方案,因爲我在紙上做這件事並不意味着它是紙面問題。用MATLAB替換紙張。 –

回答

0

這是一個編程問題,如果你想解決它的數值。對於確切的解決方案,您可以使用幾何方程。


第一個問題:從它的寬度,高度和中心限定一矩形的角落:

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]; 

接下來的問題是創造許多點代表矩形的邊緣。如果您想查看相交區域,則可以在內生成許多點

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)); 

然後旋轉矩形:

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); 

最後,檢查交叉口inpolygon

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

如果nnz(in1)>0nnz(in2)>0那麼你有一個交集!使用分散它想象:

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') 

結果:

enter image description here

+0

一個非常好的答案,但有沒有更簡單的方法做到這一點沒有旋轉? –

+0

你說在你想要旋轉的問題上?如果要使用與軸正交的矩形,可以省略旋轉線或將「a1」和「a2」設置爲0 – Wolfie

+0

即使在旋轉時也有更簡單的方法。 – Jed