www.pudn.com > convhull.zip > convhull_my.m, change:2011-05-21,size:1385b


function [xb yb]=convhull_my(x,y) 
%根据老师的思路编的,希望能给大家一个参考 
N=50; 
x=rand(N,1);y=rand(N,1); 
 
[xmax,I]=max(x); 
y_xmax=y(I); 
plot(x,y,'g*');hold on 
jiben=xmax+1i*y_xmax; 
qidian=xmax+1i*y_xmax; 
zuobiao=x+1i*y; 
xb=[xmax]; 
yb=[y_xmax]; 
axis([0 1 0 1]); 
jibenxiangliang=jiben-0; 
jibenjiao=180*imag(log(jibenxiangliang))/pi; 
zuobiaoxin=0; 
while zuobiaoxin~=qidian; 
    cha=zuobiao-jiben; 
    chajiaodu=180*imag(log(cha))/pi; 
    for i=1:length(chajiaodu) 
        if chajiaodu(i)<0 
            chajiaodu(i)=chajiaodu(i)+360; 
        else if chajiaodu(i)==0 
            chajiaodu(i)=65535; 
            end 
        end 
    end 
    jiaoducha=chajiaodu-jibenjiao; 
    for i=1:length(jiaoducha) 
        if jiaoducha(i)<0 
            jiaoducha(i)=360-jibenjiao+chajiaodu(i) 
        end 
    end; 
    [minjiaodu I]=min(jiaoducha); 
    xxin=x(I);yxin=y(I); 
    zuobiaoxin=xxin+1i*yxin; 
    xb=[xb x(I)]; 
    yb=[yb y(I)]; 
    jiben=zuobiao(I); 
    jibenjiao=chajiaodu(I); 
end 
hold on 
plot(xb,yb); 
 
%%计算面积 
ssum=0; 
for i=2:(length(xb)-2) 
      a=sqrt((xb(i)-xb(1))^2+(yb(i)-yb(1))^2); 
      b=sqrt((xb(i+1)-xb(1))^2+(yb(i+1)-yb(1))^2); 
      c=sqrt((xb(i+1)-xb(i))^2+(yb(i+1)-yb(i))^2); 
      p=(a+b+c)/2; 
      s=sqrt(p*(p-a)*(p-b)*(p-c)); 
      ssum=ssum+s; 
end 
polyarea(xb,yb) 
title(['area is ',num2str(ssum)]); 
hold off 
end