4 views (last 30 days)

Hi,

how can I calculate A complex polygon in Matlab?

image shows what I need to calculate in Matlab. do you guys have any code for that ? or are there any Matlab command which can calculate it by coordinates?

thanks in advance

Ameer Hamza
on 19 Oct 2020

The equation in your comment can be written in MATLAB like this

A = 1/2*sum(x(1:end-1).*y(2:end)-y(1:end-1).*x(2:end));

Ameer Hamza
on 19 Oct 2020

You can try something like this

X;

Y;

A = zeros(1, numel(X)-2);

for i = 1:numel(A)

x = X(1:i+2);

y = Y(1:i+2);

A(i) = 1/2*sum(x(1:end-1).*y(2:end)-y(1:end-1).*x(2:end));

end

Deeds
on 19 Oct 2020

You can use polyarea for accomplishing your task. The documentation is here:

Hope that helps!

Steven Lord
on 19 Oct 2020

If you want to do more than just compute its area I would use polyshape for that.

>> x = [7 7 5 3 -5 0 -8 2 -4];

>> y = [-7 3 -6 3 3 8 3 -3 -8];

>> P = polyshape(x, y);

>> plot(P)

There are a number of questions you can ask about a polyshape and a number of operations you can perform on one.

Opportunities for recent engineering grads.

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/618738-how-can-i-calculate-a-complex-polygon-with-coordinates-in-matlab#comment_1070293

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/618738-how-can-i-calculate-a-complex-polygon-with-coordinates-in-matlab#comment_1070293

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/618738-how-can-i-calculate-a-complex-polygon-with-coordinates-in-matlab#comment_1070328

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/618738-how-can-i-calculate-a-complex-polygon-with-coordinates-in-matlab#comment_1070328

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/618738-how-can-i-calculate-a-complex-polygon-with-coordinates-in-matlab#comment_1070363

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/618738-how-can-i-calculate-a-complex-polygon-with-coordinates-in-matlab#comment_1070363

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/618738-how-can-i-calculate-a-complex-polygon-with-coordinates-in-matlab#comment_1070373

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/618738-how-can-i-calculate-a-complex-polygon-with-coordinates-in-matlab#comment_1070373

Sign in to comment.