Generate random moving nodes inside two intersecting circle

Hi everyone, I am trying to generate random points inside a circle given the radius and the center. If anyone has a sample code or can help me with this. thanks.

 채택된 답변

KSSV
KSSV 2016년 8월 30일
clc; clear all ;
N1 = 100 ; N2 = 50 ;
th = linspace(0,2*pi,N1)' ;
r1 = 1. ; r2 = 1. ; % radii of circles
c1 = [0 0] ; % center of first cirlce
c2 = [1.5 0] ; % center of second circle
a1 = repmat(c1,[N1 1])+[r1*cos(th) r1*sin(th)] ;
a2 = repmat(c2,[N1 1])+[r2*cos(th) r2*sin(th)] ;
%
plot(a1(:,1),a1(:,2),'r') ;
hold on
plot(a2(:,1),a2(:,2),'r') ;
axis equal
%%Get points of fitst circle lying in second circle
in12 = inpolygon(a1(:,1),a1(:,2),a2(:,1),a2(:,2)) ;
P1 = a1(in12,:) ;
plot(a1(in12,1),a1(in12,2),'.b') ;
%%Get points of second circle lying in first circle
in21 = inpolygon(a2(:,1),a2(:,2),a1(:,1),a1(:,2)) ;
P2 = a2(in21,:) ;
plot(a2(in21,1),a2(in21,2),'.b') ;
%%form the oval / intersection boundary
R = [P1 ;P2] ;
%%Form a suare grid around ovel region
x = linspace(c1(1),c2(1),N2) ;
y = linspace(-r1,r2,N2) ;
[X,Y] = meshgrid(x,y) ;
XX = X(:) ; YY = Y(:) ;
%%Get the random points inside the region
l = 1 ;
ax = min(R(:,1)) ; bx = max(R(:,1)) ;
ay = min(R(:,2)) ; by = max(R(:,2)) ;
while l ==1
x = (bx-ax).*rand(1,1) + ax;
y = (by-ay).*rand(1,1) + ay;
in = inpolygon(x,y,R(:,1),R(:,2)) ;
if in
plot(x,y,'.g') ;
drawnow
end
end

추가 답변 (1개)

KSSV
KSSV 2016년 8월 29일
편집: KSSV 2016년 8월 29일
clc; clear all ;
C = [0 0] ; % center of the circle
R = 1. ; % Radius of the circle
N = 100 ;
th =linspace(0,2*pi) ;
xc = C(1)+R*cos(th) ;
yc = C(2)+R*sin(th) ;
plot(xc,yc,'b') ;
hold on
axis equal
% Generate random numbers using polar coordinates
for i = 1:N
r = R * sqrt(rand(1,1)) ;
theta = 2 * pi * rand(1,1) ;
x = r * cos(theta) ;
y = r * sin(theta) ;
plot(x,y,'.r')
drawnow
end

댓글 수: 3

Thanks for the help, but the solution is only for a circle. The problem is not yet solved. The random nodes must be inside two intersecting circles of the same radius
Two circles have different center? You have two circles of same radii and they intersect..you will get a oval shape, you want random numbers inside this? Ask question clearly.
Yes, 1. two circles with different center, 2. having the same radius 3. both intersecting such that the distance between the two centers is greater than the radius. 4. nodes randomly generated inside the two circles and the intersecting area.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Polar Plots에 대해 자세히 알아보기

질문:

2016년 8월 29일

댓글:

2016년 8월 30일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by