-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateRegularPolygon.m
More file actions
75 lines (63 loc) · 2.82 KB
/
GenerateRegularPolygon.m
File metadata and controls
75 lines (63 loc) · 2.82 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Creates a 2D polygon by sampling points on a Ellipse.
%
% Parameters:
% Inputs: aveRadius - The average radius of the ellipse inside which the
% polygon will be plotted
% This roughly controls how large the polygon will be
% numVerts - Number of vertices of the polygon
% AspectRatio - VerticalAxesLength/HorizontalAxesLength (b/a) of
% the Ellipse
%
% Output: [x_vertices, y_vertices] = Returns a list of vertices as (x,y)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [x_vertices, y_vertices] = GenerateRegularPolygon ( aveRadius, numVerts, AspectRatio )
% generate n angle steps
% Make number of angles equal to number of vertices
angleSteps(1:numVerts) = (2*pi / numVerts);
% now generate the points
angle = unifrnd(0, 1);
angle(2:numVerts+1) = angleSteps(1:numVerts);
angle = cumsum(angle); % Incrementing angle per vertex
angle(end) = []; % We wouldnt need the last angle since it is a closed polygon with a flat base
%% Using polar cordinate equation for an ellipse
% The AspectRatio variables gives the ratio of a/b where a and b are vertical axis and horizontal radius lengths.
if AspectRatio > 1
x_vertices = round((1/AspectRatio)*(aveRadius*cos(angle)));
y_vertices = round(aveRadius*sin(angle));
else
x_vertices = round((aveRadius*cos(angle)));
y_vertices = round(AspectRatio*aveRadius*sin(angle));
end
% Close the polygon by making its end vertex = start vertex
x_vertices(end+1) = x_vertices(1);
y_vertices(end+1) = y_vertices(1);
% Make the base of the polygon flat so that it can stand upright on a surface
index = find(y_vertices==min(y_vertices), 1 );
if index == numVerts
y_vertices(index-1) = y_vertices(index);
else
y_vertices(index+1) = y_vertices(index);
end
%% Check for center of gravity
% Offset the polygon to have a vertex at (0,0)
if min(y_vertices) < 0
y_vertices = y_vertices + abs(min(y_vertices));
else
y_vertices = y_vertices - abs(min(y_vertices));
end
if min(x_vertices) < 0
x_vertices = x_vertices + abs(min(x_vertices));
else
x_vertices = x_vertices - abs(min(x_vertices));
end
% Find the centroid
[x_centroid, ~] = polygoncentroid(x_vertices,y_vertices);
CenterOfMass_balanced = nnz(x_centroid >= min(x_vertices(y_vertices == min(y_vertices))) & x_centroid <= max(x_vertices(y_vertices == min(y_vertices))));
% Re-run of the structure's center of gravity does not fall within the base
if CenterOfMass_balanced ~= 1
clearvars -except aveRadius numVerts AspectRatio;
% disp("Not balanced, rerunning \n");
[x_vertices, y_vertices] = GenerateRegularPolygon ( aveRadius, numVerts, AspectRatio );
end
end