问题
I am trying to create N
random pairs of points (N = 50) of a given distances, inside a 500
meters hexagon. The distance D created by using (dmax - dmin).*rand(N,1) + dmin
, with dmin = 10
and dmax = 100
in Matlab. I understant that the first I have to generate a set of points ([x1 y1])
that have at least distance D
from the main hexagon border, then generate the second set of points ([x2 y2])
that have exact distance D
from the first set. But sometime I got the problem with the second point outside of hexagon, because if the first position on the hexagol border and plus D
disance, then the second position is outside of hexagon (I mean that I want to generate random pair position inside of hexagol). Could anybody help me in generating this kind of scenario and fix the problem? Thanks.
For example as
R = 500; % hexagol radius
N = 50; % number pair positions
d_min = 10; % minimum distance
d_max = 100; % maximum distance
D = (d_max - d_min).*rand(N,1) + d_min; % randomly distance
X = [0,0]; % hexagol center
j=0;
while j < N
j=j+1;
theta(j)=2*pi*rand(1,1);
u= rand()+ rand();
if u < 1
r(j) = R * u;
else
r(j) = R * (2 - u);
end
% to create the first position
x1(j)=r(j)*cos(theta(j)) + X(1,1); % first x positions
y1(j)=r(j)*sin(theta(j)) + X(1,2); % first y positions
end
% to create the second position
x2(j) = x1(j) + D(j); % second x positions
y2(j) = y1(j) + D(j); % second y positions
回答1:
This is quite like your other question and its solution is almost the same, but it needs a little more math. Let’s focus on one pair of points. There still are two steps:
Step 1: Find a random point that is inside the hexagon, and has distance d
from its border.
Step 2: Find another point that has distance d
from first point.
Main problem is step 1. We can say that a points that has distance d
form a hexagon with radius r
, is actually inside a hexagon with radius r-d
. Then we just need to find a random point that lays on a hexagon!
Polar Formula of Hexagons:
I want to solve this problem in polar space, so I have to formulate hexagons in this space. Remember circle formula in polar space:
The formula of a hexagon in polar space is pretty much like its circumscribe circle, except that the radius of the hexagon differs at every t
(angle). Let’s call this changing radius r2
. So, if we find the function R2
that returns r2
for all t
s then we can write polar formula for hexagon:
This image demonstrates parameters of the problem:
The key parameter here is α
. Now we need a function Alpha
that returns α
for all t
s:
Now we have all points on border of the hexagon in polar space:
r = 500;
T = linspace(0, 2*pi, 181);
Alpha = @(t) pi/2-abs(rem(t, pi/3)-(pi/6));
R2 = @(t) r*cos(pi/6)./sin(Alpha(t));
X = R2(T).*cos(T);
Y = R2(T).*sin(T);
hold on
plot(X, Y, '.b');
plot((r).*cos(T), (r).*sin(T), '.r')
Polar Formula of a Regular Polygon:
Before I go on I’d like to generalize Alpha
and R2
functions to cover all regular polygons:
Alpha = @(t) pi/2-abs(rem(t, 2*pi/(n))-(pi/(n)));
R2 = @(t) r*cos(pi/n)./sin(Alpha(t));
Where n
is the number of edges of the polygon.
Answer:
Now we can generate pairs of points just like what we did for the circle problem:
r = 500; n = 6;
a = 10; b = 50;
N = 100;
D = (b - a).*rand(N,1) + a;
Alpha = @(t) pi/2-abs(rem(t, 2*pi/(n))-(pi/(n)));
R2 = @(t) r*cos(pi/n)./sin(Alpha(t));
T1 = rand(N, 1) * 2 * pi;
RT1 = rand(N, 1) .* (R2(T1)-D);
X1 = RT1.*cos(T1);
Y1 = RT1.*sin(T1);
T2 = rand(N, 1) * 2 * pi;
X2 = X1+D.*cos(T2);
Y2 = Y1+D.*sin(T2);
Rotating the polygon:
For rotating the polygon we just need to update the Alpha
function:
t0 = pi/8;
Alpha = @(t) pi/2-abs(rem(t+t0, 2*pi/(n))-(pi/(n)));
This is a test for n=7
, N=50000
and t0=pi/10
:
来源:https://stackoverflow.com/questions/38422964/how-to-generate-random-positions-with-distance-between-them-inside-the-hexagon