How can I get matlab to draw lines between points?
Show older comments
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
More Answers (1)
Star Strider
on 15 Nov 2017
You’re overthinking the plot call.
Try this:
plot(x, y, 'or-')
axis([-1 1 -1 1]*2.1*r)
axis equal
2 Comments
Benjamin Brown
on 15 Nov 2017
Edited: Benjamin Brown
on 15 Nov 2017
Star Strider
on 15 Nov 2017
Change the plot call to:
plot(x([1 3 5 2 4 1]), y([1 3 5 2 4 1]), 'or-')
That will work.
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!