How can I modify the code below to build a hexagon knowing the coordinates of the hexagon roofs?

1 view (last 30 days)
Function []=polygon (sides)
sides=6;
radians=(2*pi)./sides;
r=ones(1,sides);
theta=1:radians:2*pi;
polar(theta,r)
end
I need to show the hexagon in the coordinate plane.The coordinates of each point should be the radius and angle from a specific point.

Accepted Answer

Star Strider
Star Strider on 12 Jan 2018
Try this:
function xy = polygon(sides)
radians=(2*pi)./sides;
r=ones(1,sides+1);
theta=0:radians:2*pi;
xy = [r(:).*cos(theta(:)) r(:).*sin(theta(:))];
end
then:
sides=6;
XY = polygon(sides) % Call ‘polygon’ Function
figure
plot(XY(:,1), XY(:,2))
axis([-1.1 1.1 -1.1 1.1])
axis equal
For best results, save the function to its own file as: polygon.m
  6 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!