Question: turtle This is the main function for the assignment. You will be simulating the following. Imagine a little green turtle is dipped in black ink
turtle
This is the main function for the assignment. You will be simulating the following. Imagine a little green turtle is dipped in black ink and then set loose. As it walks in a straight line, it leaves a trail of paint behind it. Every so often, the turtle turns some number of degrees and then continues moving. When the turtle is finally done moving, you will have a "line in the sand" that will show you were the turtle has been.
Your job is to create this March of the Turtles in MATLAB.
The turtle function takes in a bunch of information.
start - the 2d point where the turtle starts
direction - the 2d vector that the turtle is moving in
distance - how far the turtle moves before turning
turn - the number of degrees that the turtle turns each time
repeats - the number of times the turtle moves and turns, moves and turns...
Inside your function, you should:
verify that number of inputs to the function is correct
make sure the direction vector is normalized
draw a line from where the turtle is to the next point the turtle will be at.
turn the turtle
repeat the previous 2 steps until the number of repeats is up.
Note: In order to make each line show up as you draw it, rather than waiting for all the lines to be drawn and then showing the picture as a whole, you are to use the drawnow() function after each draw_line function in your turtle program.
To compute where the turtle will be next, you can add the current location of the turtle to the direction vector multiplied by the distance variable. To figure out where the turtle is going next you can "rotate_vector" the direction vector.
the draw_line function is :
function draw_line( line, attributes ) if nargin() == 1 attributes.color = 'r'; attributes.width = 1; end x_start = line.start_pt.x; y_start = line.start_pt.y; x_end = line.end_pt.x; y_end = line.end_pt.y;
plot([x_start x_end],[y_start y_end],'Linewidth', attributes.width, 'Color', attributes.color); drawnow() end
and a function for rotate_vector is:
unction rovect = rotate_vector( vector,theta ) rotate.x = (cos(theta)*vector.x)-(sin(theta)*vector.y); %This equation will rotate the vector by theta rotate.y = (sin(theta)*vector.x)+cos(theta)*vector.y; rovect = {rotate.x, rotate.y}; end
there is also one for normalizing a vector which is:
function norm = normalize(my_vector) mag=magnitude(my_vector); norm.x = my_vector.x/mag; norm.y = my_vector.y/mag;
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
