我需要在MATLAB中创建一种高速公路场景。我需要在高速公路上生成随机点(即车辆)。使用randn()命令,随机点会重叠在彼此之上。我希望生成的随机点之间保持最小距离。
有谁能帮助我生成这种场景吗?
我需要在MATLAB中创建一种高速公路场景。我需要在高速公路上生成随机点(即车辆)。使用randn()命令,随机点会重叠在彼此之上。我希望生成的随机点之间保持最小距离。
有谁能帮助我生成这种场景吗?
% 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')