plotting an N segments circle

4 visualizaciones (últimos 30 días)
Al Alothman
Al Alothman el 20 de Mzo. de 2019
Comentada: Star Strider el 21 de Mzo. de 2019
Hi all,
I am trying to plot an N segments shape using this line of code:
N =10;
theta = (0: (2*pi)/N : 2 * pi) ;
theta_centers = (pi/2: (2*pi)/N : 4*pi) ; % this is to find the centers of the prevoius line
xb = 0.02*sin(theta) ;
yb = 0.02*cos(theta) ;
xbc = 0.02*sin(theta_centers) ; % to find the centers of xb
ybc = 0.02*cos(theta_centers) ;
plot(xb,yb,'r',xbc,ybc,'o')
%%%%%%
after running this code, the 'o' are not on the main function lines....
WHY?
thanks

Respuesta aceptada

Star Strider
Star Strider el 20 de Mzo. de 2019
WHY?
Because you are plotting a decagon rather than a circle. If you want them to be on the line, you need to reduce their radius to times the circle radius, so:
Ns = N; % Number Of Sides Of Polygon
ro = sin(pi*(Ns-2)/(2*Ns));
xbc = ro*0.02*sin(theta_centers) ; % to find the centers of xb
ybc = ro*0.02*cos(theta_centers) ;
I will let you ponder that derivation in detail.
However it will help to know that each segment of the circle subtends an angle of , so half of that is . The angle formed by the radius passing through the center of the chord of each segment is a right angle, , so the remaining angle is . Multiply the sine of that angle by the radius of the circle (here 0.02) to put the ‘o’ markers on the lines between the segments.
  2 comentarios
Al Alothman
Al Alothman el 20 de Mzo. de 2019
Thank you so much!
I've noticed for even numbers of N, (xb,yb) & (xbs,ybc) overlaps, as for 20,40,60...
I appriciate your help
n_20.jpg
Star Strider
Star Strider el 21 de Mzo. de 2019
My pleasure.
I've noticed for even numbers of N, (xb,yb) & (xbs,ybc) overlaps, as for 20,40,60...
It depends on how you define the polygon. As an experiment (and to test my code), I came up with this before I posted my Answer:
N = 60;
theta = linspace(0, 2*pi, N);
theta_centers = theta + pi/(N-1);
xb = 0.02*sin(theta) ;
yb = 0.02*cos(theta) ;
Ns = N - 1; % Number Of Sides Of Polygon
ro = sin(pi*(Ns-2)/(2*Ns));
xbc = ro*0.02*sin(theta_centers) ; % to find the centers of xb
ybc = ro*0.02*cos(theta_centers) ;
figure
plot(xb,yb,'+-r',xbc,ybc,'o')
axis equal
axis([-0.017 -0.012 -0.017 -0.012]) % Zoom Axis (Delete To See Entire Circle)
There is never an overlap, and the ‘o’ markers are always mid-way between the polygon ‘+’ angle markers. (The ‘Ns’ variable is defined differently here because of the way linspace creates its vectors. The rest of the code is the same.)

Iniciar sesión para comentar.

Más respuestas (1)

Al Alothman
Al Alothman el 21 de Mzo. de 2019
Thank you so much!
in my previous code, I started my theta from (pi/2: (2*pi)/N : 4*pi) to get the first edge of the polygon at 0, now it is starts away from the xb = 0,02 and yb = 0
any advice?
Thanks!
  1 comentario
Star Strider
Star Strider el 21 de Mzo. de 2019
As always, my pleasure!
My code only does the circumference one time. (The plot looks really strange otherwise.) If you want it to start at pi/2 (or any other angle), add that value to the ‘theta’ vector created by linspace:
theta = linspace(0, 2*pi, N) + pi/2;
The rest of my code remains the same. (The polygon then rotates by the added angle.)

Iniciar sesión para comentar.

Categorías

Más información sobre Elementary Polygons en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by