I am trying to generate uniformly distributed points inside a hexagon of specific centre (other than the origin). Any help is most welcome, thank you.

4 views (last 30 days)
I have seen codes like this with origin as centre only, but i need to apply it for hexagonal cells of different centres.
  2 Comments
Mystery Mystery
Mystery Mystery on 7 Oct 2018
Here's the code. I don't understand how to add the centre, could you help please?
N = 400; %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.
%Vertexes
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

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 7 Oct 2018
Define the centre points, then add them as appropriate to ‘v_x’ and ‘v_y’ and ‘c_x’ and ‘c_y’:
xcntr = 7; % X-Coordinate Of Centre Of Hexagon & Points
ycntr = 5; % Y-Coordinate Of Centre Of Hexagon & Points
...
v_x = R * cos((0:6)*pi/3) + xcntr;
v_y = R * sin((0:6)*pi/3) + ycntr;
...
c_x = R-rand(1, 3*N)*2*R + xcntr;
c_y = R-rand(1, 3*N)*2*R + ycntr;
In context:
N = 400; %Number of users
R = 10; %Radius of Hexagon
xcntr = 7; % X-Coordinate Of Centre Of Hexagon & Points
ycntr = 5; % Y-Coordinate Of Centre Of Hexagon & Points
%Define the vertexes of the hexagon. They for angles 0, 60, 120, 180, 240 and 300 withe origin.
%Vertexes
v_x = R * cos((0:6)*pi/3) + xcntr;
v_y = R * sin((0:6)*pi/3) + ycntr;
%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 + xcntr;
c_y = R-rand(1, 3*N)*2*R + ycntr;
%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);
hold off
axis equal
axis([-1 1 -1 1]*20)

More Answers (2)

jonas
jonas on 7 Oct 2018
Edited: jonas on 7 Oct 2018
Simply move the center of the polygon and the random points a distance [xc;yc] from the origin. Here is your adjusted code:
N = 400; %Number of users
R = 10; %Radius of Hexagon
% Centers
xc=5;
yc=10;
%Define the vertexes of the hexagon. They for angles 0, 60, 120, 180, 240 and 300 withe origin.
%Vertexes
v_x = (R * cos((0:6)*pi/3))+xc;
v_y = (R * sin((0:6)*pi/3))+yc;
%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)+xc;
c_y = (R-rand(1, 3*N)*2*R)+yc;
%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

Bruno Luong
Bruno Luong on 7 Oct 2018
Edited: Bruno Luong on 7 Oct 2018
Here is a method without rejection:
R = 3;
centerx = 3;
centery = 7;
n = 10000;
m = 3;
X = rand(m-1,n) .^ (1./(m-1:-1:1)'); % use bsxfun(@power,...) for old release
X = cumprod([ones(1,n);X]).*[ones(m,n)-[X;zeros(1,n)]];
z6 = exp(2i*pi/6);
Z = [0, 1, z6]*X;
Z = Z .* (z6.^floor(6*rand(1,n)));
x = centerx+R*real(Z);
y = centery+R*imag(Z);
plot(x,y,'.')
axis equal
  4 Comments

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!