generate uniformly distributed points inside a hexagon
Ältere Kommentare anzeigen
am trying to generate uniformly distributed points inside a hexagon, but am stuck can any one help???
2 Kommentare
the cyclist
am 27 Mär. 2012
Bearbeitet: Jan
am 18 Jan. 2018
Jan
am 8 Okt. 2018
Uniformly distributed or a random positions over a uniformly distributed randomness?
Akzeptierte Antwort
Weitere Antworten (1)
Bruno Luong
am 8 Okt. 2018
Bearbeitet: Bruno Luong
am 18 Mär. 2023
Here is a generating method without rejection
R = 3;
centerx = 3;
centery = 7;
n = 10000;
% Generate uniform points in the simplex
% convex combination of 3points in R^3: (1,0,0) (0,1,0) (0,0,1)
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)]];
% use X as a barycentric of the triangle (0,1,z6) in the complex plane
% so point Z is uniform in this triangle
z6 = exp(2i*pi/6);
Z = [0, 1, z6]*X;
% multiply by random 6th-roots of 1 to map into unit hexagonal
Z = Z .* (z6.^floor(6*rand(1,n)));
% shift and scale
x = centerx+R*real(Z);
y = centery+R*imag(Z);
plot(x,y,'.')
axis equal
6 Kommentare
Mystery Mystery
am 10 Okt. 2018
Thank you so much sir, could you please add a few comments in the code so that i get a clear view of the method? thanks again.
Bruno Luong
am 10 Okt. 2018
Done
Amar Patra
am 23 Okt. 2018
Hi Bruno, I want to store the coordinates of these points in a two column csv file. The first column will have the x-coordinate while the second will have the y-coordinate. I tried with putting these instructions right before plot(x,y,'.'):
a = [x y] dlmwrite('file.csv',a,'-append','delimiter',',','newline', 'pc')
but the generated csv file has all the points stored in the fashion (x1, x2, x3, ..., xn, y1, y2, y3, ..., yn).
Could you please help!
Bruno Luong
am 23 Okt. 2018
Bearbeitet: Bruno Luong
am 23 Okt. 2018
a = [x(:),y(:)]
before write it.
Amar Patra
am 23 Okt. 2018
Thank you so much Bruno!
Omar Allahham
am 18 Mär. 2023
hi bruno
if I want to make the points move randomly and continuously in this hexagonal what should i do?
Kategorien
Mehr zu Random Number Generation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

