MATLAB Answers

## re. finding perimeter of a 2D shape

Asked by Nithin

### Nithin (view profile)

on 14 Jun 2018
Latest activity Commented on by Nithin

### Nithin (view profile)

on 15 Jun 2018
Accepted Answer by Anton Semechko

### Anton Semechko (view profile)

Hi, I have coordinates of a 2D shape (x,y) and I am trying to determine the perimeter of the shape. I had initially tried the below code on circles etc, using the alpha shape function and perimeter with a given alpha radius of 2.5 and it worked fine. But my current shape I cant seem to use the alpha radius, as with alpha radius the perimeter is coming as zero. But without it works but I dont know if the result if correct as while plotting the figure seems a bit incomplete ie, the area within the contour is not completely filled. I am attaching the shape file, Thank you, Nithin
the code:
[Data.num] = xlsread('Profgraph.xls', 'sheet2');
nx50 = Data.num(1:end,1 ); ny50 = Data.num(1:end,2);
shp = alphaShape(nx50,ny50);
shp.Alpha=2.5;
plot(shp)
totalperim = perimeter(shp)

#### 0 Comments

Sign in to comment.

## 2 Answers

Answer by Anton Semechko

### Anton Semechko (view profile)

on 14 Jun 2018
Accepted Answer

% Contour coordinates
A=xlsread('Profgraph.xls','sheet2');
% Compute perimeter
N=size(A,1);
D=A(:,2:N)-A(:,1:(N-1));
D=sqrt(sum(D.^2,2)); % edge lenghts
P=sum(D); % perimeter
disp(P)
Answer comes out to 386.19

Nithin

### Nithin (view profile)

on 14 Jun 2018
Thank you for the alternative solution. Will look into this. :) Nithin There is some indexing error in the second line d = y-x. But hope to fix it.
Sorry, When I run the code with D=A(:,2:end)-A(:,1:(end-1));
I get around 585.71, Dont know what mistake I am doing.
Anton Semechko

### Anton Semechko (view profile)

on 14 Jun 2018
My bad:
% Compute perimeter
N=size(A,1);
D=A(2:N,:)-A(1:(N-1),:);
D=sqrt(sum(D.^2,2)); % edge lenghts
P=sum(D); % perimeter
disp(P)

Sign in to comment.

Answer by Steven Lord

### Steven Lord (view profile)

on 14 Jun 2018

Is an alpha value of 2.5 going to be appropriate for all data sets? Probably not.
You probably want to use the criticalAlpha and/or alphaSpectrum function to find an appropriate alpha value for your data set. Using the example from the alphaSpectrum page:
% Generate sample data and plot it
th = (pi/12:pi/12:2*pi)';
x1 = [reshape(cos(th)*(1:5), numel(cos(th)*(1:5)),1); 0];
y1 = [reshape(sin(th)*(1:5), numel(sin(th)*(1:5)),1); 0];
x = [x1; x1+15;];
y = [y1; y1];
plot(x,y,'.')
axis equal
hold on
% Build an alphaShape using the default alpha value
shp = alphaShape(x,y);
% Determine alpha values that produce unique shapes
alphaspec = alphaSpectrum(shp);
% Iterate through the alpha values. Show each one in turn
% with a 0.1 second pause between each shape
for k = 1:length(alphaspec)
shp.Alpha = alphaspec(k);
h = plot(shp);
title(sprintf('Alpha value of %.16f', alphaspec(k)));
pause(0.1)
delete(h)
end
The alpha values returned by criticalAlpha are in the alphaspec variable.

Nithin

### Nithin (view profile)

on 15 Jun 2018
Thank you both very much :)

Sign in to comment.