Area of a implicit curve

조회 수: 7 (최근 30일)
Maxim Bogdan
Maxim Bogdan 2021년 4월 17일
댓글: Maxim Bogdan 2021년 4월 18일
Let's say that I have a multiply connected curve defined implicitly. For example:
f=@(x,y) sin(x).*sin(y)-0.5;
fimplicit(f,[-10,10,-10,10]);
How can we find the area of from the rectangle ?
So, if we can calculate the area of a filled figure than it's all done. There is a function for that kind of evaluations?
P.S. I wrote myself a program which detects where the changes from a connected component to another is made by fimplicit data points and I made a loop that sums all the areas of the polygons created using the [k,v]=boundary(Points,1) command.
I have the areas precision up to 4 decimal places, but I cannot solve the problems that appear on the corners of the rectangle , because boundary don't have them included in the points obtained from fimplicit. To be more precis, if I have the area that my code gives is not the area of a quarter of a circle, but the area of that sector minus the area of the triangle determined by it.
Also I have to mention that the integral method fails (it has too little precision). I mean that we can use the formula
integral2(@(x,y)heaviside(f(x,y)),-10,10,-10,10)

채택된 답변

Matt J
Matt J 2021년 4월 17일
편집: Matt J 2021년 4월 18일
Perhaps as follows?
plotRange=[-1,+1, -1,+1]*8;
plotCorners=[-1 1;1 1;1 -1; -1 -1]*8;
f=@(x,y) (sin(x).*sin(y)-0.5);
fp=fimplicit(f,2*plotRange,'MeshDensity',3000);
XY=[fp.XData;fp.YData].'; close
shpRegions=polyshape(XY);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
shpBounds=polyshape(plotCorners);
shpRegions=intersect(shpBounds,shpRegions);
Area=area(shpRegions)
Area = 48.6866
plot(shpRegions,'FaceColor','r')
  댓글 수: 2
Maxim Bogdan
Maxim Bogdan 2021년 4월 18일
It's a very nice solution! However the program does not make the difference between f or . It returns the same result and the same region. I wonder if it is posible to introduce somehow the condition ... If it is veryfied by a coloured point the the region is that. Else the region is the complementary one, and that should be filled.
Matt J
Matt J 2021년 4월 18일
Here is a modification to detect the complementary area
plotRange=[-1,+1, -1,+1]*8;
plotCorners=[-1 1;1 1;1 -1; -1 -1]*8;
f=@(x,y) (sin(x).*sin(y)-0.5);
fp=fimplicit(f,2*plotRange,'Mesh',4000);
XY=[fp.XData;fp.YData].'; close
shpRegions=polyshape(XY);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
shpBounds=polyshape(plotCorners);
shpRegions=intersect(shpBounds,shpRegions);
[x,y]=shpRegions.centroid(1:numboundaries(shpRegions));
s=f(x,y); %test the sign of the centroids
if any( s>0 ) %take complementary region
shpRegions=subtract(shpBounds,shpRegions);
end
Area=area(shpRegions)
Area = 207.3129
plot(shpRegions,'FaceColor','r')

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

추가 답변 (1개)

Matt J
Matt J 2021년 4월 18일
편집: Matt J 2021년 4월 18일
Here is a very similar strategy based on alphaShape instead of polyshape. It is slower, but IMO handles the bounding rectangle as well as the distinction between and more gracefully.
plotRange=[-1,+1, -1,+1]*8;
f=@(x,y) (sin(x).*sin(y)-0.5);
fp=fimplicit(f,plotRange,'MeshDensity',3000);
XY=unique([fp.XData;fp.YData].','rows','stable'); close
XY( any( isnan(XY),2) , : )=[];
[Xb,Yb]=ndgrid(linspace(plotRange(1), plotRange(end),500));
supp=f(Xb,Yb)<=0;
shp=alphaShape([XY;Xb(supp), Yb(supp)],0.1);
Area=area(shp)
Area = 207.3125
plot(shp,'FaceColor','r','EdgeColor','none')
  댓글 수: 2
John D'Errico
John D'Errico 2021년 4월 18일
Both good answers.
Maxim Bogdan
Maxim Bogdan 2021년 4월 18일
Thanks a lot Matt J! Now the problem of the area and perimeter of a implicitly defined shape is very clear.

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

카테고리

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