How to draw a curved line in matlab

I draw a graph that has N nodes and M edges. There may be an edge from node A to node B, as well as node B to A, so I cannot use a straight line to draw both lines. How can I make one of them curved to distinguish it from the other? Here is my code to draw one line between j and k.

line([Xloc(j) Xloc(k)], [Yloc(j) Yloc(k)], 'LineStyle', '-');

+4
source share
3 answers

You will need to determine which intermediate points you want to draw.

Then you can define them manually or take a look at spline interpolation.

With spline interpolation, you only need one point between them to determine the complete curve.

In MATLAB you can find a spline2d demo that does something like this. Here is its essence:

 % 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 intermediate points. If you just want your points to be connected seamlessly, this might be worth a look.

+8
source

This file sharing feature seems to be exactly what you need. From the description of the author:

Directional (one-sided) ribs are constructed as curved dashed lines with anti-clockwise curvature of curvature extending from the point

If you need additional functionality or customization, it should be easy for you to change the code to suit your needs.

+3
source

Instead of doing one curved, offset, or the other, you can use different linestyle for different directions:

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

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

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

-one
source

Source: https://habr.com/ru/post/1399516/


All Articles