I was thinking about a solution for the following problem: How to find X points (coordinates) that have equal distances from each other on a polygon boundary (polyshape object.). I do not know even how to approach it so any ideas how to do it are welcome?
Code:
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;
SegemntP
Hope the following code explains:
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
Result:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.