%% theta = linspace(0,2*pi,7); D=2; %邊長 X=1; %中心橫座標 Y=2; %中心縱座標 plot(D*cos(theta)+X,D*sin(theta) + Y,'g-'); axis square i = 0; while i < 3 x = 2*D*rand(1,2)-1*D; if (abs(x(1)) + abs(x(2))/sqrt(3) ) <= D && abs(x(2)) <= D*sqrt(3)/2 i = i+1; hold on plot(x(1) + X, x(2) + Y,'r*'); end end hold off