如何在六边形内生成它们之间有距离的随机位置? [英] How to generate random positions with distance between them inside the hexagon?
问题描述
我正在尝试在 500
米六边形内创建给定距离的 N
个随机点对(N = 50).通过使用(dmax-dmin).* rand(N,1)+ dmin
创建的距离D,其中 dmin = 10
和 dmax = 100
在Matlab中.我确信第一个点必须生成一组点([x1 y1])
,它们与六边形主边界的距离至少为 D
与第一组具有精确距离 D
的点([[x2 y2])
的数量.但是有时我会遇到六角形之外的第二个点的问题,因为如果六角形边界上的第一个位置加上 D
距离,那么第二个位置是六角形之外的(我是说我想在hexagol内生成随机对位置).有人可以帮助我产生这种情况并解决问题吗?谢谢.
例如
R = 500;己二醇半径%N = 50;数字对位置百分比d_min = 10;最小距离百分比d_max = 100;最大距离百分比D =(d_max-d_min).* rand(N,1)+ d_min;%随机距离X = [0,0];己醇中心%j = 0;而j<ñj = j + 1;theta(j)= 2 * pi * rand(1,1);u = rand()+ rand();如果u<1个r(j)= R * u;别的r(j)= R *(2-u);结尾%创建第一个位置x1(j)= r(j)* cos(theta(j))+ X(1,1);前x个排名的百分比y1(j)= r(j)* sin(theta(j))+ X(1,2);前首位%结尾%创建第二个位置x2(j)= x1(j)+ D(j);%秒x排名y2(j)= y1(j)+ D(j);%第二年y位置
这很像您的
极坐标空间中六边形的公式与外切圆非常相似,不同之处在于,六边形的半径在每个 t
(角度)上均不同.我们将此变化半径称为 r2
.因此,如果我们找到对所有 t
s返回 r2
的函数 R2
,那么我们可以为六边形写极坐标公式:
此图演示了问题的参数:
此处的关键参数是α
.现在我们需要一个函数 Alpha
,它为所有 t
s返回α
:
现在,所有点都在极坐标空间的六边形的边界上:
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);坚持,稍等情节(X,Y,'.b');图((r).* cos(T),(r).* sin(T),'.r')
正多边形的极性公式:
在我继续之前,我想概括一下 Alpha
和 R2
函数,以涵盖所有
答案:
现在,我们可以像处理圆弧问题一样生成成对的点:
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);
旋转多边形:
要旋转多边形,我们只需要更新 Alpha
函数:
t0 = pi/8;Alpha = @(t)pi/2-abs(rem(t + t0,2 * pi/(n))-(pi/(n)));
这是对 n = 7
, N = 50000
和 t0 = pi/10
的测试:
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
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
:
这篇关于如何在六边形内生成它们之间有距离的随机位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!