生成N个随机点,它们之间具有一定的预定义距离 [英] Generating N random points with certain predefined distance between them

查看:229
本文介绍了生成N个随机点,它们之间具有一定的预定义距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在MATLAB中创建高速公路场景.我必须在高速公路上生成随机点(即车辆).通过使用randn()命令,随机点彼此重叠.我想生成随机点,以便保持随机点之间的最小距离.

I have to create highway scenario in MATLAB. I have to generate random points (i.e. vehicles) on highway. By using randn() command, random points are overlapping on each other. I want to generate random points such that a minimum distance between random points is maintained.

有人能帮助我产生这种情况吗?

Could anybody help me in generating this kind of scenario..

推荐答案

这不是一个很好的解决方案,但是它满足您的最小距离约束.

This is not an elegant solution, but it satisfies your minimum distance constraint.

% Highway dimensions
lx = 1000;
ly = 1000;

% Minimum distance
d = 100;

% Number of points to generate
n = 50;

points = [rand(1, 2) .* [lx ly]];
d2 = d ^ 2;

% Keep adding points until we have n points.
while (size(points, 1) < n)

    % Randomly generate a new point
    point = rand(1, 2) .* [lx ly];

    % Calculate squared distances to all other points
    dist2 = sum((points - repmat(point, size(points, 1), 1)) .^ 2, 2);

    % Only add this point if it is far enough away from all others.
    if (all(dist2 > d2))
        points = [points; point];
    end
end

plot(points(:,1), points(:,2), 'o')

这篇关于生成N个随机点,它们之间具有一定的预定义距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆