How can I get matlab to draw lines between points?

Basically, I have to draw a pentagram. I've got the points, I just need the lines to be drawn between them. Here's my code so far:
axis equal
r = input('Enter the distance from the origin: ');
while r < 0
r = input('The distance must be greater than 0: ');
end
angle = linspace(0,2*pi,6);
x = r*sin(angle);
y = r*cos(angle);
plot(x(2),y(2),x(5),y(5),'or-',x(5),y(5),x(3),y(3),'or-',x(3),y(3),x(6),y(6),'or-',x(6),y(6),x(4),y(4),'or-',x(4),y(4),x(2),y(2),'or-')
I don't understand why adding the "-" part of 'or-' isn't making the lines show up.

 Accepted Answer

To draw pentagram, the latter half of your script should be:
angle = linspace(0,2*pi,6);
x = r*sin(angle);
y = r*cos(angle);
idx = mod((1:2:12), 5) + 1;
plot(x(idx),y(idx),'or-');

More Answers (1)

You’re overthinking the plot call.
Try this:
plot(x, y, 'or-')
axis([-1 1 -1 1]*2.1*r)
axis equal

2 Comments

That's how I would do it if I wanted to make a pentagon, but I don't understand how to make the points go out of order to make a pentagram like this:
except without the circle around it.
Change the plot call to:
plot(x([1 3 5 2 4 1]), y([1 3 5 2 4 1]), 'or-')
That will work.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!