Polyprop

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

%% Polygon Project

% Ronnie McKay (rmckay3)


% Online Section
% EF 230
% September 24, 2014
% Purpose: Take input and produce the centroid, number of sides, area, and perim
eter for a polygon
% Solution: Used a series of functions and for loops to find the required items
function [out] = polyprop(x)
out.numsides = numsides(x); % Gives
out.area = area(x); % Gives area of
out.perimeter = polyper(x); % Gives
out.centroid = polycen(x); % Center
end

number of sides to polygon


polygon
perimeter of poygon
of poygon in (x,y) coordinates

function[N] = numsides(x)
if size(x,2) ~= 2
[N] = 0;
error('Invalid Input (Matrix must have 2 columns)'); % Error message if inpu
t matrix does not have 2 columns
return;
end
if length(x) < 3
[N]=0;
error('Input Invalid (must have at least 3 points)'); % Error message if the i
nput matrix has less than 3 rows (3 coordinates)
else
[N] = length(x); % length(x) equals number of points. Also number of sides
end
end
function[A] = area(x)
a=0;
for i = 1:(length(x)-1);
a = a + abs((x(i,1).*x(i+1,2) - x(i+1,1).*x(i,2))); % Formula for area of a
polygon
end
[A] = .5.*a;
end
function [P] = polyper(x)
per = 0;
for i = 2:length(x)
per = per + (((x(i-1,1)-x(i,1)).^2)+((x(i-1,2)-x(i,2)).^2)).^(1./2);
end
[P] =per + (((x(length(x),1)-x(1,1)).^2)+(((x(length(x),2)-x(1,2)).^2))).^(1./2)
;
end
function[C] = polycen(x)
Cx = 0;
A= area(x);
b = length(x)-1;
for i = 1:b;
Cx=Cx+((x(i,1)+x(i+1,1)).*(((x(i,1)).*(x(i+1,2)))-((x(i+1,1)).*(x(i,2)))));
%summation for finding the horrizontial center of the polygon
end

centx = 1./(6.*A).*Cx; % Finds x coordinate of the centroid


Cy=0;
for n = 1:b
Cy = Cy+((x(n,2)+x(n+1,2)).*(((x(n,1)).*(x(n+1,2)))-((x(n+1,1)).*(x(n,2)))))
; %summation for finding the vertical center of the polygon
end
centy = 1./(6.*A).*Cy; % Finds y coordinate of the centroid
[C] = [centx,centy];
end

You might also like