📄 angle_cal.m
字号:
% Taken from www.cs.ucr.edu/~eamonn/205/angle_cal.m
% given 3 points; return 0~2pi angle value of their inner angle
% the coordinate is based on the image coordinate. 0,0 is upper
% left corner
% function angle_cal
% Left = [0;2];
% Middle = [1; 3];
% Right = [0.5; 3];
% angle1 = angle1(Left,Middle,Right);
function real_angle = angle_cal(Left, Middle, Right)
%Left = [0, 2];
%Middle = [1, 3];
%Right = [0.5, 3];
if ((Left(1)==Middle(1))&&(Left(1)==Right(1))||((Left(2)==Middle(2))&&(Left(2)==Right(2))))
real_angle = pi;
else
LM = sqrt ((Left(1) - Middle(1))^2 + (Left(2) - Middle(2))^2);
MR = sqrt ((Right(1) - Middle(1))^2 + (Right(2) - Middle(2))^2);
LR = sqrt ((Left(1) - Right(1))^2 + (Left(2) - Right(2))^2);
cosval = (LM^2 + MR^2 - LR^2)/(2*LM*MR);
inner_angle = acos(cosval);% get the angel of such inner angle[0, pi] of the two lines
% determin whether such angle will be bigger than pi or not.
if Left(1) == Middle(1) % horizonal condition
if Left(2) > Middle(2) % <---
if Right(1)<Left(1) % above the line by "left and middle"
real_angle = inner_angle; % [0~pi]
else real_angle = 2*pi - inner_angle; % below the line [pi ~ 2pi]
end;
elseif Left(2) < Middle(2) % ---->
if Right(1)<Left(1) % above the line by "left and middle"
real_angle = 2 * pi - inner_angle; % [pi ~ 2pi]
else real_angle = inner_angle; % below the line [0 ~ pi]
end;
end;
else % dealing with the vertical cases
% testy is used to test the right point is in left or right side of the line
% testy = (y1-y2)/(x1-x2) * x3 + (x1*y2-x2*y1)/(x1-x2)
testy = ((Right(1) - Middle(1))*Left(2) + (Left(1) - Right(1)) * Middle(2))/(Left(1) - Middle(1));
if Left(1) > Middle(1) % raising trend
if Right(2) < testy % left to the line [pi ~ 2*pi]
real_angle = 2 * pi - inner_angle;
elseif Right(2) > testy
real_angle = inner_angle;
else real_angle = pi;
end;
else % down trend
if Right(2) < testy % left to the line [pi ~ 2*pi]
real_angle = inner_angle;
elseif Right(2) > testy
real_angle = 2 * pi - inner_angle;
else real_angle = pi;
end;
end;%raising trend
end;%horizonal condition
if (real_angle<0 || real_angle>2*pi)
disp('Angle caution!!');
pause;
end;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -