简体   繁体   English

matlab 矩形 plot 带矩阵

[英]matlab rectangle plot with a matrices

I am plotting rectangles.我正在绘制矩形。 I want the rectangles to be drawn starting from the biggest rectangle to the smallest.The problem with my code is that I keep on concentric rectangle in which the gap to the width is less than the gap to the length.我希望从最大的矩形开始绘制矩形到最小的矩形。我的代码的问题是我保持同心矩形,其中宽度的间隙小于长度的间隙。 if you want to know more put a breakpoint in line 81 and run each loop to see what i mean.如果您想了解更多信息,请在第 81 行放置一个断点并运行每个循环以了解我的意思。 look at how each succeeding rectangle is plotted.查看每个后续矩形是如何绘制的。 You will see that the gap between the widths of the old and new rectangles is less than the length gaps.您会看到新旧矩形的宽度之间的差距小于长度差距。 I a providing with code so that you can help.我提供代码以便您提供帮助。 help is greatly appreciated.非常感谢您的帮助。

    gap=(22.2/20)*10^(-3);
h=(0:7)*10^(-3);
n=20;
a=-59.9*10^(-3);
b=-5.5*10^(-3);
c=-125*10^(-3);
d=125*10^(-3);




a2=59.9*10^(-3);
b2=5.5*10^(-3);
c2=-125*10^(-3);
d2=125*10^(-3);




for i=1:length(h)
for j=1:n
%line 1
y1=c+gap*(j-1):0.1*10^-(3):d-gap*(j-1);
x1=ones(1,length(y1))*(a+gap*(j-1));

z1=ones(1,length(y1))*h(i);


lx1=ones(1,length(c2+gap*(j-1):0.1*10^(-3):d2-gap*(j-1)))*(a2-gap*(j-1));
ly1=c2+gap*(j-1):0.1*10^-(3):d2-gap*(j-1);
lz1=ones(1,length(ly1))*h(i);



%line 2
x2=a+gap*(j-1):0.1*10^-(3):b-gap*(j-1);
y2=ones(1,length(x2))*d-gap*(j-1);
z2=ones(1,length(y2))*h(i);



lx2=a2-gap*(j-1):-0.1*10^-(3):b2+gap*(j-1);
ly2=ones(1,length(lx2))*(d2-gap*(j-1));
lz2=ones(1,length(ly2))*h(i);


%line 3
y3=d-gap*(j-1):-0.1*10^-(3):c+gap*(j-1);
x3=ones(1,length(y3))*(b-gap*(j-1));

z3=ones(1,length(y3))*h(i);


ly3=d2-gap*(j-1):-0.1*10^-(3):c2+gap*(j-1);
lx3=ones(1,length(ly3))*(b2+gap*(j-1));

lz3=ones(1,length(ly3))*h(i);
%line 4
x4=b-gap*(j-1):-0.1*10^-(3):a+gap*(j-1);
y4=ones(1,length(x4))*c+gap*(j-1);
z4=ones(1,length(x4))*h(i);



lx4=b2+gap*(j-1):0.1*10^-(3):a2-gap*(j-1);
ly4=ones(1,length(lx4))*c2+gap*(j-1);
lz4=ones(1,length(lx4))*h(i);


u=[x1 x2 x3 x4];
v=[y1 y2 y3 y4];
w=[z1 z2 z3 z4];

u1=[lx1 lx2 lx3 lx4];
v1=[ly1 ly2 ly3 ly4];
w1=[lz1 lz2 lz3 lz4];
plot3(u1,v1,w1);
hold on
plot3(u,v,w); 
hold on
end
end

Your code is way too complicated for what it is supposed to do.您的代码对于它应该做的事情来说太复杂了。 A rectangle can be defined with only 5 points A,B,C,D,A, where the last point close the rectangle.一个矩形只能用 5 个点 A,B,C,D,A 定义,其中最后一个点关闭矩形。

Here is an example where I use a scaling matrix to apply the same gap between each rectangle:这是一个示例,我使用缩放矩阵在每个矩形之间应用相同的间隙:

%% Creation of the variables
gap = 0.1;                     % gap between each rectangle
w = 1;                         % width
l = 2;                         % length
x = [-w/2 w/2 w/2 -w/2 -w/2];  % zero centered x-coordinate
y = [-l/2 -l/2 l/2 l/2 -l/2];  % zero centered y-coordinate
z = [1 1 1 1 1];               % z coordinate

%% Plot each rectangle
for ii = 0:5
    S = diag([1-2*ii*gap/w, 1-2*ii*gap/l, 1]) % scaling matrix where the gap is ponderated with the widht/length of our rectangle
    C = S*[x;y;z];                            % apply the scaling
    plot3(C(1,:),C(2,:),C(3,:))
    hold on
end

axis equal

Result:结果:

在此处输入图像描述

You can easily shift a rectangle with:您可以使用以下方法轻松移动矩形:

plot3(C(1,:)+shift_x,C(2,:)+shift_y,C(3,:))

So basically when you need to transform a geometry, the easiest way is often to use a transformation matrix.所以基本上当你需要变换几何体时,最简单的方法通常是使用变换矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM