2017-05-26 11 views
0

2つの矩形の中心とx軸(水平軸)の角度を知ることで、Matlabの交点がゼロかどうかをどのように認識できますか?この情報を含む回答は高く評価されます。長方形の幅と長さも知られていますmatlabで2つの矩形の交点がゼロの場合

+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)>0またはnnz(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

関連する問題