在多边形边界上彼此等距的点(坐标)-Matlab

时间:2020-07-15 21:00:29

标签: matlab

我正在考虑以下问题的解决方案:如何找到在多边形边界(多边形对象)上彼此等距的X点(坐标)。我什至不知道如何处理它,因此欢迎任何想法来实现它?

代码:

clc;
clear all;
close all;
 
numOfSegments = 10; % just an example
polygon = polyshape([0 5 15 15 20 18 10 20 20],[1 5 10 10 10 15 10 25 35]);
plot(polygon)
P = perimeter(polygon);
SegemntP = P/numOfSegments;

1 个答案:

答案 0 :(得分:1)

  1. 将LengthToGo设置为SegemntP
  2. 在多边形上选择起点
  3. 将LengthToGo移至下一个点
  4. 如果您没有达到这一点,那么您已经找到了自己的观点之一。使用它作为新起点,然后从0开始。
  5. 如果您已达到此点,则将LengthToGo减小起点和到达点的距离。将到达的点设置为新的起点。继续0

希望以下代码说明:

clc;
clear all;
close all;
 
numOfSegments = 10; % just an example
polygon = polyshape([0 5 15 15 20 18 10 20 20],[1 5 10 10 10 15 10 25 35]);
% polygon = polyshape([0 20 20 0 0],[0 0 20 20 0]);
plot(polygon)
hold on;

P = perimeter(polygon);
SegemntP = P/numOfSegments;

% this is first point on the polygon
lastPoint = polygon.Vertices(1,:);
% container for points
points = lastPoint;

polyIdx = 1;
lenToGo = SegemntP;
plygonPoints = [polygon.Vertices;polygon.Vertices(1,:)];% add first point so polygon is closed
while(size(points,1)<numOfSegments)
    lenOnPolyline = norm(plygonPoints(polyIdx+1,:)-lastPoint);
    if lenOnPolyline > lenToGo
        % move on this line
        dir = plygonPoints(polyIdx+1,:)-lastPoint;
        dir = dir ./ norm(dir);
        lastPoint = dir*lenToGo+lastPoint;
        points = [points;lastPoint];
        lenToGo = SegemntP;
    else
        % go to next line segment
        lenToGo = lenToGo-lenOnPolyline;
        polyIdx = polyIdx +1;
        lastPoint = plygonPoints(polyIdx,:);
    end
end

plot(points(:,1),points(:,2),'b*')
axis equal

结果:

enter image description here