Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to draw curved line in Matlab

Tags:

matlab

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', '-');

like image 911
Sara Avatar asked Dec 21 '22 01:12

Sara


2 Answers

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

Resulting plot

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.

like image 117
Egon Avatar answered Jan 04 '23 18:01

Egon


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.

like image 25
foglerit Avatar answered Jan 04 '23 16:01

foglerit