• 大小: 6KB
    文件类型: .m
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: Matlab
  • 标签: matlab  

资源简介

通过matalb实现图形学中的扫描线填充空心多边形程序实现

资源截图

代码片段和文件信息

clear
clc
[xy]=ginput();
%测试样例
% x=[5 10 5 0];
% y=[11 6 2 6];
len=length(x);
x1=x;
y1=y;
x1(len+1)=x(1);
y1(len+1)=y(1);
plot(x1y1‘b‘);
hold on;
%画内部空缺部分
[x2y2]=ginput();
%测试样例
% x2=[5 8 5 2];
% y2=[9 6 3 6];
len=length(x2);
x3=x2;
y3=y2;
x3(len+1)=x2(1);
y3(len+1)=y2(1);
plot(x3y3‘b‘);
%找到外部的最小值
row=find(y==min(y));
%如果有多个最小值则只取一个
if length(row)>1
    row=row(1);
end

%找到内部的最小值
row2=find(y2==min(y2));
%如果有多个最小值则只取一个
if length(row2)>1
    row2=row2(1);
end


for oy=0:0.0001:(max(y)-y(row))
    n=0;
    m=0;
    w=0;
    n2=0;
    m2=0;
    t1=y(row)+oy;
    %遍历外部的每一个点
    for i=1:1:length(y)
        c=i-1;
        h=i+1;
        if c==0
            c=length(y);
        end
        if h==length(y)+1
            h=1;
        end
        %找出该t1值穿过的线段
        if  y(i)<=t1 && y(c)>=t1
            n=n+1;
            low1(n)=i;
            up1(n)=c;
            
        end
        %找出该t1值穿过的另一条线段
        if  y(i)<=t1 && y(h)>=t1
            
            m=m+1;
            low2(m)=i;
            up2(m)=h;
            
        end
        
    end
    
    %遍历内部的每一个点
    %初始化Low2 Up2因为从底部开始划线一开始
    %不会与内部有交点
    Low2=[];
    Up2=[];
    for i=1:1:length(y2)
        c=i-1;
        h=i+1;
        if c==0
            c=length(y2);
        end
        if h==length(y2)+1
            h=1;
        end
        %找出该t1值穿过的线段
        if  y2(i)<=t1 && y2(c)>=t1
            n2=n2+1;
            Low1(n2)=i;
            Up1(n2)=c;
            
        end
        %找出该t1值穿过的另一条线段
        if  y2(i)<=t1 && y2(h)>=t1
            
            m2=m2+1;
            Low2(m2)=i;
            Up2(m2)=h;
            
        end
        
    end
    % 初始化ox1
    ox1=[];
    %求该t1值穿过外部一条线段的所有交点的x值
    for j=1:m
        
        k1=(x(low2(j))-x(up2(j)))/(y(up2(j))-y(low2(j)));
        ox=x(low2(j))-((t1-y(row))-(y(low2(j))-y(row)))*k1;
        w=w+1;
        ox1(w)=ox;
        
        if ismember(oxx)
            xb=find(x==ox);
            for k=1:length(xb)
                f=xb(k);
                if(y(f)==t1)
                    break;
                end
            end
            
            c=f-1;
            h=f+1;
            
            if c==0
                c=length(y);
            end
            
            if h==length(y)+1
                h=1;
            end
            
            if  (y(f)-y(c)) * (y(f)-y(h))<0
                xb2=find(ox1==ox);
                if length(xb2)>=2
                    ox1(w)=[];
                    w=w-1;
                end
                
            end
        end
    end
    %求该t1值穿过线段的内部一条线的所有交点的x值
    for j=1:m2
        
        k11=(x2(Low2(j))-x2(Up2(j)))/(y2(Up2(j))-y2(Low2(j)));
        ox=x

评论

共有 条评论