% 输入参数
clear all;
clc;
poly1_x = [6.2 0 4 5.1 6.2];
poly1_y = [6.7 3 4.5 1.5 6.7];% 待裁剪的多边形
nVertex = length(poly1_x)-1; % 多边形顶点数
poly2_x = [0 4.5 10 5.1 0]; poly2_y = [5 0 5 2.8 5]; % 多边形裁剪窗口(窗口边界为逆时针方向)
mVertex = length(poly2_x)-1;%裁剪多边形顶点数
% 设置绘图环境
hold on;
axis equal;
grid on;
% 画出要裁剪的多边形和裁剪窗口
plot(poly1_x,poly1_y,'b-','LineWidth',2);
for i = 1:nVertex
text(poly1_x(i),poly1_y(i),['P' num2str(i) '(' num2str(poly1_x(i)) ',' num2str(poly1_y(i)) ')'], 'FontSize',10,'Color',[0,1,1]);
end
plot(poly2_x,poly2_y,'g-','LineWidth',1.5);
for j = 1:mVertex
text(poly2_x(j),poly2_y(j),['O' num2str(j) '(' num2str(poly2_x(j)) ',' num2str(poly2_y(j)) ')'], 'FontSize',10,'Color',[0,0.5,1]);
end
title('Weiler-Atherton多边形裁剪算法演示');
%pause;
poly1_x=poly1_x';
poly2_x=poly2_x';
poly1_y=poly1_y';
poly2_y=poly2_y';
S(:,1) = [poly1_x;poly2_x]; % 将两个多边形的坐标存入 S 中,顺序无所谓
S(:,2) = [poly1_y;poly2_y];
num = size(poly1_x,1)+size(poly2_x,1)+1;
for i = 1:size(poly1_x,1) - 1
for j =1:size(poly2_x,1)-1
X1 = [poly1_x(i);poly1_x(i+1)];
Y1 = [poly1_y(i);poly1_y(i+1)];
X2 = [poly2_x(j);poly2_x(j+1)];
Y2 = [poly2_y(j);poly2_y(j+1)];
[intspoint_x,intspoint_y] = polyxpoly(X1,Y1,X2,Y2); % 求两条线段交点的x,y坐标
%intspoint_x
% intspoint_y
if ~isempty(intspoint_x) % 若两条线段无交点则跳至下一组线段,若有交点则将交点的x,y坐标存至S中
S(num,1) = intspoint_x;
S(num,2) = intspoint_y;
num = num + 1; % 存入 S 后往下递推一行
end
end
end
IN = inpolygon(S(:,1),S(:,2),poly1_x,poly1_y);
S(IN == 0,:) = []; % 剔除掉不位于多边形 A 中的顶点坐标
IN = inpolygon(S(:,1),S(:,2),poly2_x,poly2_y);
S(IN == 0,:) = []; % 剔除掉不位于多边形 B 中的顶点坐标
X = S(:,1); % 得到交集多边形的各个顶点坐标
Y = S(:,2);
S_1 = [1.9452 1.1710 1.3458 2.4802 4.5338 5.4039];
S_2 = [4.1609 3.6988 3.5047 3.9301 3.0443 2.9364];
out1_poly_x = [6.2 1.9452 1.1710 0 1.3458 2.4802 4 4.5338 5.1 5.4039 6.2];
out2_poly_x = [0 1.1710 1.3458 4.5 10 5.4039 5.1 4.5338 2.4802 1.9452 0];
out1_poly_y = [6.7 4.1609 3.6988 3 3.5047 3.9301 4.5 3.0443 1.5 2.9364 6.7];
out2_poly_y = [5 3.6988 3.5047 0 5 2.9364 2.8 3.0443 3.9301 4.1609 5];
out11_poly_x = [6.2 0 4 4.5338 5.1 5.4039 6.2];%将交点坐标按顺序插入裁剪多边形和边框多边形中
out22_poly_x = [0 4.5 10 5.4039 5.1 4.5338 0];
out11_poly_y = [6.7 3 4.5 3.0443 1.5 2.9364 6.7];
out22_poly_y = [5 0 5 2.9364 2.8 3.0443 5];
for i = 1: 6
for j = 1: 11
if (rem(i, 2) == 1) && (S_1(i) == out1_poly_x(j))
Final_poly_x(i) = out1_poly_x(j);
Final_poly_y(i) = out1_poly_y(j);
Final_poly_x(i + 1) = out1_poly_x(j + 1);
Final_poly_y(i + 1) = out1_poly_y(j + 1);
end
end
for j =1:11
if (rem(i, 2) == 0) && (S_1(i) == out2_poly_x(j))
Final_poly_x(i) = out2_poly_x(j);
Final_poly_y(i) = out2_poly_y(j);
if out2_poly_x(j + 1) == S_1(1)
Final_poly_x(i + 1) = Final_poly_x(1);
Final_poly_y(i + 1) = Final_poly_y(1);
flag = 1;
end
end
end
if flag == 1 break;
end
end
%------------------------------------------------------------------------------------
S_1 = [4.5338 5.4039];
S_2 = [3.0443 2.9364];
for k = 1: 6
for j = 1: 7
if (rem(k, 2) == 1) && (S_1(k) == out11_poly_x(j))
Final_poly_x1(k) = out11_poly_x(j);%创建一个数组存储最终输出的点
Final_poly_y1(k) = out11_poly_y(j);
Final_poly_x1(k + 1) = out11_poly_x(j + 1);
Final_poly_y1(k + 1) = out11_poly_y(j + 1);
end
end
for j =1:7
if (rem(k, 2) == 0) && (S_1(k) == out22_poly_x(j))
Final_poly_x1(k + 1) = out22_poly_x(j);
Final_poly_y1(k + 1) = out22_poly_y(j);
Final_poly_x1(k + 2) = out22_poly_x(j + 1);
Final_poly_y1(k + 2) = out22_poly_y(j + 1);
if out22_poly_x(j + 2) == S_1(1)
Final_poly_x1(k + 3) = Final_poly_x1(1);
Final_poly_y1(k + 3) = Final_poly_y1(1);
return ;%绘制好一个多边形的点就先跳出循环
end
end
end
end