MATLAB Function Generates the Vertices of a Regular N-gon with Line Segments

8 ビュー (過去 30 日間)
Sophie Culhane
Sophie Culhane 2020 年 12 月 3 日
回答済み: Steven Lord 2020 年 12 月 4 日
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

回答 (2 件)

Ameer Hamza
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
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
Sophie Culhane
Sophie Culhane 2020 年 12 月 4 日
I am not able to use commands "hold on", "ndgrid", "sort", or "diff" as we have not learned those yet. Is there another way to go about this problem without those commands?

サインインしてコメントする。


Steven Lord
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.

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by