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

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.

Related

Matlab - isolating x/y values of a circle within a circle

I am looking for a way to isolate the x and/or y coord of a circle within a circle, as shown in the image.
I need to isolate this so i can set a condition that when a ball enters a circle, i can change some qualities of the ball, i have been able to do this for the outer circle, which is centred at [0 0] using the code below,
while sqrt(XY(1)^2 + XY(2)^2) < 5
but cannot figure out how to do it for the interior circles.
Thanks
If you know the center and the radius of the inner circles you are able to calculate the X and Y coordinates of the circles then, you can use the inpolygon function to thest if a point is inside a circle (inpolygon returns 1 if a point is inside the polygon, 0 otherwise). In this case the polygon is constitued by the points of the circles.
In the following code a point moves across three circles (two of them placed inside the bigger one).
The inpolygon function is used to test if the point (the ball) is inside a circle and in change its colour according to the circle it is inside to.
% 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
Hope this helps.
Qapla'

I need to create a 3D sphere from a bunch of 2D slices

I currently have to create a sphere from a stack of 2D slices (2D matrices in MATLAB, which represent 2D gray scale images). I have created a bunch of slices using the code below. To create the sphere I have repeatedly created slices of circles of increasing size till 100, and then of decreasing sizes. All of these slices are added to a 3D matrix.
Circle = ones(200,400,400);
for i = 1:100
[rr cc] = meshgrid(1:400);
C = sqrt((rr-200).^2+(cc-200).^2)<=i;
for j = 1:400
for k = 1:400
Circle(i,j,k) = C(j,k);
end
end
end
index = 100;
for i = 1:100
[rr cc] = meshgrid(1:400);
C = sqrt((rr-200).^2+(cc-200).^2)<=index;
for j = 1:400
for k = 1:400
Circle(i+100,j,k) = C(j,k);
end
end
index = index - 1;
end
viewer3d(Circle);
viewer3d is 3rd part library that helps you view your 3D image stacks of slices as 3d objects. Once I visualized this 'supposed' sphere, I realized that it is a diamond shape top and not a sphere.
Therefore I do not understand how to vary the size of circles till the center point of the sphere in the y plane and then decrease it with the same algorithm.
Thank you for your answers and please do not hesitate to ask me to clarify anything within this question.
Alternatively, create a sphere directly, without using loops:
Circle = zeros(200,400,400);
[x,y,z]=meshgrid(1:size(Circle,1),1:size(Circle,2),1:size(Circle,3));
radius=??; %// e.g. radius=100;
%//this sphere is centered in the middle of the image
Circle(sqrt((x-size(Circle,1)/2).^2+(y-size(Circle,2)/2).^2..
+(z-size(Circle,2)/2).^2)<radius)=1;
Yes, the radius along the Z axis is not linear but varies with cos/sin function. Using this representation:
your radius is "Radius = r sin(Theta)", with "Theta = arccos(r / z)". So "r" is the radius of your sphere, and "z" the level/slice you want to draw in. Don't forget to that "z" goes from -"r" to "r". I've tested the formulae and it works fine for an image stack (slices).

How to draw multiple shapes using Matlab vision toolbox and step function?

I'm trying to insert 2 shapes (circle and rectangle) to an image using these functions. But I'm unable to do that.
Here's my code
J = step(shapeInserter, I, bbox); %bbox is rectangle which is already defined
J = step(shapeInserter, I, circle); %circle is circle which is already defined
imwrite(J,'image.jpg','jpg'); % it draws only the circle
I have a long way which is to save the rectange image then load again to draw the circle and resave. Which i wish to avoid as it's really time consuming.
I'm trying to do something like this (similar to the plotting graph function)
hold on
%draw circle
%draw rectangle
hold off
imwrite(J,'image.jpg','jpg');
Please advise, thanks
The vision.ShapeInserter object has a property Shape, which can either be set to
'Rectangles'
'Circles'
'Lines'
'Polygons'
By default, it is set to 'Rectangles'. To use the same ShapeInserter object to place a circle, you will have to release it first by calling release(shapeInserter); and modifying the Shape property by set(shapeInserter,'Shape','Circles'). Then you can call the step method again to insert the circle.
Here is a small example:
I = imread('cameraman.tif');
rectangle = int32([10,10,50,60]);
circle = int32([200,200,40]);
shapeInserter = vision.ShapeInserter('Fill',true);
J = step(shapeInserter,I,rectangle);
release(shapeInserter);
set(shapeInserter,'Shape','Circles');
K = step(shapeInserter,J,circle);
imshow(K);
I'm a total noob to Matlab Image stuff but here is some very strait forward way of doing it :
frame=imread( '/home/omido/DeepLearning/matlab_segmentation/track/track/Image2.jpg' );
result = insertShape(frame, 'FilledRectangle', [100,100,100,100], 'Color', 'green');
result = insertShape(result, 'FilledRectangle', [200,200,200,200] , 'Color', 'yellow');
imshow(result);
and this is the result:
and as in the previous answer there are multiple shapes, you can find them here.

Drawing multiple polygons in Matlab with no borders

I'm creating a figure with many polygons which can overlap in Matlab. I do not want borders on any of the shapes, only a translucent fill. I was able to get the first shape to obey this request using:
fill(xv,yv,'blue','FaceAlpha',0.1,'EdgeColor','None','LineStyle','none');hold on;
However, each subsequent shape which is drawn in the loop ignores this style, instead cycling through colored borders. I was able to use the set command to override the color cycle, but this still draws a border...I do not want any border. The border cannot be drawn at all because we are using the overlapping properties of the shapes, and the presence of any borders would perturb the nature of our simulation.
Here is the complete code:
for count = 1:92
x=randn*clustering;
y=randn*clustering;
angle=randn*360;
rectangle(width,height,x,y,angle);
end
function rectangle(w,h,x,y,angle)
%Define the rectangle
xv=[x x+w x+w x x];
yv=[y y y+h y+h y];
%Define the rotation transformation matrix
transform=[cos(angle) -sin(angle);sin(angle) cos(angle)];
%Define the translation to origin transform
xcenter=x+.5*w;
ycenter=y+.5*h;
%Perform translation to origin
tx=xv-xcenter;
ty=yv-ycenter;
%Perform rotation
rotated=transform*[tx;ty];
%Perform translation back to original location
xv=rotated(1,:)+xcenter;
yv=rotated(2,:)+ycenter;
%Plot result
figure(1);
plot(xv,yv);
fill(xv,yv,'blue','FaceAlpha',0.1);hold on;
axis square;
axis([-30 30 -30 30]);

How can I draw this network of tubes in Matlab?

I have a distribution of tube radius r, and I would like to plot tubes for all the sampled r in single figure as shown in the figure above. The tubes have following characteristics:
The length of all tubes is constant, but radius is varying.
The narrowest tube will be completely filled
with light gray color.
The length of the light gray color from bottom in all other tubes is
inversely proportional to the radius of the tube i.e.
length of light grey color from bottom = constant/r
The remaining length of the tube will be filled with dark gray color.
Magnitudes of r and total length of each tube is of the order of 1e-005m and 1e-002 m, respectively, so they need to be standardized compared to the X and Y axes units.
The white interspaces are just spaces and not tubes.
UPDATE (Based on the answer by Boris)
This is the code from Boris in which I made certain changes based on the characteristics of the tubes that I have described above. I am having scaling issues as I am not able to visualize my network of tubes as clearly as can be seen in the figure above.
function drawGrayTube (x, r, sigma_wn, theta, del_rho, rmin, rmax,L)
% sigma_wn is in N/m (=Kg/s^2), theta is in degrees, del_rho is in Kg/m^3
% and L is in m
h=((2*sigma_wn*cos((pi/180)*theta))./(del_rho*9.81.*r));
hmin=((2*sigma_wn*cos((pi/180)*theta))./(del_rho*9.81.*rmax));
hmax=((2*sigma_wn*cos((pi/180)*theta))./(del_rho*9.81.*rmin));
rectangle ('Position',[x,0,r/rmax,h], 'FaceColor',[0.7,0.7,0.7]);
ylim([0 1]);
if L>h
rectangle ('Position',[x,L,r/rmax,L-h], 'FaceColor',[0.3,0.3,0.3]);
ylim([0 1]);
else
rectangle ('Position',[x,L,r/rmax,L], 'FaceColor',[0.3,0.3,0.3]);
ylim([0 1]);
end
end
A simple function to draw the gray tubes could be for instance
function drawGrayTube (x, w, h)
rectangle ('Position',[x,0,w,h], 'FaceColor',[0.7,0.7,0.7]);
rectangle ('Position',[x,h,w,100-h], 'FaceColor',[0.3,0.3,0.3]);
end
Hereby, x is the x position of the tube, w denotes the width and h between 0 and 100 the height of the light gray part of the tube.
You can now use it in your example by calling
drawGrayTube (x, r, 100*constant/r)
where you have to adapt the constant such that constant/r is at most 1.
You can write a similar function for the white interspaces.
Assume that you have given a vector of radii (already scaled such that the values are between 0 and 1), e.g., r=[0.5, 0.7, 0.9, 0.1, 0.5, 0.01] then on possibility to draw the tubes is
interspace = 0.5;
for i=1:length(r)
drawGrayTube(sum(r(1:i-1))+i*interspace, 100*r(i)+1e-10, r(i)+1e-10);
end
You should use the function rectangle