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