for i = 0:3000
r = 80;
R = 120;
rr = rand * (R * R - r * r) + r * r;
rr = sqrt(rr);
theta = -pi/2 + pi*rand; %[-pi/2,pi/2]随机角度
if mod(round(rr),2) == 0
scatter(rr * cos(theta+pi/6),rr * sin(theta+pi/6) / 4,5,[0 0 1],'filled');
else
scatter(-rr * cos(theta+pi/6),-rr * sin(theta+pi/6) / 4,5,[0 0 1],'filled');
end
hold on;
end
2. 椭圆绘制
Num=100;
cicle=zeros(Num,2);
a=60;%长半轴
b=30;%短半轴
for i=1:Num
theta=-pi+i/Num*2*pi;
dOP=a*b/sqrt(a^2*sin(theta)^2+b^2*cos(theta)^2);
cicle(i,:)= dOP*[cos(theta+pi/6) sin(theta+pi/6)];
end
plot(cicle(:,1),cicle(:,2),'k','linewidth',1,'linestyle','--');
%fill(cicle(:,1)+80,cicle(:,2)-150,'g');%填充颜色
axis equal
3. 椭圆均匀采样
% % % 作者:西边升起太阳
% % % 链接:https://www.zhihu.com/question/277712372/answer/550002849
% % % 来源:知乎
% % % 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
% % % 参考:Informed RRT*: Optimal Incremental Path Planning Focused through an Admissible Ellipsoidal Heuristic
%坐标系正常
clc
clear
time=0;
start=[0,0];
goal=[20,50];
% start=[13 13]; % source position in Y, X format
% goal=[42 32]; % goal position in Y, X format
cmin=norm(goal-start);
cbest=120;
axis([-100,100,-100,100])
scatter(start(1),start(2),100,[0 1 0],'filled');
hold on;
scatter(goal(1),goal(2),100,[1 0 0],'filled');
hold on;
set(gcf,'position',[0.1,0.1,500,500])
time=0;
while time<500
%内部点均匀采样:圆→椭圆
x_center=[(start+goal)/2,0];
x_center=x_center';
a_1=[(goal(1)-start(1))/cmin;(goal(2)-start(2))/cmin;0];
id_t=[1,0,0];
M=a_1*id_t;
[U,S,Vh]=svd(M);
C=(U*diag([1,1,det(U)*det(Vh')]))*(Vh);
r=[cbest/2,sqrt(cbest.^2-cmin.^2)/2,sqrt(cbest.^2-cmin.^2)/2];
L=diag(r); a=rand(); b=rand();
if b<a
tmp=b;
b=a;
a=tmp;
end
x_ball=[b*cos(2*pi*a/b);b*sin(2*pi*a/b);0];
randpoint=C*L*x_ball+x_center;
scatter(randpoint(1,1),randpoint(2,1),'.','b')
hold on
grid on
%绘制外围椭圆边界
if time==200
for i=1:500
theta=-pi+i/500*2*pi;
y_ball=[cos(theta);sin(theta);0];
ellipse_bound(i,:,:)=(C*L*y_ball+x_center)';
plot(ellipse_bound(:,1),ellipse_bound(:,2),'k','linewidth',2,'linestyle','--')
hold on
end
end
time=time+1;
end
%单位球内采样
phi = rand().*2*pi;
costheta = -1+rand()*2;
u = rand();
theta = acos(costheta)
r = 1*u^(1/3);
x_ball=[r*(sin(theta).*cos(phi));r*(sin(theta).*sin(phi));r*cos(theta);];