How can I plot a 2D figure in MATLAB by "connecting the dots"?

$\begingroup$

I'm an amateur at MATLAB and I know how to do the basic stuff such as plotting functions, writing M files, functions, for loops, etc but I've be tasked to do something I have never done before.

I have to draw this figure which resembles a Tree or Home (2D childs drawing), and I know all of its points: (6,1), (8,1), (6, 3) (8,3) makes the base of the house (and the diagonals are drawn too) and then the points (6,3), (7,4), (8,3) make the roof.

How do I connect the points in a way that it resembles what I'm looking at?

Thanks.

$\endgroup$ 3

1 Answer

$\begingroup$
points = [ 6 1 8 1 6 3 8 3 7 4];
sequence = [1 2 4 3 1 4 2 3 5 4];
plot(points(sequence, 1), points(sequence, 2), '-o')
set(gca, 'XLim', [5, 9], 'YLim', [0, 5],'DataAspectRatio', [1 1 1])
figure(gcf)

Perhaps more idiomatically:

points = [ 6 1 8 1 6 3 8 3 7 4];
sequence = [1 2 4 3 1 4 2 3 5 4];
u = points(sequence, :);
plot(u(:, 1), u(:, 2), '-o')
set(gca, 'XLim', [5, 9], 'YLim', [0, 5],'DataAspectRatio', [1 1 1])
figure(gcf)
$\endgroup$ 2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like