简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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