-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateHexGrid.m
144 lines (119 loc) · 5.03 KB
/
generateHexGrid.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cellCoordinates = generateHexGrid(Radius, numOfLayers,plotData)
% Generate a hexagonal grid
%
%
%
% _______
% \ / \ /
% \ / Radius \ /
% \_______/ ----->\_______/
% / \ / \
% / \ / \
% _______/ \_______/ \_______
% / \ / \ / \
% / \ / 1st \ / \
% / \_______/ layer \_______/ . \
% \ / \ / \ | /
% \ / 1st \ / 1st \ | Re-use distance
% \_______/ layer \_______/ layer \___|___/
% / \ / \ / | \
% / \ / \ / | \ /
% / \_______/ Central \_______/ v \_______/
% \ / \ cell / \ / \
% \ / 1st \ / 1st \ / \
% \_______/ layer \_______/ layer \_______/
% / \ / \ / \
% / \ / 1st \ / \
% / \_______/ layer \_______/ \
% \ / \ / \ /
% \ / \ / \ /
% \_______/ \_______/ \_______/
% \ / \ /
% \ / \ /
% \_______/ \_______/
% \ / \
% \ / \
% \_______/ \
%
% Input arguments:
%
% Radius - cell radius
% For reuse 1, the reuse distance is sqrt(3)*Radius
%
% numOfLayers - number of cell layers.
% 0 --> single cell
% 1 --> one central cell and 6 neighbouring cells
% 2 --> one central cell and 18 neighbouring cells
% .
% .
% Output:
%
% cellCoordinates - (x,y) coordinate of center of cells
% first entry is the central cell
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(Radius < 0)
error('generateHexGrid: Cell radius must be positive real number');
end
if(numOfLayers < 0)
error('generateHexGrid: numOfLayers must be positive integer');
end
if(numOfLayers == 0)
cellCoordinates = [0,0]; % Single cell
return;
else
reuseDistance = sqrt(3)*Radius;
columnOfCells = zeros((2*numOfLayers+3),2);
columnOfCells(:,2) = [-(numOfLayers+1):(numOfLayers+1)]'*reuseDistance;
coordinates = columnOfCells; % Initialize
N = size(columnOfCells,1);
for k = 1:numOfLayers
if (mod(k,2)==1)
lift = sqrt(3)*Radius/2;
else
lift = 0;
end
left = columnOfCells +repmat([-1.5*k*Radius,lift],N,1);
right = columnOfCells +repmat([1.5*k*Radius,lift],N,1);
coordinates = [ coordinates; left;right];
end
cellDist = sqrt(sum(coordinates.^2, 2) ); % Distance from the orign
tmp = [ coordinates, ...
cellDist, ...
floor(eps+cellDist/(1.5*Radius)), ...
angle(coordinates(:,1) + 1j*coordinates(:,2) )];
tmp = sortrows(tmp, [4,5]);
% Distance of the farthest cell in the grid with "numOfLayers"
maxDist = Radius* sqrt( (1 + 1.5*numOfLayers)^2 + 3/4);
%---------Pick only cells that lie inside the layer
cellCoordinates = tmp(tmp(:,3)<=maxDist, 1:2);
end
% if (plotData)
%
% figure('color','w');
%
% plot(cellCoordinates(:,1),cellCoordinates(:,2),'kp',...
% 'MarkerSize',15);
% axis(1.2*[min(cellCoordinates(:,1)),max(cellCoordinates(:,1)), ...
% min(cellCoordinates(:,2)),max(cellCoordinates(:,2))]);
% hold on;
% plot(cellCoordinates(:,1),cellCoordinates(:,2),'rv:',...
% 'MarkerSize',6, 'MarkerFaceColor','r','LineWidth',3);
% grid on;
% axis equal;
% end
% Plot cells individually
%////////////////////////////////////////
if(plotData)
figure('color','w');
hold on;
theta = (0:6)'*2*pi/6;
cellBorder = Radius*[cos(theta), sin(theta)];
for k=1:size(cellCoordinates,1)
cellEdges = repmat(cellCoordinates(k,:),7,1) + cellBorder;
plot(cellEdges(:,1), cellEdges(:,2),'k','LineWidth',2);
end
xlabel('meters'); ylabel('meters');
grid on;
axis equal;
end