好的,对于任何感兴趣的人或任何其他可能正在解决像我这样的问题的人,我有下面编写的最终工作代码。此功能可以执行双重任务。如果输入的坐标向量为3对,则该函数将计算三角形的面积。如果有3组以上的坐标对,则它将计算由这些坐标包围的多边形的面积。
narginchk(2,2) ;
if length(coords_x) == 3
ii = 1
jj = 1
area = sum(abs(0.5.*(coords_x(ii).*(coords_y(jj+1)-coords_y(jj+2))- ...
coords_x(ii+1).*(coords_y(jj)-coords_y(jj+2))+coords_x(ii+2).*...
(coords_y(jj)-coords_y(jj+1))))) ;
else
ii = 1:length(coords_x) -3 ;
jj = 1:length(coords_y) -3 ;
area = sum((abs(0.5.*(coords_x(1).*(coords_y(jj+1)-coords_y(jj+2)) ...
-coords_x(ii+1)...
.*(coords_y(1)-coords_y(jj+2))+coords_x(ii+2).*(coords_y(1)- ...
coords_y(jj+1)))))) ;
end
end