if pc_inter.size<=0 || pc1.size<=0
proportion = single(0);
else
proportion = computeArea(pc_inter)/computeArea(pc1);
end
% proportion = polyarea([pc_inter.x],[pc_inter.y])/polyarea([pc1.point.x],[pc1.point.y]);
if proportion>threshold
ret=1;
else
ret=0;
end
% plot([pc1.point.x pc1.point(1).x],[pc1.point.y pc1.point(1).y],'b',[pc2.point.x pc2.point(1).x],[pc2.point.y pc2.point(1).y],'g',[pc_inter.point.x pc_inter.point(1).x],[pc_inter.point.y pc_inter.point(1).y],'r')
end
function res=intersectPolygonSHPC(Slot1,Slot2)
n=Slot1.size;
point.x=single(0);
point.y=single(0);
res.point=repmat(point,16,1);
% res.size = uint8(0);
res.size = 0;
p1=res;
dir = left_of(Slot2.point(1), Slot2.point(2), Slot2.point(3));
Slot1_temp=res;
Slot2_temp=res;
for k=1:Slot1.size
Slot1_temp.point(k).x=Slot1.point(k).x;
Slot1_temp.point(k).y=Slot1.point(k).y;
Slot1_temp.size=Slot1.size;
end
for k=1:Slot2.size
Slot2_temp.point(k).x=Slot2.point(k).x;
Slot2_temp.point(k).y=Slot2.point(k).y;
Slot2_temp.size=Slot2.size;
end
p2=poly_edge_clip(Slot1_temp,Slot2_temp.point(n), Slot2_temp.point(1), dir);
for i =1:n-1
tmp = p2;
p2 = p1;
p1 = tmp;
p2=poly_edge_clip(p1, Slot2_temp.point(i), Slot2_temp.point(i+1), dir);
end
index=0;
for i =1:p2.size
index=index+1;
res.point(index)=p2.point(i);
res.size = res.size + 1;
end
end
function res=poly_edge_clip(sub,x0,x1,left)
n=sub.size;
point.x=single(0);
point.y=single(0);
res.point=repmat(point,16,1);
res.size = 0;
if n==0
return;
end
v0=sub.point(n);
side0 = left_of(x0, x1, v0);
if side0 ~=-left
res.size=res.size+1;
res.point(res.size).x=v0.x;
res.point(res.size).y=v0.y;
end
for i=1:n
v1 = sub.point(i);
side1 = left_of(x0, x1, v1);
if ((side0 + side1 == 0 )&& side0)
% /* last point and current straddle the edge */
[ret,temp]=line_sect(x0, x1, v0, v1);
if (ret)
res.size=res.size+1;
res.point(res.size)=temp;
end
end
if (i == n)
break;
end
if (side1 ~= -left)
res.size=res.size+1;
res.point(res.size).x=v1.x;
res.point(res.size).y=v1.y;
end
v0 = v1;
side0 = side1;
end
end
function [ret,temp]=line_sect(x0,x1,y0,y1)
ret = 1;
temp.x = single(0);
temp.y = single(0);
temp.x=single(0);
temp.y=single(0);
temp.angle=single(0);
pc.point=repmat(temp,16,1);
pc.size = pt_num;
for i = 1:pt_num
pc.point(i).x = pt(i).x;
pc.point(i).y = pt(i).y;
pc.point(i).angle = atan2((pt(i).y - center.y), (pt(i).x - center.x));
end
pc.point=qsort(pc.point, pt_num, 1);%按角度升序排列,直角坐标系X轴为0度 从-pi到pi排列
for i = 1:pt_num
pt(i).x = pc.point(i).x;
pt(i).y = pc.point(i).y;
end
end
function pc=pointsOrdered2(pt)
n=pt.size;
temp.x=single(0);
temp.y=single(0);
temp.angle=single(0);
pc.point=repmat(temp,16,1);
pc.size = n;
if( n <= 0)
return;
end
center.x = single(0);
center.y = single(0);
for ii = 1:n
center.x = center.x + pt.point(ii).x;
center.y = center.y + pt.point(ii).y;
end
center.x = center.x/single(n);
center.y = center.y/single(n);
for i = 1:n
pc.point(i).x = pt.point(i).x;
pc.point(i).y = pt.point(i).y;
pc.point(i).angle = atan2((pt.point(i).y - center.y), (pt.point(i).x - center.x));
end
pc.point=qsort(pc.point, n, 1);
for i = 1:n
pt.point(i).x = pc.point(i).x;
pt.point(i).y = pc.point(i).y;
end
end
%comp_point_with_angle=1 升序,-1 降序
function ret=qsort(pc,n,comp_point_with_angle)
if comp_point_with_angle==1
for i=1:n
for j=i:n
if pc(i).angle>pc(j).angle
temp=pc(i);
pc(i)=pc(j);
pc(j)=temp;
end
end
end
else
for i=1:n
for j=i+n
if pc(i).angle<pc(j).angle
temp=pc(i);
pc(i)=pc(j);
pc(j)=temp;
end
end
end
end
ret=pc;
end
function area0=computeArea(pt)
area0 = single(0);
n=pt.size;
if n <= 0
return;
end