如何在椭圆矩阵内部绘制随机点 [英] how to draw random points inside an ellipse matlab

查看:426
本文介绍了如何在椭圆矩阵内部绘制随机点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在其中填充N个随机点的椭圆 我会很高兴的任何帮助

I want to fill this ellipse with N random points inside it any help I'd be glad

clear ,close all;
xCenter = 15;
yCenter = 10;
xRadius = 3.5;
yRadius = 8;
theta = 0 : 0.01 : 2*pi;
N = 100; % N rand points

x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;    

plot(x, y, 'LineWidth', 1);

axis square;

grid on;

我尝试使用此代码在椭圆内生成具有特定参数的100个点,但是我没有实现我的目标,

I tried this code to generate 100 points inside the ellipse with specific parameters but I did not achieve my goal,

xCenter = 5;
yCenter = 3;
xRadius = 3.5;
yRadius = 8;
theta = 0 : 0.01 : 2*pi;
N = 100;

x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;

xq=(rand(N,1)*(2*yRadius) - yRadius);
yq=(rand(N,1)*(2*yRadius) - yRadius);    

in = inpolygon(xq,yq,x,y);
hold on
inX = xq(in);
inY = yq(in);
outX = xq(~in);
outY = yq(~in);
plot(inX, inY , 'ro');
plot(outX, outY, 'b*');
plot(x, y, 'LineWidth', 1);

axis square;

grid on;

推荐答案

Sardar的答案产生的点在椭圆内不均匀分布.这段代码产生了均匀的点分布:

Sardar's answer produces points not evenly distributed within the ellipse. This code produces an even distribution of points:

xCenter = 15;
yCenter = 10;
xRadius = 3.5;
yRadius = 8;
N = 100;

% Generate points in the ellipse
t = 2*pi * rand(N,1);
d = sqrt(rand(N,1));
x = xCenter + xRadius * d .* cos(t);
y = yCenter + yRadius * d .* sin(t);
plot(x,y,'o')

差异是与原点d的归一化(0至1)距离上的sqrt.通过计算平方根,可以增加靠近椭圆边缘的点的密度.这样可以补偿在中心附近过于密集的点.沿该归一化距离的点的均匀分布是导致中心附近的点的密度更高的原因.

The difference is the sqrt on the normalized (0 to 1) distance from the origin d. By computing this square root, you increase the density of points closer to the edge of the ellipse. This compensates for points otherwise being too dense close to the center. The uniform distribution of points along that normalized distance is what causes higher density of points near the center.

这篇关于如何在椭圆矩阵内部绘制随机点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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