今天在MATLAB论坛上,有一位坛友要画已知相对角度相对长度的连续线段。
查了一些资料,想了一些办法。发现利用极坐标画向量,再由向量叠加的方式进行绘制就可以解决这个问题。写一个trans_cood函数:
function [Lx,Ly] = trans_cood(x,y,RHO,R)
th = [0; RHO*pi/180];
rho = [0; R];
[xvec,yvec] = pol2cart(th,rho);
Lx(1) = x;
Ly(1) = y;
Lx(2) = x + xvec(2);
Ly(2) = y + yvec(2);
这个函数中pol2cart生成一个起始点为(0,0),角度由RHO决定,相对长度由R决定的向量。将这个向量与原点相加,得到了所需要的连续线段坐标Lx(2),Ly(2).
例:按下面这个案例画路径图:
P_x = 0; P_y = 0;
rectangle('Position', [0 0 4 4.598]);hold on;
x = 0 ; y = 0;
RHO = [90,0,300,60,300,60,300,60,300,90,90,90,180,240,120,240,120,240,120,240,90,90,90,0,300,60,300,60,300,60,0,270];
R = [0.866025403784439,0.500000000000000,1,1,1,1,1,1,1,0.866025403784439,1,0.866025403784439,0.500000000000000,1,1,1,1,1,1,1,0.866025403784439,1,0.866025403784439,0.500000000000000,1,1,1,1,1,1,0.500000000000000,0.866025403784439];
for i = 1 :32
[Lx,Ly] = trans_cood(x,y,RHO(i),R(i));
x = Lx(2); y = Ly(2);P_x =[P_x Lx(2)]; P_y = [P_y Ly(2)];
end
axis equal
plot(P_x,P_y,'o','markerfacecolor','r','markeredgecolor','k','markersize',10);
H = quiver(P_x(1:end-1),P_y(1:end-1), P_x(2:end) - P_x(1:end-1), P_y(2:end) - P_y(1:end-1));
axis([-0.5 4.5 -0.5 5]);
set(H,'color','b','maxheadsize',0.1);
box on;