Effacer les filtres
Effacer les filtres

smallest line that passes through a point while staying within polygon boundaries

4 vues (au cours des 30 derniers jours)
Talha Majeed
Talha Majeed le 7 Juin 2021
Réponse apportée : Nipun le 17 Mai 2024
I have a polygon that is generated through x and y coordinates, I also have the center point of the polygon. What I want to do is extend a line at every degree (0-180) that goes through the center point of the polygon but stops at both sides of the polygon boundary. What I want to do then is to extract the length of the smallest of these lines, and the line that is perpendicular to it. (I have been using polyxpoly function from the mapping toolbox, I'm just confused how to apply it to this).
Here is what I have:
  6 commentaires
Walter Roberson
Walter Roberson le 8 Juin 2021
Is that what you want, or would your needs be met by a feret diameter? https://www.mathworks.com/help/images/ref/bwferet.html
Talha Majeed
Talha Majeed le 8 Juin 2021
I dont think that would work as I have many of these polygons and I dont want to add the extra step of converting it to an image that would work for that function. I also later need to find the perpandicular line to the minimum radius and display both on a plot of the polygon above. This is why I want to use polyxpoly which I used for finding the MAX vector and its perpandicular (shown below).

Connectez-vous pour commenter.

Réponses (1)

Nipun
Nipun le 17 Mai 2024
Hi Talha,
I understand that you are trying to generate lines at every degree through the center point of a polygon, find where these lines intersect the polygon boundary, and then calculate the lengths of these lines to identify the shortest one and the line perpendicular to it. Here is a concise MATLAB code snippet to achieve this:
% Assuming polygonX, polygonY are the coordinates of the polygon vertices,
% and centerX, centerY are the coordinates of the center point.
shortestLength = inf;
shortestAngle = 0;
% Loop through each degree from 0 to 180
for angle = 0:180
% Generate points far enough to ensure intersection with the polygon
farPointX = centerX + cosd(angle) * 10000; % Adjust 10000 based on your polygon scale
farPointY = centerY + sind(angle) * 10000;
% Find intersection points with the polygon
[xi, yi] = polyxpoly([centerX, farPointX], [centerY, farPointY], polygonX, polygonY);
% Calculate the distance if there are intersections
if numel(xi) > 1
distances = hypot(diff(xi), diff(yi));
minDistance = min(distances); % In case of multiple segments, take the shortest
if minDistance < shortestLength
shortestLength = minDistance;
shortestAngle = angle;
end
end
end
% Calculate perpendicular line
perpendicularAngle = mod(shortestAngle + 90, 180);
farPointX = centerX + cosd(perpendicularAngle) * 10000;
farPointY = centerY + sind(perpendicularAngle) * 10000;
[xi_perp, yi_perp] = polyxpoly([centerX, farPointX], [centerY, farPointY], polygonX, polygonY);
% Assuming perpendicular line also intersects the polygon at exactly two points
if numel(xi_perp) > 1
perpendicularLength = hypot(xi_perp(2) - xi_perp(1), yi_perp(2) - yi_perp(1));
else
perpendicularLength = NaN; % Handle case where there isn't exactly one intersection pair
end
fprintf('Shortest Line Length: %f\n', shortestLength);
fprintf('Perpendicular Line Length: %f\n', perpendicularLength);
This code calculates the shortest line and its perpendicular counterpart based on their intersection points with the polygon. It assumes that the lines will intersect the polygon at exactly two points, which might need adjustment for complex polygons or specific scenarios.
Hope this helps.
Regards,
Nipun

Catégories

En savoir plus sur Elementary Polygons dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by