为什么plot和patch命令都不能在Matlab中执行?

时间:2016-09-26 16:41:04

标签: matlab

所以我的任务是在黄色矩形的顶部绘制金色矩形和金色螺旋。我们得到了矩形的代码,对于“螺旋”,我使用了forum post中的多个弧,我只能使用iffor语句。矩形的代码工作,螺旋的代码几乎可以工作,但我真正需要的是让patch显示在plot后面。这是我现在的代码:

clear variables; close all; clc

phi = (1+sqrt(5))/2;
x0 = 0;
y0 = 0;

for i = 1:8
    edgeLength = 1/(phi^(i-1));
    moveLength = 1/(phi^i);
    modStep = mod(i,4);
    if (modStep == 1) %move W, orient NW
       x0 = x0 - moveLength; 
       sx = [x0 x0 x0-edgeLength x0-edgeLength];
       sy = [y0 y0+edgeLength y0+edgeLength y0];

       %spiral
       P1 = [(x0-edgeLength);y0];
       P2 = [x0;(y0+edgeLength)];
       P0 = [x0;y0];
       n = 1000;
       v1 = P1-P0;
       v2 = P2-P0;
       c = det([v1,v2]);
       a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
       v3 = [0,-c;c,0]*v1;
       v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
       if i == 1
           vx1 = v(1,:);
           vy1 = v(2,:);   
       elseif i == 5
           vx6 = (v(1,:)+(1/(phi^5)));
           vy6 = (v(2,:)+(1/(phi^5))); 
       end
    end
    if (modStep == 2) %move N, orient NE
       y0 = y0 + moveLength;
       sx = [x0 x0+edgeLength x0+edgeLength x0];
       sy = [y0 y0 y0+edgeLength y0+edgeLength];

       %spiral
       P1 = [x0;(y0+edgeLength)];
       P2 = [(x0+edgeLength);y0];
       P0 = [x0;y0];
       n = 1000;
       v1 = P1-P0;
       v2 = P2-P0;
       c = det([v1,v2]);
       a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
       v3 = [0,-c;c,0]*v1;
       v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
       if i == 2
           vx2 = v(1,:);
           vy2 = (v(2,:)+(1/(phi^2)));   
       elseif i == 6
           vx5 = (v(1,:)+(1/(phi^5)));
           vy5 = (v(2,:)+(1/(phi^6))); 
       end
    end
    if (modStep == 3) %move E, orient SE
       x0 = x0 + moveLength;
       sx = [x0 x0 x0+edgeLength x0+edgeLength];
       sy = [y0 y0-edgeLength y0-edgeLength y0];

       %spiral
       P1 = [(x0+edgeLength);y0];
       P2 = [x0;(y0-edgeLength)];
       P0 = [x0;y0];
       n = 1000;
       v1 = P1-P0;
       v2 = P2-P0;
       c = det([v1,v2]);
       a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
       v3 = [0,-c;c,0]*v1;
       v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
       if i == 3
           vx3 = (v(1,:)+(1/(phi^3)));
           vy3 = (v(2,:)+(1/(phi^2)));   
       elseif i == 7
           vx7 = (v(1,:)+(1/(phi^7)));
           vy7 = (v(2,:)+(1/(phi^6))); 
       end
    end
    if (modStep == 0) %move S, orient SW
       y0 = y0 - moveLength;
       sx = [x0 x0-edgeLength x0-edgeLength x0];
       sy = [y0 y0 y0-edgeLength y0-edgeLength];

       %spiral
       P1 = [(x0-edgeLength);y0];
       P2 = [x0;(y0-edgeLength)];
       P0 = [x0;y0];
       n = 1000;
       v1 = P1-P0;
       v2 = P2-P0;
       c = det([v1,v2]);
       a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
       v3 = [0,-c;c,0]*v1;
       v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
       if i == 4
           vx4 = (v(1,:)+(1/(phi^3)));
           vy4 = (v(2,:)+(1/(phi^3)));   
       elseif i == 8
           vx8 = (v(1,:)+(1/(phi^7)));
           vy8 = (v(2,:)+(1/(phi^7))); 
       end
    end
end
patch(sx,sy,0.8+0.2*rand(1,3));
vx = [vx1 vx2 vx3 vx4 vx5 vx6 vx7 vx8];
vy = [vy1 vy2 vy3 vy4 vy5 vy6 vy7 vy8];
plot(vx,vy);
axis equal

在代码中,有关North,West等的注释是参考显示的矩形及其方向。组成螺旋的代码部分同样被标记。

所以我真的有两个问题,但是,最紧迫的问题是为什么只有plot(vx,vy)命令才有效而不是patch?我试着查看其他事情的例子,但他们似乎只是按照他们希望打印的顺序输入每个命令,它就是这样做的。

另一个问题我得知,当我运行此代码时,弧对i = 1:4完全有效,但在那之后,你可以看到,这些线都搞砸了。我真的不知道为什么会这样。 如果我遗漏了任何其他信息,我很抱歉! 谢谢

0 个答案:

没有答案