Wenn Sie das Zentrum und den Radius der inneren Kreise wissen Sie in der Lage sind, die X
und Y
Koordinaten der Kreise berechnen dann können Sie die inpolygon Funktion verwenden, um thest wenn ein Punkt innerhalb a Kreis (inpolygon
gibt 1
zurück, wenn ein Punkt innerhalb des Polygons liegt, andernfalls 0
). In diesem Fall besteht das Polygon aus den Punkten der Kreise.
Im folgenden Code bewegt sich ein Punkt über drei Kreise (zwei davon innerhalb des größeren).
Die inpolygon
Funktion wird verwendet, um zu testen, ob der Punkt (der Ball) innerhalb eines Kreises ist und seine Farbe entsprechend dem Kreis ändert, in dem er sich befindet.
% Define the radius and centre of three circles
r1=10;
r2=3
r3=4
c1=[0 0];
c2=[3 3];
c3=[-4 -4]
% Calculate the X and Y coord of the three circles
t=0:.01:2*pi;
x=cos(t)*r1
y=sin(t)*r1
x1=cos(t)*r2+c2(1)
y1=sin(t)*r2+c2(2)
x2=cos(t)*r3+c3(1)
y2=sin(t)*r3+c3(2)
% Plot the circles
plot(x,y,'r')
hold on
plot(x1,y1,'g')
plot(x2,y2,'b')
daspect([1 1 1])
% Define the ball trajectory
mx=-10:1:10;
my=-10:1:10;
for i=1:length(mx)
% Plot the ball: black if outside of all the circle
mh=plot(mx(i),my(i),'o','markerfacecolor','k','markeredgecolor','k')
% If the ballk is inside the first circle, make it red
if(inpolygon(mx(i),my(i),x,y))
mh.MarkerFaceColor='r';
mh.MarkerEdgeColor='r';
end
if(inpolygon(mx(i),my(i),x1,y1))
% If the ballk is inside the second circle, make it green
mh.MarkerFaceColor='g';
mh.MarkerEdgeColor='g';
end
if(inpolygon(mx(i),my(i),x2,y2))
% If the ballk is inside the third circle, make it blue
mh.MarkerFaceColor='b';
mh.MarkerEdgeColor='b';
end
pause(1)
end
Hoffnung, das hilft.
Qapla '
Was wissen Sie über die inneren Kreise? Das Zentrum? Der Radius? –
Es hilft anderen Benutzern und ist üblich, [Antworten (Link)] (http://stackoverflow.com/help/someone-answers) zu markieren, die Sie in der Frage erhalten haben. – zdim