MATLAB Answers

Perimeter of a implicit curve

24 ビュー (過去 30 日間)
Maxim Bogdan
Maxim Bogdan 2021 年 4 月 18 日 12:26
コメント済み: Matt J 2021 年 4 月 18 日 13:43
I tried to make a program that returns the perimeter of a plane curve defined implicitly. The difficulty is that this type of curves can have more connected regions.
As an example we can take
f=@(x,y) sin(x).*sin(y)-0.5;
fp=fimplicit(f,[-8,8,-8,8]);
I observed that the points used by the fimplicit command are in such an order that makes the connected regions separately, and I detect the indices where we pass from one connected region to another. My idea was that if the segment connected two consecutive points from Points is greater than a small enough number (but not too small) tol than this is the step where a connected region is finished and we go to the next one. I'm not sure that this is indeed the way fimplicit command works. I see this picture that I make using the plot command:
plot(real(Points),imag(Points)) %see below what is the vector Points
Here is my code:
tol=0.01;
fp=fimplicit(f,[-8,8,-8,8],'meshdensity', 1000,'visible', 'off');
Points=fp.XData+1i.*fp.YData; %these are the x and y coordinates of the points used by fimplicit to represent the curve f(x,y)=0.
delete(fp);
Points=Points(isfinite(Pointsnew)==1);
d=diff(Points);
P=sum(hypot(real(d),imag(d)).*(hypot(real(d),imag(d))<tol)); %this is the perimeter
It returns very good values (3 or 4 decimals are exact). But it depends on how I choose the tolerance tol. If I take it too low the perimeter will be less than it is indeed. Moreover if I hace a part of a connected region on a corner then I want to take into account the part of the perimeter of the rectangle that bounds that region. With my program that is impossible.
Is there a way to write that program without the use of tol and to take into account the corners if needed?
  2 件のコメント
Maxim Bogdan
Maxim Bogdan 2021 年 4 月 18 日 13:10
I made a typo with Pointsnew;
If I delete NaN points with the command Points=Points(isfinite(Pointsnew)==1) and use plot afterwards I will get to the image that I posted above.
I want to calculate the perimeter indicated by black lines in the picture inserted by Matt J in his answer (so with the corners taken into account).

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2021 年 4 月 18 日 12:33
Perhaps as follows (see also this thread)
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);
Perimeter=perimeter(shpRegions)
Perimeter = 109.2290
plot(shpRegions,'FaceColor','r')
  7 件のコメント
Matt J
Matt J 2021 年 4 月 18 日 13:43
Well, you don't seem to have included samples of the boundary rectangle in the list of points in any way.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by