Home > Software design >  Line plot - Matlab
Line plot - Matlab

Time:12-30

When I run this code:

pts= [-1 0; 0 1; 1 0; 0 -1];
xCenter = 0;
yCenter = 0;
plot(xCenter,yCenter,pts(:,1), pts(:,2),'g');

I get this plot: enter image description here

What do I need to change so I will plot the sign, that all the lines will start from the center.

CodePudding user response:

Just add the origin between every pair of points

pts= [-1 0; 0 0; 0 1; 0 0; 1 0; 0 0; 0 -1];

otherwise you're skipping between the end-points of the " ", which is why you get 3 sides of a square.

You could do this automatically with something like

pts= [-1 0; 0 1; 1 0; 0 -1];
xCenter = 0;
yCenter = 0;

% Initialise the array to all be the center point
newPts = repmat( [xCenter, yCenter], size(pts,1)*2-1, 1 );
% Every other row comes from the original "pts"
newPts(1:2:2*size(pts,1),:) = pts;
  • Related