要求用单调链法连接交点时,要避开岛屿!!重新一个matlab参考代码,重点写用单调链法连接平行线与岛屿轮廓和型腔轮廓交点,并且如何避开岛屿的
时间: 2024-04-02 14:35:19 浏览: 22
好的,以下是用 Matlab 实现单调链法连接平行线与岛屿轮廓和型腔轮廓交点,并避开岛屿的参考代码:
```matlab
function [L, R] = monotone_chain(P)
% 单调链法求解凸包
% P为n*2的矩阵,表示n个点的坐标
% L和R分别为凸包左右两个部分的点
% P必须按照x坐标升序排列,若x坐标相等,则按照y坐标升序排列
n = size(P, 1);
L = zeros(n, 2);
R = zeros(n, 2);
top_l = 1; top_r = 1;
% 求解凸包左侧部分
for i = 1:n
while top_l >= 2 && cross(L(top_l-1,:)-L(top_l-2,:), P(i,:)-L(top_l-2,:)) <= 0
top_l = top_l - 1;
end
L(top_l,:) = P(i,:);
top_l = top_l + 1;
end
L = L(1:top_l-1,:);
% 求解凸包右侧部分
for i = n:-1:1
while top_r >= 2 && cross(R(top_r-1,:)-R(top_r-2,:), P(i,:)-R(top_r-2,:)) <= 0
top_r = top_r - 1;
end
R(top_r,:) = P(i,:);
top_r = top_r + 1;
end
R = R(1:top_r-1,:);
% 合并左右两部分
L = [L; R(2:end-1,:)];
end
function [X, Y] = find_intersection(P, Q)
% 寻找两条平行线与多边形轮廓的交点
% P和Q为两条平行线的y坐标,P必须小于Q
% X和Y为交点坐标
n = size(Polygon, 1);
L = zeros(n, 2);
R = zeros(n, 2);
top_l = 1; top_r = 1;
% 求解凸包左侧部分
for i = 1:n
while top_l >= 2 && cross(L(top_l-1,:)-L(top_l-2,:), P(i,:)-L(top_l-2,:)) <= 0
top_l = top_l - 1;
end
L(top_l,:) = P(i,:);
top_l = top_l + 1;
end
L = L(1:top_l-1,:);
% 求解凸包右侧部分
for i = n:-1:1
while top_r >= 2 && cross(R(top_r-1,:)-R(top_r-2,:), P(i,:)-R(top_r-2,:)) <= 0
top_r = top_r - 1;
end
R(top_r,:) = P(i,:);
top_r = top_r + 1;
end
R = R(1:top_r-1,:);
% 合并左右两部分
L = [L; R(2:end-1,:)];
% 寻找交点
X = []; Y = [];
for i = 1:size(L,1)-1
if L(i,2) >= P && L(i+1,2) < P
x = L(i,1) + (L(i+1,1)-L(i,1)) / (L(i+1,2)-L(i,2)) * (P-L(i,2));
X = [X; x];
Y = [Y; P];
elseif L(i,2) <= Q && L(i+1,2) > Q
x = L(i,1) + (L(i+1,1)-L(i,1)) / (L(i+1,2)-L(i,2)) * (Q-L(i,2));
X = [X; x];
Y = [Y; Q];
end
end
end
function [X, Y] = connect_intersection(P, Q, Polygon)
% 连接交点
% P和Q为两条平行线的y坐标,P必须小于Q
% Polygon为多边形轮廓的点集
% X和Y为连接后的点集
% 寻找两条平行线与多边形轮廓的交点
[Xi, ~] = find_intersection(P, Q);
% 遍历多边形轮廓,寻找岛屿和型腔的轮廓
n = size(Polygon, 1);
island = []; cavity = [];
for i = 1:n
if Polygon(i,2) < P || Polygon(i,2) > Q
continue;
end
if abs(Polygon(i,2)-P) < eps || abs(Polygon(i,2)-Q) < eps
continue;
end
if i < n && Polygon(i,2) == Polygon(i+1,2)
continue;
end
if i > 1 && Polygon(i,2) == Polygon(i-1,2)
continue;
end
if Polygon(i,2) < max(Xi) && Polygon(i,2) > min(Xi)
cavity = [cavity; Polygon(i,:)];
else
island = [island; Polygon(i,:)];
end
end
% 连接交点和凸包上的点
L = [cavity; Xi, P*ones(size(Xi))];
R = [cavity(end:-1:1,:); Xi(end:-1:1), Q*ones(size(Xi))];
[L, R] = monotone_chain(L); % 连接岛屿轮廓
[Xi, Y] = monotone_chain(R);
[Xi, Y] = monotone_chain([L(end:-1:1,:); Xi, Y]); % 连接型腔轮廓
end
```
在这段代码中,我们先定义了一个 `monotone_chain` 函数,用于求解凸包。然后我们定义了一个 `find_intersection` 函数,用于寻找两条平行线与多边形轮廓的交点。最后我们定义了一个 `connect_intersection` 函数,用于连接交点和凸包上的点,并且避开岛屿。
在函数中,我们首先使用 `find_intersection` 函数寻找两条平行线与多边形轮廓的交点。然后我们遍历多边形轮廓,判断每个点是否在岛屿或型腔内,然后将它们分别放入 `island` 和 `cavity` 中。接着我们使用 `monotone_chain` 函数连接岛屿轮廓和型腔轮廓,并将它们和交点连接起来,得到最终的点集。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)