plotting an N segments circle
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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
2019-3-21
1 个评论
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 Center 和 File Exchange 中查找有关 Elementary Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!