Azzera filtri
Azzera filtri

Create a Pen class and integrate it into Turtle

1 visualizzazione (ultimi 30 giorni)
Instead of having the Turtle remember the color and width of the pen, just give the Turtle a pen object to use. The Pen class should have properties color and width and methods Pen(), setColor(), setWidth(). Add a setPen() method to Turtle to handle changing the Turtle’s pen. You will also need to make small modifications to your other code to reflect the fact that the Turtle has a Pen object as a property
How do I define the pen object and the corresponding classes? This is what I have so far, but it does not run:
classdef Turtle
%TURTLE Turtle with a pen
%Turtle with a pen
properties
%location of turtle
x
y
%0 is E, 90 is N, 180 is W, 270 is S
heading
%pen status
pen_on_paper
%pen
pen
end
methods
function obj=Turtle()
%make a new Turtle
obj.x=0;
obj.y=0;
obj.heading=90;
obj.pen_on_paper= true;
obj.pen = true;
obj.pen = true;
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 line
hold on
l= line([obj.x x2], [obj.y y2]);
l.Color='black';
l.LineWidth=1;
hold off
pause(0.1)
end
%update location
obj.x=x2;
obj.y=y2;
end
function obj = rotate(obj,angle)
% rotate CCW by given angle
obj.heading = mod(obj.heading + angle,360);
end
function obj = penUp(obj)
obj.pen_on_paper = false;
end
function obj = penDown(obj)
obj.pen_on_paper = true;
end
function obj= Pen(color, width)
obj.pen= True
end
function obj= SetColor()
obj.pen= color
end
function obj= setWidth()
obj.pen = width
end
function obj = setPen(obj)
obj.pen= True
end
end
end
  1 Commento
Guillaume
Guillaume il 17 Ott 2016
You, him and him should get together to solve this.
And maybe go through your course notes. None of you have made a serious attempt at solving this.

Accedi per commentare.

Risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by