sutherland_hodgeman_algorithum.m

来自「自己编写的 matlab gui 可以实现任意多边形定点参数输入 然后使用Sut」· M 代码 · 共 41 行

M
41
字号
%this programme is to solve the polygen cut line function

zonepoint=[10 10];
error=0.0001;
p=[5 15;15 10;5 -5;-2 2];
[n,dim]=size(p);
pp=[p;p(1,:)];
j=1;
bittable=[1 2 4 8];
for i=1:4
    for k=1:n+1
        bit2and=2^(i-1);
        temp(k)=getzonecode(zonepoint,pp(k,:));  %get the all the point's code grouped by 1st line
        code(k)=bitand(temp(k),bit2and);
    end
    
    for k=1:n
        judge1=bitand(code(k),code(k+1));
        if judge1~=0     %this line is totally unvisible,record no points
        else 
            if code(k)==0&&code(k+1)==0   %this line is totally visible,keep the terminal point
            output(j,:)=pp(k+1,:);
            j=j+1;
            elseif code(k)==0&&code(k+1)~=0   %this line has only p1 in area while p2 outside
            sectpoint=intersectp(pp(k,:),pp(k+1,:),zonepoint,error,bit2and,0);
            output(j,:)=sectpoint;
            j=j+1;
            else %this line has only p2 in area while p1 outside
            sectpoint=intersectp(pp(k,:),pp(k+1,:),zonepoint,error,bit2and,1);
            output(j,:)=sectpoint;
            j=j+1;
            output(j,:)=pp(k+1,:);
            j=j+1;
            end
        end
    end
end

plot(p(:,1),p(:,2),'b');hold on;
plot([0 0 10 10 0],[0 10 10 0 0],'r');hold on;
plot(output(:,1),output(:,2),'g');grid on;      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?