-
Notifications
You must be signed in to change notification settings - Fork 0
/
area_of_nsided_convex_polygon.m
63 lines (62 loc) · 2.63 KB
/
area_of_nsided_convex_polygon.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [total_area] = area_of_nsided_convex_polygon(vertices)
% input n number of vertices, and output the area, assuming
% it is convex (think polar coordinates)
% I am assuming the vertices are ordered
total_area = 0;
n = size(vertices,1);
for i= (mod(n,2) + n)/2 - 1
total_area = total_area + ((norm(cross(abs(vertices(2*(i-1)+1,:)-vertices(2*i,:)),abs(vertices(2*i,:)-vertices(2*i+1,:)))))/2);
end
if mod(n,2) == 1
total_area = total_area + ((norm(cross(abs(vertices(n,:)-vertices(1,:)),abs(vertices(1,:)-vertices(2,:)))))/2);
end
% n = size(vertices, 1);
% total_area = 0;
% centroid_x = 0;
% centroid_y = 0;
% centroid_z = 0;
% for i = 1:n
% centroid_x = centroid_x + vertices(i, 1);
% centroid_y = centroid_y + vertices(i, 2);
% centroid_z = centroid_z + vertices(i, 3);
% end
% centroid = [(centroid_x / n), (centroid_y / n), (centroid_z / n)];
% for i = 1:(n - 1)
% total_area = total_area + area_of_triangle(centroid, vertices(i,:), vertices(i + 1,:)); %add up areas of triangles formed by centroid and 2 consecutative vertices
% end
% total_area = total_area + area_of_triangle(centroid, vertices(1, :), vertices(n, :)); %account for first and last consecutative vertices
% n = size(vertices, 1);
% total_area = 0;
% centroid_x = 0;
% centroid_y = 0;
% centroid_z = 0;
% for i = 1:n
% centroid_x = centroid_x + vertices(n, 1);
% centroid_y = centroid_y + vertices(n, 2);
% centroid_z = centroid_z + vertices(n, 3);
% end
% centroid = [(centroid_x / n), (centroid_y / n), (centroid_z / n)];
% vertices_ordered = zeros(n, 3);
% curr_vertex = vertices(1, :);
% vertices_ordered(1,:) = curr_vertex(1,:);
% closest_vertex = zeros(1, 3);
% compared_vertex = zeros(1, 3);
% for i = 1:n
% dist = 9999999999999;
% curr_vertex = vertices_ordered (i,:);
% for j = 1:n
% if j ~= i
% compared_vertex = vertices(j,:);
% end
% curr_dist = sqrt(((curr_vertex(1) - compared_vertex(1))^2) + ((curr_vertex(2) - compared_vertex(2))^2) + ((curr_vertex(3) - compared_vertex(3))^2));
% if curr_dist < dist
% dist = curr_dist;
% closest_vertex = compared_vertex;
% end
% end
% vertices_ordered(i,:) = closest_vertex;
% end
% for i = 1:(n - 1)
% total_area = total_area + area_of_triangle(centroid, vertices_ordered(i,:), vertices_ordered(i + 1,:)); %add up areas of triangles formed by centroid and 2 consecutative vertices
% end
% total_area = total_area + area_of_triangle(centroid, vertices_ordered(1, :), vertices_ordered(n, :)); %account for first and last consecutative vertices