Modify your Turtle class to allow the user to change the pen color and pen width.
5 views (last 30 days)
Show older comments
Modify your Turtle class to allow the user to change the pen color and pen width. The initial color is ‘black’, and the initial width is 1. Test your code by drawing rainbow concentric boxes. You will need to add two properties to the Turtle class: pen_width – the width of the pen pen_color – the color of the pen You will need to add two methods to the Turtle class: obj = penColor(obj,new_color) – changes the pen color obj = penWidth(obj,new_width) – changes the pen width You will need to change the code of the forward(obj,distance) method to use the stored values of pen_width and pen_color properties instead of the hardcoded values.
This is the code that I have so far:
classdef Turtle
properties
% location of turtle
x
y
% 0 is E, 90 is N, 180 is W, 270 is S
heading
% pen status
pen_on_paper
% width of pen
pen_width
% color of pen
pen_color
end
methods
function obj = Turtle() %constructor
% make a new turtle
obj.x = 0;
obj.y = 0;
obj.heading = 90;
obj.pen_on_paper = true;
obj.pen_width = [];
obj.pen_color = [];
end
function obj = forward(obj,distance)
% move forward in current heading given distance
x2 = obj.x + distance*cosd(obj.heading);
y2 = obj.y + distance*sind(obj.heading);
if obj.pen_on_paper
% draw a line
hold on
l = line([obj.x x2],[obj.y y2]);
l.Color = penColor;
l.LineWidth = penWidth;
l.LineWidth = 1;
hold off
pause (0.1)
end
% update location
obj.x = x2;
obj.y = y2;
end
function obj = penWidth(obj)
new_width = [1,2,3,4];
obj.pen_width = rand(new_width);
end
function obj = penColor(obj)
new_color = ['yellow','red','blue','green','black','orange','purple'];
obj.pen_color = rand(new_color);
end
end
end
I also have another .m file to draw the squares, I just can't figure out to do the rainbow boxes with different line widths.
function s = square(s,side)
s = Turtle();
l = 100;
for i = 1:10
for j = 1:4
s.pen_on_paper = true;
s = s.forward(l);
s = s.rotate(90);
end
l = l - 10;
s.pen_on_paper = false;
s = s.forward(5);
s = s.rotate(90);
s = s.forward(5);
s = s.rotate(270);
l.Color = rand('yellow','blue','red','purple','black');
end
end
1 Comment
Adam
on 17 Oct 2016
Your penWidth and penColour functions should take an argument to set these according to your question, rather than set them randomly.
You presumably then need to call these with different colours/widths between drawing different squares.
In your square function, l appears to be a numeric variable so you can't assign colour to it as
l.Color = rand('yellow','blue','red','purple','black');
I assume here you should be using
t.penColour( [1,0,0] )
or something similar.
Answers (1)
Guillaume
on 17 Oct 2016
D- for effort.
As your assignment says: "You will need to add two methods to the Turtle class: obj = penColor(obj,new_color) – changes the pen color obj = penWidth(obj,new_width) – changes the pen width"
You've not done that yet. Your functions only have one input whereas you're told to have two. Once you've got two inputs, it's just a matter of assigning that second input to the object property.
You're then told "You will need to change the code of the forward(obj,distance) method to use the stored values of pen_width and pen_color properties instead of the hardcoded values". Again, you've not done that. Instead you're calling the above methods (with no arguments, so it's going to fail).
After that, learn how to use rand. Writing code at random (pun intended) is not going to produce good results.
rand('yellow','blue','red','purple','black')
has absolutely no meaning.
Finally, you should also learn how to manipulate character arrays. Note that
new_color = ['yellow','red','blue','green','black','orange','purple'];
is the same as
new_color = 'yellowredbluegreenblackorangepurple';
and probably not what you want. If you want to store several char arrays in a container, you have to put them into a cell array:
valid_colours = {'yellow','red','blue','green','black','orange','purple'}; %note the {}
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!