# Regular polygon drawing with number of sides and number of points per side as input

3 Ansichten (letzte 30 Tage)
Albert am 30 Mär. 2022
Kommentiert: William Rose am 30 Mär. 2022
Hello, I want to generate the points that define a regular polygon (a hexagon in my case). I am able to generate the vertices as:
nsides = 6; % hexagon
stepsize = 360/nsides;
theta = 0:stepsize:359;
x = cosd(theta);
y = sind(theta);
This only generates the vertices (ie 1 point per side), but I also want to include the possibility to choose the number of points per side. For the hexagon case the code above generates 6 points, but I would like the hexagon to have also 6 additional points located at the centre of each side, so 12 in total. How could I improve the code above to do this? Thanks
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

William Rose am 30 Mär. 2022
There are different way you could go. One way is to define a time vector
t=0:6;
Define the corner coordinates points.
theta = 0:60:360;
x = cosd(theta);
y = sind(theta);
The initial point appear at the start and at the end. Choose the number of points per side:
M=10;
Create a time vector for interpolating the points:
t1=0:1/M:6;
Interpolate:
x1=interp1(t,x,t1);
y1=interp1(t,y,t1);
Plot x1 versus y1.
plot(x1,y1,'-k.');
axis equal
Done.
##### 2 KommentareKeine anzeigenKeine ausblenden
Albert am 30 Mär. 2022
Excellent!! Works perfectly! Thanks
William Rose am 30 Mär. 2022
@Albert, you're welcome.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Elementary Polygons finden Sie in Help Center und File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by