Matlab,生成并绘制分布在三角形内的点云 [英] Matlab, generate and plot a point cloud distributed within a triangle

查看:867
本文介绍了Matlab,生成并绘制分布在三角形内的点云的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成均匀分布在三角形内的2D点云.到目前为止,我已经实现了以下目标:

I'm trying to generate a cloud of 2D points (uniformly) distributed within a triangle. So far, I've achieved the following:

我使用的代码是这样:

N = 1000;
X = -10:0.1:10;
for i=1:N
    j = ceil(rand() * length(X));
    x_i = X(j);
    y_i = (10 - abs(x_i)) * rand;

    E(:, i) = [x_i y_i];
end

但是,从左和右角清楚可见,这些点并不是均匀分布的.如何改善结果?我也一直在尝试搜索不同的形状,但是没有运气.

However, the points are not uniformly distributed, as clearly seen in the left and right corners. How can I improve that result? I've been trying to search for the different shapes too, with no luck.

推荐答案

您首先应该问自己,如何使三角形内的点均匀分布.

You should first ask yourself what would make the points within a triangle distributed uniformly.

长话短说,给定三角形的所有三个顶点,您需要像这样转换两个均匀分布的随机值:

To make a long story short, given all three vertices of the triangle, you need to transform two uniformly distributed random values like so:

N = 1000;                    % # Number of points
V = [-10, 0; 0, 10; 10, 0];  % # Triangle vertices, pairs of (x, y)
t = sqrt(rand(N, 1));
s = rand(N, 1);
P = (1 - t) * V(1, :) + bsxfun(@times, ((1 - s) * V(2, :) + s * V(3, :)), t);

这将产生一组点,这些点在指定三角形内均匀分布:

This will produce a set of points which are uniformly distributed inside the specified triangle:

scatter(P(:, 1), P(:, 2), '.')

请注意,此解决方案不涉及对随机数的重复条件操作,因此它不可能陷入无限循环.

Note that this solution does not involve repeated conditional manipulation of random numbers, so it cannot potentially fall into an endless loop.

要进一步阅读,请查看本文.

For further reading, have a look at this article.

这篇关于Matlab,生成并绘制分布在三角形内的点云的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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