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

3 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 2 May 2017
Mark did not return to indicate which definition of enclosure was desired. :(

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 16 Nov 2016
Edited: Walter Roberson on 2 May 2017
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.

Community Treasure Hunt

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

Start Hunting!