原帖由 eight 于 2007-11-1 19:59 发表
help inpolygon
谢谢,这个不错,呵呵,就用这个命令了。另外刚才我自己写了一个,还是利用循环的,两个的实现都贴上来吧。
- %%
- close all;
- points=round(rands(50,2)*100);
- figure(1);plot(points(:,1),points(:,2),'o');
- minpoint=points(5,:); xmin=minpoint(1);ymin=minpoint(2);
- maxpoint=points(10,:);xmax=maxpoint(1);ymax=maxpoint(2);
- figure(1);hold on;plot(xmin,ymin,'r*');
- figure(1);hold on;plot(xmax,ymax,'r*');
- figure(1);hold on;plot([xmin xmax],[ymin ymin]);
- figure(1);hold on;plot([xmin xmax],[ymax ymax]);
- figure(1);hold on;plot([xmin xmin],[ymin ymax]);
- figure(1);hold on;plot([xmax xmax],[ymin ymax]);
- interpointsnum=0;
- for ii=1:size(points,1);
- xtemp = points(ii,1); ytemp=points(ii,2);
- if (xtemp-xmin)*(xtemp-xmax)<=0
- if (ytemp-ymin)*(ytemp-ymax)<=0
- interpointsnum = interpointsnum+1;
- end
- end
- end
- x=points(:,1);y=points(:,2);
- xv=[xmin xmin xmax xmax xmin];yv=[ymin ymax ymax ymin ymin];
- in = inpolygon(points(:,1),points(:,2),xv,yv);
- plot(xv,yv,x(in),y(in),'r+',x(~in),y(~in),'bo')
- insize=length(in)
- interpointsnum
复制代码 |