Using while loop in a function?

18 visualizaciones (últimos 30 días)
Jenniluyn Nguyen
Jenniluyn Nguyen el 10 de Mzo. de 2020
Respondida: David Hill el 10 de Mzo. de 2020
Hello! First of all thank you for helping me out, this forum has done a lot to teach me more about MatLab.
I have a function that rotates a shape on a plot by however many degrees is inputted, which looks like this:
function [newx newy] = rotate(xcoords, ycoords, angle)
angle = angle*(pi/180); % convert angle to radians
newx = xcoords*cos(angle) - ycoords*sin(angle);
newy = xcoords*sin(angle) + ycoords*cos(angle);
I'm trying to write a second function with this function (we'll call rotate) with a while loop, but it does not seem to be working. What I want to do is when there is an input of x coordinates, y coordinates, and a number (which I assigned to repeats), it plots the specified number of rotations on a graph.
function [xc1, xc2] = spin(xcoords1, ycoords1,repeats)
degreeangle = 360/repeats;
hold on
while repeats > 1
[xc1, xc2] = rotate(xcoords1,ycoords1,degreeangle);
plot(xcoords1,ycoords1,'b-')
end
hold off
I am not sure why my code isn't working. Would appreciate any help! Thank you!

Respuesta aceptada

David Hill
David Hill el 10 de Mzo. de 2020
function spin(xcoords1, ycoords1,repeats)
degreeangle = 360/repeats;
hold on
while repeats > 1
[xcoords1, ycoords1] = rotate(xcoords1,ycoords1,degreeangle);
plot(xcoords1,ycoords1,'b-')
repeats=repeats-1;
end
hold off

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots 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