Generating Hexagonal Cell Site with random users in Matlab

This code will generate Hexagonal Cell Site in Matlab.

I’d used this Cell generation code in one of my Projects in which I’d to perform Monte Carlo simulation with users being generated in a hexagonal cell site

In this code, the cell radius was 1.4 units.

You can alter this. For eg. If your cell radius needs to be 1 unitthen replace

1.40 by 1
2.42 (1.4 x 1.732) by 1.732 (1 x 1.732)
1.21 (1.4 x 0.866) by 0.866 (1 x 0.866)
2.10 (1.4 x 1.500) by 1.500 (1 x 1.500)
Also adjust xa, xb, ya & yb accordingly.
% Yogesh Mhatre || IGOY.IN
clc;
clear all;
%close all;

%Generating a hexagon with centre (0,0)and radius 1.4 

t=linspace(0,2*pi,7);
ahx=0+1.4*cos(t);
bhx=0+1.4*sin(t);
plot(ahx,bhx);
plot(0,0,'ro');
grid on
hold on

% To generate 6 adjacent hexagon

a1=0+1.4*cos(t);                        % Upper hex
b1=2.42+1.4*sin(t);
plot(a1,b1);
plot(0,2.42,'ko');

a2=0+1.4*cos(t);                        % Lower hex
b2=-2.42+1.4*sin(t);
plot(a2,b2);
plot(0,-2.42,'ko');

a3=2.1+1.4*cos(t);                      % Right lower hex
b3=-1.21+1.4*sin(t);
plot(a3,b3);
plot(2.1,-1.21,'ko');

a4=2.1+1.4*cos(t);                      % Right upper hex
b4=1.21+1.4*sin(t);
plot(a4,b4);
plot(2.1,1.21,'ko');

a5=-2.1+1.4*cos(t);                     % Left upper hex
b5=1.21+1.4*sin(t);
plot(a5,b5);
plot(-2.1,1.21,'ko');

a6=-2.1+1.4*cos(t);                     % Left lower hex
b6=-1.21+1.4*sin(t);
plot(a6,b6);
plot(-2.1,-1.21,'ko'); 

    for i=1:250
        xa=-0.75+1.5*rand(1,1);
        ya=-1.2+2.4*rand(1,1);
        xra(i)=xa;
        yra(i)=ya;

        xb=-1+2*rand(1,1);
        yb=-0.65+1.3*rand(1,1);
        xrb(i)=xb;
        yrb(i)=yb; 

        figure(1)
        plot(xra(i),yra(i),'k.');
        hold on
        plot(xrb(i),yrb(i),'k.');

   end


Matlab

  • Tmr7813

    really useful,  thank you
    one comment: you dropped user starting from x = -0.75 which drop them in a square not a hexagon.I think it is better to drop them in a circle then exclude the out of cell using inpolygon function.