Spread a random user in hexagonal cell
Afficher commentaires plus anciens
Hello,i drawn one hexagonal cells and I want to spread a random users in it and specify the coordinates of each user in an array. thats my trial :
clear all;
close all;
clc;
t=linspace(0,2*pi,7);
x=0+1*cos(t);
y=0+1*sin(t);
plot(x,y);
hold on
grid on;
for i=1:100
xa=-0.8+1.6*rand(1,1);
ya=-0.8+1.6*rand(1,1);
xra(i)=xa;
yra(i)=ya;
figure(1)
plot(xra(i),yra(i),'k.');
hold on
end
as u see the whole cell cannot be filled by the users ,
Thanks for ur help :)
Réponse acceptée
Plus de réponses (1)
Ahmed Ibrahim
le 6 Mai 2016
Modifié(e) : Ahmed Ibrahim
le 6 Mai 2016
this is an update for your code:
-----------------------------------
function [xra,yra]=hex_rand(R,i)
% R is the hexagon side length
% i is the number of samples
t=linspace(0,2*pi,7);
x=0+R*cos(t);
y=0+R*sin(t);
plot(x,y);
hold on
grid on;
for m=1:i
xa=x(1)*2*(rand()-1/2);
ya=y(2)*2*(rand()-1/2);
if ((xa>(R/2)) && (ya>(-sqrt(3)*xa+(R*sqrt(3))))) %%the areas where the problem occur
ya=-sqrt(3)*xa+(R*sqrt(3));
elseif ((xa>(R/2)) && (ya<(sqrt(3)*xa-(R*sqrt(3)))))
ya=sqrt(3)*xa-(R*sqrt(3));
elseif ((xa<(-R/2)) && (ya>(sqrt(3)*xa+(R*sqrt(3)))))
ya=sqrt(3)*xa+(R*sqrt(3));
elseif ((xa<(-R/2)) && (ya<(-sqrt(3)*xa-(R*sqrt(3)))))
ya=-sqrt(3)*xa-(R*sqrt(3));
end
xra(m)=xa;
yra(m)=ya;
figure(1)
end
plot(xra,yra,'k.');
hold on
end
2 commentaires
AMAL ALGEDIR
le 10 Juil 2016
Modifié(e) : AMAL ALGEDIR
le 10 Juil 2016
how I can change this code if the center of the hexagon is not (0,0). how I can make work with more than one cell? I could draw more than one cell but I could not fix the position od users?
Walter Roberson
le 10 Juil 2016
It is often easiest to generate the numbers around an origin of 0 and then add the center as the last step.
Catégories
En savoir plus sur Surface and Mesh Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!