MATLAB Function Generates the Vertices of a Regular N-gon with Line Segments
8 ビュー (過去 30 日間)
古いコメントを表示
My task is to write a MATLAB function that generates the verticies of a regular n-gon and draws a line segment between each pair of verticies. The routine is only to plot one line segment at a time and the graph can only be generated by one "plot" command. I have a very rough outline of a program written up. There are some issues with the program. It plots one less vertice than n and I have no line segments connecting every vertice. Please help me to figure out where to go next with my program.
function e3(n)
%
%
t = linspace(0,1,n);
x = cos(t.*2*pi);
y = sin(t.*2*pi);
plot(x,y)
axis off
axis equal
0 件のコメント
回答 (2 件)
Ameer Hamza
2020 年 12 月 3 日
編集済み: Ameer Hamza
2020 年 12 月 3 日
You need n+1 points, because points at angle of 0 and 2*pi are same.
t = linspace(0,1,n+1);
Can you explain, what do you mean by connecting all the vertices?
7 件のコメント
Ameer Hamza
2020 年 12 月 3 日
Try this
n = 15;
t = linspace(0,1,n+1);
x = cos(t.*2*pi);
y = sin(t.*2*pi);
[m1, m2] = ndgrid(1:n);
combs = unique(sort([m1(:) m2(:)],2), 'rows');
combs(diff(combs,[], 2)==0, :) = [];
hold on
for i = 1:size(combs,1)
plot(x(combs(i,:)), y(combs(i,:)), 'b')
end
axis off
axis equal
Steven Lord
2020 年 12 月 4 日
You might find the degree-based trig functions sind and cosd useful. If you need to work exclusively with radians, the sinpi and cospi functions may be useful as well.
Finally for your iterative plotting either the comet or animatedline and addpoints functions may be of use.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Animation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!