Home > MATLAB, Simulation, Wireless > Creating N Random points in a Hexagon

Creating N Random points in a Hexagon

In wireless communication there is a concept in which frequencies are allocated to an area in a regular pattern of areas, called ‘cells’. In modeling and simulation the hexagonal cell is usually used. The following routine describes how random nodes can be created in the cell area. The nodes created are random enough for all practical purposes.

In the code, I will create a hexagon centered at (0,0) with radius R. The snipplets can be used in mobile capacity predicts and general systems level simulation of cellular networks.

N = 40;     %Number of users
R = 10;     %Radius of Hexagon

Define the vertexes of the hexagon. They for angles 0, 60, 120, 180, 240 and 300 withe origin.
%V
ertexes
v_x = R * cos((0:6)*pi/3);
v_y = R * sin((0:6)*pi/3);

The method used here is to generate many points in a square and choose N points that fall within the hexagon

%Generate 3N random points with square that is 2R by 2R
c_x = R – rand(1, 3*N)*2*R;
c_y = R – rand(1, 3*N)*2*R;

There is a command in MATLAB inploygon.

The command finds points within a polygon region.

%get the points within the polygon
IN = inpolygon(c_x, c_y, v_x, v_y);

%drop nodes outside the hexagon
c_x = c_x(IN);
c_y = c_y(IN);

%choose only N points
idx = randperm(length(c_x));
c_x = c_x(idx(1:N));
c_y = c_y(idx(1:N));

plot(c_x, c_y, ‘r*’)
hold on
plot(v_x,v_y)
axis square
hold off