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 :)

 채택된 답변

Walter Roberson
Walter Roberson 2012년 1월 27일

1 개 추천

When you are choosing xa and ya, you are choosing the coordinates from a square from [-0.8, 0.8] in both x and y. Your hexagon extends a bit past +/- 0.8 in y, and extends to +/- 1 in x, and because of the angled lines there are parts of the square (whose coordinates you are choosing from) that are not within the hexagon.
The square: because -0.8+1.6*rand(1,1) ranges from -0.8 + 1.6*0 to -0.8 + 1.6 * 1 which is -0.8 to +0.8, and you use that calculation for both x and y.
You need to find a way of choosing exactly withinin the hexagon, or you need to find a way of choosing over a larger area and discarding the points that are not in the hexagon.

댓글 수: 3

Ahmed Hassaan
Ahmed Hassaan 2012년 1월 27일
yes That's what I am working on it ,converting that square to hexagon.or discarding the users that are not in hexagon.
About the array i put z=[xra(i) yra(i)] in the for loop and each z appears in the command window.
But I want like this ex: z1=[xra(1) yra(1)] ,z2=[xra(2) yra(2)],....
thx I appreciate ur help :)
Walter Roberson
Walter Roberson 2012년 1월 28일
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Walter Roberson
Walter Roberson 2012년 1월 28일
Hint: http://www.mathworks.com/help/techdoc/ref/inpolygon.html
There is even a relevant example.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ahmed Ibrahim
Ahmed Ibrahim 2016년 5월 6일
편집: Ahmed Ibrahim 2016년 5월 6일

1 개 추천

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

AMAL ALGEDIR
AMAL ALGEDIR 2016년 7월 10일
편집: AMAL ALGEDIR 2016년 7월 10일
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
Walter Roberson 2016년 7월 10일
It is often easiest to generate the numbers around an origin of 0 and then add the center as the last step.

댓글을 달려면 로그인하십시오.

카테고리

질문:

2012년 1월 27일

댓글:

2016년 7월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by