边相关扫描线多边形填充算法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if X(2)==X(1) ET{i,k}=[max(Y1(2),Y1(1)),X(1),0]; else ET{i,k}=[max(Y1(2),Y1(1)),X(1),... (Y(2)-Y(1))/(X(2)-X(1))]; end k=k+1; end Edge(1)=1; end if Edge(sY)==0 if Y(sY)~=Y(1) if X(sY)==X(1) ET{i,k}=[max(Y1(sY),Y1(1)),X(1),0]; else ET{i,k}=[max(Y1(sY),Y1(1)),X(1),... (Y(1)-Y(sY))/(X(1)-X(sY))]; end k=k+1; end Edge(sY)=1; end end end %扫描到P1以外的点时 if A(j)~=1 X1=X;X1(sY+1)=X(1); if X(A(j)-1)<X1(A(j)+1) if Edge(A(j)-1)==0 if Y(A(j)-1)~=Y(A(j)) if X(A(j)-1)==X(A(j)) ET{i,k}=[max(Y1(A(j)-1),Y1(A(j))),X(A(j)),0]; else
ຫໍສະໝຸດ Baidu
for j=2:2:m if AET{i-1,j-1}(1,2)+AET{i-1,j-1}(1,3)<ET{i,1} (1,2)&&... AET{i-1,j}(1,2)+AET{i-1,j}(1,3)>ET{i,2}(1,2) m=m+2; for k=1:j-1 AET{i,k}=AET{i-1,k}; AET{i,k}(1,2)=AET{i-1,k}(1,2)+AET{i-1,k} (1,3); end AET{i,j}=ET{i,1}; AET{i,j+1}=ET{i,2}; for k=j+2:m AET{i,k}=AET{i-1,k-2}; AET{i,k}(1,2)=AET{i-1,k-2}(1,2)+AET{i-1,k2}(1,3); end j=m; end end end end end end for j=2:2:m for k=ceil(AET{i,j-1}(1,2)):floor(AET{i,j}(1,2)) plot(k,i,'b.','markersize',60); end end end
ET{i,k}=[max(Y1(A(j)-1),Y1(A(j))),... X(A(j)),(X(A(j))-X(A(j)-1))/(Y(A(j))Y(A(j)-1))]; end k=k+1; end Edge(A(j)-1)=1; end if Edge(A(j))==0 Y2=Y;Y2(sY+1)=Y(1); Y3=Y1;Y3(sY+1)=Y1(1); if Y2(A(j)+1)~=Y(A(j)) if X1(A(j)+1)==X(A(j)) ET{i,k}=[max(Y3(A(j)+1),Y1(A(j))),X(A(j)),0]; else ET{i,k}=[max(Y3(A(j)+1),Y1(A(j))),X(A(j)),... (X1(A(j)+1)-X(A(j)))/(Y2(A(j)+1)-Y(A(j)))]; end k=k+1; end Edge(A(j))=1; end else if Edge(A(j))==0 Y2=Y;Y2(sY+1)=Y(1); if Y2(A(j)+1)~=Y(A(j)) if X1(A(j)+1)==X(A(j)) ET{i,k}=[max(Y1(A(j)+1),Y1(A(j))),X(A(j)),0]; else ET{i,k}=[max(Y1(A(j)+1),Y1(A(j))),X(A(j)),... (X1(A(j)+1)-X(A(j)))/(Y2(A(j)+1)-Y2(A(j)))]; end k=k+1;
figure 3 X=[2,6,10,10,8,6,4,2];Y=[1,1,3,7,6,7,5,7];scanedge(X,Y)
figure 4 X=[2,6,10,10,8,6,4,2,1];Y=[1,1,3,7,6,7,5,7,6];scanedge(X,Y)
figure 5
结果分析: 前四幅图表明程序对于简单的多边形填充是正确的,但由 第五幅图可知当ET表中有超过2列时将无法正确填充,问题出 在程序结构的简单,不具有迭代性。
Change{1,j}=AET{i-1,j}; end k=2; while k~=m if Change{1,k-1}(1,2)==Change{1,k}(1,2) for j=k-1:m-2 Change{1,j}=Change{1,j+2}; end k=k-2; m=m-2; end k=k+2; end for j=1:m AET{i,j}=Change{1,j}; AET{i,j}(1,2)=Change{1,j}(1,2)+Change{1,j}(1,3); end else if isempty(ET{i,2}) if AET{i-1,1}(1,2)+AET{i-1,1}(1,3)>=ET{i,1}(1,2) AET{i,1}=ET{i,1}; for j=2:m AET{i,j}=AET{i-1,j}; AET{i,j}(1,2)=AET{i-1,j}(1,2)+AET{i-1,j}(1,3); end end if AET{i-1,m}(1,2)+AET{i-1,m}(1,3)<=ET{i,1}(1,2) AET{i,m}=ET{i,1}; for j=1:m-1 AET{i,j}=AET{i-1,j}; AET{i,j}(1,2)=AET{i-1,j}(1,2)+AET{i-1,j}(1,3); end end
if AET{i-1,1}(1,2)+AET{i-1,1}(1,3)<ET{i,1}(1,2)&&... AET{i-1,m}(1,2)+AET{i-1,m}(1,3)>ET{i,1}(1,2) for j=1:m; Change{1,j}=AET{i-1,j}; end k=2; while k~=m if Change{1,k-1}(1,2)==Change{1,k}(1,2) for j=k-1:m-2 Change{1,j}=Change{1,j+2}; end k=k-2; m=m-2; end k=k+2; end for j=1:m AET{i,j}=Change{1,j}; AET{i,j}(1,2)=Change{1,j}(1,2)+Change{1,j}(1,3); end end else if AET{i-1,1}(1,2)+AET{i-1,1}(1,3)<ET{i,1}(1,2)&&... AET{i-1,m}(1,2)+AET{i-1,m}(1,3)>ET{i,2}(1,2) if m==2 m=4; AET{i,1}=AET{i-1,1}; AET{i,1}(1,2)=AET{i-1,1}(1,2)+AET{i-1,1}(1,3); AET{i,2}=ET{i,1}; AET{i,3}=ET{i,2}; AET{i,4}=AET{i-1,2}; AET{i,4}(1,2)=AET{i-1,2}(1,2)+AET{i-1,2}(1,3); else
end Edge(A(j))=1; end if Edge(A(j)-1)==0 if Y(A(j)-1)~=Y(A(j)) if X(A(j)-1)==X(A(j)) ET{i,k}=[max(Y1(A(j)-1),Y1(A(j))),X(A(j)),0]; else ET{i,k}=[max(Y1(A(j)-1),Y1(A(j))),X(A(j)),... (X(A(j))-X(A(j)-1))/(Y(A(j))-Y(A(j)-1))]; end k=k+1; end Edge(A(j)-1)=1; end end end end end %建AET表 AET=ET; if AET{1,1}(1,2)>AET{1,2}(1,2) Change=AET{1,1}; AET{1,1}=AET{1,2}; AET{1,2}=Change; end for i=ceil(AET{1,1}(1,2)):floor(AET{1,2}(1,2)) plot(i,1,'b.','markersize',60); end m=2; for i=minY+1:maxY if isempty(ET{i,1}) for j=1:m;
测试代码与输出图像:
X=[6,8,6,2,2];Y=[7,5,1,3,6];scanedge(X,Y)
figure 1 X=[2,6,10,10,4,2];Y=[1,1,3,8,5,7];scanedge(X,Y)
figure 2 X=[2,6,10,10,4,2,1];Y=[1,1,3,8,5,7,6];scanedge(X,Y)
边相关扫描线多边形填充算法
M文件代码:
function scanedge(X,Y) [s,sY]=size(Y); maxY=max(Y); minY=min(Y); %奇异点 Y1=Y; if (Y(sY)>Y(1)&Y(2)<Y(1))|(Y(sY)<Y(1)&Y(2)>Y(1)) Y1(1)=Y(1)-1; end if (Y(sY-1)>Y(sY)&Y(1)<Y(sY))|(Y(sY-1)<Y(sY)&Y(1)>Y(sY)) Y1(sY)=Y(sY)-1; end for i=2:sY-1 if (Y(i-1)>Y(i)&Y(i+1)<Y(i))|(Y(i-1)<Y(i)&Y(i+1)>Y(i)) Y1(i)=Y(i)-1; end end %画出多边形 hold on; plot([X(1),X(sY)],[Y(1),Y(sY)],'r'); plot(X,Y,'r'); axis equal; grid on; %建ET表 Edge=zeros(sY); ET=cell(maxY,1); for i=minY:maxY A=find(Y==i);
sA=size(A); k=1; for j=1:sA(2) %扫描到P1点时 if A(j)==1 if X(sY)<X(2) if Edge(sY)==0 if Y(sY)~=Y(1) if X(sY)==X(1) ET{i,k}=[max(Y1(sY),Y1(1)),X(1),0]; else ET{i,k}=[max(Y1(sY),Y1(1)),X(1),... (X(1)-X(sY))/(Y(1)-Y(sY))]; end k=k+1; end Edge(sY)=1; end if Edge(1)==0 if Y(2)~=Y(1) if X(2)==X(1) ET{i,k}=[max(Y1(2),Y1(1)),X(1),0]; else ET{i,k}=[max(Y1(2),Y1(1)),X(1),... (Y(2)-Y(1))/(X(2)-X(1))]; end k=k+1; end Edge(1)=1; end else if Edge(1)==0 if Y(2)~=Y(1)