A、B两点构成向量AB;求AB与X正方向夹角(0-360),逆时针为正
代码如下:
clc
clear
close all;
%%%%%%%%%%%%%%%%%%%%%以下为主程序
%%%%%%%%%%%%%A、B点构成向量AB
A = [8*rand(1,1);8*rand(1,1)];%向量1A 点
B = [8*rand(1,1);8*rand(1,1)];%向量1B 点
%%%%%%%%%%%%%A、B点构成向量AB
AB = (B - A);
C = [1;0];
A = [0;0];
B = AB;
if AB(2) == 0
if AB(1) > 0
AngBAX = 0;
else
AngBAX = 180;
end
elseif AB(2) > 0
AngBAX = acosd((norm(A-B)^2+norm(A-C)^2-...
norm(B-C)^2)/(2*(norm(A-B)*norm(A-C))));
elseif AB(2) < 0
AngBAX = 180+acosd((norm(A-B)^2+norm(A+C)^2-...
norm(B+C)^2)/(2*(norm(A-B)*norm(A+C))));
end
%%%%%%%%%%%%%%%%%%%%%以上为主程序(结果:AngBAX--AB与X正向夹角)
%%%%%%%%%%%%%%%%%%%%%画图
hold on;
plot([A(1),B(1)],[A(2),B(2)],'k');
plot([0,AB(1)],[0,AB(2)],'r');
plot([-8,8],[0,0],'k--');
plot(0,0,'o');
axis([-8,8,-8,8]);