smallest line that passes through a point while staying within poly... (2024)

15 views (last 30 days)

Show older comments

Talha Majeed on 7 Jun 2021

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries

Answered: Nipun on 17 May 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:

smallest line that passes through a point while staying within poly... (2)

6 Comments

Show 4 older commentsHide 4 older comments

SALAH ALRABEEI on 7 Jun 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569380

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569380

Do you mean you want to bdrow line passing throu the center!!

Talha Majeed on 7 Jun 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569390

yes by extend I mean draw.

Image Analyst on 7 Jun 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569435

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569435

Why does it need to go through the "center" point? What is the real world use case for this situation?

How accurate does it need to be? If you could burn that polygon into a digital image with poly2mask(), then you could simply use bwdist() in the Image Processing Toolbox to construct the Euclidean Distance Map (EDM or EDT) then look for the shortest radius which is the max value of the EDM. The more pixels in your image, the more accurate it would be. So is 1 decimal place of accuracy good enough or do you need 16 places (doubtful)? That's why we need to know the use case.

Talha Majeed on 7 Jun 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569455

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569455

Using polyxpoly for a previous similar script returned a length with 4 decimal places, which is enough. My use case is just general. I have that center point (centroid) for accuracy as if I was looking for the general shortest length it would be off in the edge. Also like I said I only want to test 1 line for each degree, totalling 180 lines.

Walter Roberson on 8 Jun 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569465

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569465

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 on 8 Jun 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569500

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#comment_1569500

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).

smallest line that passes through a point while staying within poly... (9)

Sign in to comment.

Sign in to answer this question.

Answers (1)

Nipun on 17 May 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#answer_1459536

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/850285-smallest-line-that-passes-through-a-point-while-staying-within-polygon-boundaries#answer_1459536

Open in MATLAB Online

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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABMathematicsComputational GeometryElementary Polygons

Find more on Elementary Polygons in Help Center and File Exchange

Tags

  • intersection
  • polygon

Products

  • MATLAB
  • Mapping Toolbox

Release

R2021a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


smallest line that passes through a point while staying within poly... (11)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

smallest line that passes through a point while staying within poly... (2024)
Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6354

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.