如果matlab中两个矩形的交点为零
问题描述:
通过x轴(水平轴)了解两个矩形的中心和它们的角度,在Matlab中如何识别它们的交点是否为零?任何包含这些信息的答案都是非常感谢的矩形的宽度和长度也是已知的如果matlab中两个矩形的交点为零
答
这是一个编程问题,如果你想解决它的数值。对于确切的解决方案,您可以使用几何方程。
第一个问题:从它的宽度,高度和中心限定一矩形的角落:
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')
结果:
这是一个数学问题不是一个问题MATLAB。 –
但我想通过matlab @AnderBiguri解决它你知道我能做什么吗? –
首先找出数学问题,然后来到这里,描述它并告诉我们你有什么编程问题。我在纸上写数学,但是我没有要求纸业公司寻求解决方案,因为我在纸上做这件事并不意味着它是纸面问题。用MATLAB替换纸张。 –