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

8 visualizaciones (últimos 30 días)
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

Respuestas (2)

Ameer Hamza
Ameer Hamza el 3 de Dic. de 2020
Editada: Ameer Hamza el 3 de Dic. de 2020
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 comentarios
Ameer Hamza
Ameer Hamza el 3 de Dic. de 2020
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 el 4 de Dic. de 2020
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?

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 4 de Dic. de 2020
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.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by