当前位置: 动力学知识库 > 问答 > 编程问答 >

MATLAB how to Draw a circle around a chosen node in a graph?

问题描述:

l have generated a topology of a network with 200 nodes.than l want to draw a circle with black color around a choosen node(satisfying a certain condition IF (condition) ).

to simplify let the user introduce the the index of the node to circle with the black color.

here is my code of the generated topology.l need to add wich instructions to draw the circle around the choosen node ?

X=100;

Y=100;

N=200; %number of nodes

nodesX(1)=rand*X;

nodesY(1)=rand*Y;

for i=2:N

nodesX(i)=rand*X;

nodesY(i)=rand*Y;

d(i-1) =((nodesX(i)-nodesX(i-1)).^2+(nodesY(i)-nodesY(i-1)).^2).^0.5;

while (d(i-1)>200)

nodesX(i)=rand*X;

nodesY(i)=rand*Y;

d(i-1) =((nodesX(i)-nodesX(i-1)).^2+(nodesY(i)-nodesY(i-1)).^2).^0.5;

end

end

h_old=plot(nodesX,nodesY,'m*');

labels=[1:N]';

labels=num2str(labels);

text(nodesX,nodesY,labels);

xlabel('X (Meters)');

ylabel('Y (Meters)');

title(['Network Topology with',num2str(N),'nodes']);

hold on

for k=1:N;

for j=1:N;

if (k~=j)

d=((nodesX(k)-nodesX(j))^2+(nodesY(k)-nodesY(j))^2)^0.5;

end

if (k~=j);

if(d < 50);

line([nodesX(k),nodesX(j)],[nodesY(k),nodesY(j)]);

end

end

end;

end;

网友答案:

Split it into two tasks:

Task 1:

Define a simple function to draw a circle. A cheap and dirty way is to use polar expressions.

function [] = circle(center_x,center_y,r)

theta = 0:0.01:2*pi;
x  = center_x + r*cos(theta);
y = center_y + r*sin(theta);

plot(x,y,'k','LineWidth',2)

Task 2:

Pass this function the NodeX and NodeY values of the indexed node as the point at which the circle is centered. You can set the radius of the circle as per your choice. Using r=1 and picking an arbitrary index, I got:

Just one caveat: Make sure your axis are square. Otherwise, a circle might look like an ellipse. If you want to draw a circle around non-square axis, then you can modify the code to generate an ellipse instead.

分享给朋友:
您可能感兴趣的文章:
随机阅读: