I am drawing a plot that has N nodes and M edges. There can be an edge from node A to node B and also node B to A so I can't use straight line to draw both line. How can I make one of them curved in order to be distinguishable from the other one?here is my code to draw one straight line between j and k.
line([Xloc(j) Xloc(k)], [Yloc(j) Yloc(k)], 'LineStyle', '-');
You will need to define what intermediate points you want to be drawn.
Then you can either define them manually, or take a look at spline interpolation.
With spline interpolation, you only need a single point in-between to determine the full curve.
In MATLAB you can find the demo spline2d
which does something like this. Here is the gist of it:
% end points
X = [0 1];
Y = [0 0];
% intermediate point (you have to choose your own)
Xi = mean(X);
Yi = mean(Y) + 0.25;
Xa = [X(1) Xi X(2)];
Ya = [Y(1) Yi Y(2)];
t = 1:numel(Xa);
ts = linspace(min(t),max(t),numel(Xa)*10); % has to be a fine grid
xx = spline(t,Xa,ts);
yy = spline(t,Ya,ts);
plot(xx,yy); hold on; % curve
plot(X,Y,'or') % end points
plot(Xi,Yi,'xr') % intermediate point
In splined2
, it is used for a larger set of points, but without the intermediate points. If you just want your points to be connected smoothly, that might be worthwhile to take a look at.
This function from the File Exchange seems to be exactly what you need. From the author's description:
Directed (1-way) edges are plotted as curved dotted lines with the curvature bending counterclockwise moving away from a point
If you need extra functionality or tweaks, it should be simple to change the code to your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With