Need something like polyarea(), but that handles crossings.

조회 수: 9 (최근 30일)
Mark Hayworth
Mark Hayworth 2016년 11월 16일
댓글: Walter Roberson 2017년 5월 2일
I want to find the enclosed area of a list of (x,y) coordinates that define a perimeter. Normally polyarea() will do this, however if the curve "crosses" itself, it considers the new area negative. For example the area of a bowtie is reported by polyarea() as 0. See demo code below:
x = [0, 1, 1, .5, 0, 0];
y = [0, 1, 0, .5, 1, 0];
plot(x, y, 'b-', 'LineWidth', 3);
grid on;
area = polyarea(x, y) % Gives 0
What I would like is to get the area that is completely enclosed by 1 or more lines - not part of, or "touching", the outside "background" at all. So in the above example the area would be 0.5, not zero.
In the example below:
x = rand(1, 30);
y = rand(1, 30);
x(end+1) = x(1); % Connect last to first.
y(end+1) = y(1); % Connect last to first.
plot(x, y, 'b-', 'LineWidth', 3);
grid on;
area = polyarea(x, y) % Gives wrong value
It would give the area enclosed by the outermost blue line, regardless if a "white" region is enclosed within another white region. That is, no areas are considered as negative.
Needless to say, getting the convex hull is not useful. I could of course use poly2mask() to convert it to a binary image, but that will have "holes" in it, though they could be filled in with imfill(). Then I could sum the binary image, or use bwarea(), or use regionprops(). However this imaging-based solution will have digitization error and doesn't work at all if the numbers are less than 1 (because the mask would be less than a pixel big). I was wondering if there was an accurate analytical solution.
Is there any alternative function to polyarea() that operates in this manner?
  댓글 수: 2
Sinashm
Sinashm 2017년 5월 2일
Did you find the answer of your questeion?
Walter Roberson
Walter Roberson 2017년 5월 2일
Mark did not return to indicate which definition of enclosure was desired. :(

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 11월 16일
편집: Walter Roberson 2017년 5월 2일
There is a difference between finding the area enclosed by the 'outermost' line, and finding the area enclosed by some lobe of the polygon. Finding the area enclosed by some lobe of the polygon involves holes that should not be counted.
Consider
|------------------|
| ________________ |
| | | |
| | A | |
| | -------------| |
|_| |______________|
The area of this can only include what is "inside" the thin shell, with the area marked in A definitely excluded. Now let us extend this slightly
|------------------|
| ________________ |
| | | |
| | A | |
| |--------------| |
|_|________________|
where now the edges touch all the way around. Under the "outermost line" definition, suddenly the area including A needs to be included. Under the "area enclosed by some lobe" definition, the section marked A is not to be included.
You need to be clear as to which definition you mean.

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by