Sara
Sara

Reputation: 2436

how to draw curved line in 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', '-');

Upvotes: 4

Views: 13919

Answers (3)

foglerit
foglerit

Reputation: 8269

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.

Upvotes: 3

Egon
Egon

Reputation: 4787

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.

Upvotes: 9

tmpearce
tmpearce

Reputation: 12693

Rather than making one curved, offset, or otherwise, you could use different linestyles for the different directions:

Line 1: plot(..., 'Linestyle', '-', 'Linewidth', 1)

Line 2: plot(..., 'Linestyle', '.-', 'Linewidth', 3)

this would make your lines in different directions distinguishable without requiring an arbitrary shift in space.

Upvotes: -1

Related Questions