plotting an N segments circle

조회 수: 4 (최근 30일)
Al Alothman
Al Alothman 2019년 3월 20일
댓글: Star Strider 2019년 3월 21일
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

채택된 답변

Star Strider
Star Strider 2019년 3월 20일
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
Al Alothman
Al Alothman 2019년 3월 20일
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 2019년 3월 21일
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.)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Al Alothman
Al Alothman 2019년 3월 21일
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
Star Strider
Star Strider 2019년 3월 21일
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.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by