给定一个用鼠标指针顺时针标记的 4 个点的交互式输入,我需要使用 Matlab 检查绘制的形状是否是四边形凸面。我看到一些人建议使用礼品包装算法。我的想法只是使用tan
,这样如果我的角度大于 180 度,则形状不是凸面。你能建议一个更好的方法吗?感谢您参考以下代码:
showImage(imageA)
hold on
% Initially, the list of points is empty.
xy = [];
n = 0;
% Loop, picking up the points.
disp('Please enter corners of place to insert image in clockwise order.')
for i = 1:4
[xi,yi] = ginput(1);
plot(xi,yi,'yo')
xy(:,i) = [xi;yi];
end
%check if this is a convex quadrillateral
a1 = ( xy(2,2) - xy(2,1) ) / ( xy(1,2) - xy(1,1) );
a2 = ( xy(2,3) - xy(2,2) ) / ( xy(1,3) - xy(1,2) );
a3 = ( xy(2,4) - xy(2,3) ) / ( xy(1,4) - xy(1,3) );
a4 = ( xy(2,1) - xy(2,4) ) / ( xy(1,1) - xy(1,4) );
tan1 = abs( atand( (a2-a1) /( 1+a1*a2) ) );
tan2 = abs( atand( (a3-a2) / (1+a3*a2) ) );
tan3 = abs( atand( (a4-a3) / (1+a4*a3) ) );
tan4 = abs( atand( (a1-a4) / (1+a1*a4) ) );
if ((tan1 > 180) | (tan2 > 180) | (tan3 > 180) | (tan4 > 180))
disp('this is not a convex quadrillateral!!')
end