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]隨機(jī)角度
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;%長(zhǎng)半軸
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. 橢圓均勻采樣
% % % 作者:西邊升起太陽(yáng)
% % % 鏈接:https://www.zhihu.com/question/277712372/answer/550002849
% % % 來源:知乎
% % % 著作權(quán)歸作者所有擅腰。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
% % % 參考:Informed RRT*: Optimal Incremental Path Planning Focused through an Admissible Ellipsoidal Heuristic
%坐標(biāo)系正常
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
%內(nèi)部點(diǎn)均勻采樣:圓→橢圓
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
%單位球內(nèi)采樣
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);];