Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In python please, please add comments explaining where needed, thank you! This p

ID: 3828953 • Letter: I

Question

In python please, please add comments explaining where needed, thank you!

This part of project will give you more experience with defining classes and using inheritance. In this part , you are going to use turtle graphics to define some elementary shapes that know how to draw themselves. You are going to create at least 3 classes . Using instances of these classes, you are going to draw a simple picture.

Your Task

1. You will define at least three classes: A Triangle class, a Rectangle class, and a Circle class.

2. Instances of each class respond to at least the following methods:

a. __init__ : create an instance of the class from the following:

   i. For a Triangle: three pairs of coordinates, which indicate the three vertices (not a list, but three arguments, which you may give some default values for easy testing)

   ii. For a Rectangle: two pairs of coordinates—one giving the coordinates of the lower left corner and the other giving the coordinates of the top right corner (not a list, but two arguments).

   iii. For a Circle: a coordinate pair giving the center of the circle and a float giving the radius

   iv. For all, a string indicating a fill color (default value “”, the empty string)

   v. For all, a string indicating the line color (default value “black”)

b. __str__ : conversion to a string; returns the string to be used for printing in the Python shell.

c. draw: will take a turtle.Turtle object and will use it for drawing the shape;

3. All classes, methods and functions require a docstring for a general description of the object/method/function. (see docstring note below)

4. Triangle and Rectangle should both inherit from Polygon and reuse what they can from Polygon.

5. Once you have tested your class definitions, draw a simple picture using them. For instance, you could draw a house using rectangles, triangles, circles and lines. The only requirement is that it should be made up of multiple shapes.

Using turtle graphics : In order to use turtle graphics in Python you must first import the turtle module.

You can then use the help function in idle to find out what methods this module includes and what they do. Just type import turtle in the idle command window, hit enter, and then type help(turtle) and scroll up through the list and information. Assuming the assignment pen = turtle.Turtle(), some useful features include,:

• pen.up(),pen.down(): Set the pen state to be up (not drawing) or down (drawing)

• pen.right(degrees), pen.left (degrees): Turn the direction that the pen is facing. The amount of turn is indicated in degrees.

•pen.forward(distance), pen.backward(distance): Move the pen forward or backward the amount of distance indicated. Depends on the direction the pen is facing.

Draws a line if the pen is down, not if the pen is up.

• pen.goto(x,y): Move the pen to the specified point, drawing a line along the way if the pen is down, and not drawing if the pen is up.

• pen.pencolor(r,g,b), pen.pencolor(s): Set the color that the pen will hold for all drawing until the pen color is changed. In the first version, each argument is a floating point number between 0.0-1.0; the first is the amount of red, the second, the amount of green and the third the amount of blue. In the second version, the argument is a string indicating a color by name or by its hexcode, e.g., “green”, “red”, “#66FFFF”. Hex color codes are at: www.web-source.net/216_color_chart.htm

•pen.fillcolor(r, g, b), pen.fillcolor(s): Set the color for filling figures. Arguments are the same as for the pen color.

•pen.circle(radius): draw a circle of the indicated radius. The circle is drawn tangent to the direction of the pen in a clockwise direction (if radiusis positive).

•pen.write(string): Write a string starting at the present pen point.

•To fill a figure, use the command pen.begin_fill() before you start drawing it. Draw the figure,then execute the command pen.end_fill(). The figure drawn between the two fill commands will be filled with the present color setting.

• pen.clear(): Clear (erase) everything written by the pen.

The picture below illustrates a house scene that could be drawn using your classes.

Explanation / Answer

code in python:

import turtle
class Polygon:

def __init__(self,numsides, startloc, color ):
self.sides = numsides
self.location = startloc
self.fillcolor = color

def numSides(self):
return self.sides

def draw(self, pen):
pen.goto(self.location[0], self.location[1])
pen.fillcolor(self.fillcolor)
pen.down()
pen.begin_fill()

class Triangle(Polygon):
def __init__(self, v1, v2, v3 , color):
Polygon.__init__(self,3,v1, color)
self.vertex1 = v1
self.vertex2 = v2
self.vertex3 = v3

def draw(self,pen):
Polygon.draw(self,pen)
pen.goto(self.vertex2[0],self.vertex2[1])
pen.goto(self.vertex3[0],self.vertex3[1])
pen.goto(self.location[0],self.location[1])
pen.up()
pen.end_fill()

def __str__(self):
return "Triangle( " + str(self.location) + ", " + str(self.vertex2) + ", " + str(self.vertex3) + " )";


class Rectangle(Polygon):
def __init__(self,v1,v2,color):
Polygon.__init__(self,4,v1,color)
self.vertex2=(v2[0],v1[1])
self.vertex3=v2
self.vertex4=(v1[0],v2[1])

def draw(self, pen):
Polygon.draw(self,pen)
pen.goto(self.vertex2[0],self.vertex2[1])
pen.goto(self.vertex3[0],self.vertex3[1])
pen.goto(self.vertex4[0],self.vertex4[1])
pen.goto(self.location[0],self.location[1])
pen.up()
pen.end_fill()

def __str__(self):
return "Rectagle( "+str(self.location) + " , " + str(self.vertex3) + " )"

class Circle:
def __init__(self, cent, rad, col):
self.center = cent
self.radius = rad
self.color = col

def draw(self, pen):
pen.goto(self.center[0],self.center[1])
pen.fillcolor(self.color)
pen.begin_fill()
pen.down()
pen.circle(self.radius)
pen.up()
pen.end_fill()

def __str__(self):
return "Circle (" + str(self.center) + ", " + str(self.radius) + " )"

basesq = Rectangle((0,0) , (100,100),"blue")
window1 = Rectangle( (15,70), (30,85),"gray")
window2 = Rectangle( (70,70), (85,85), "gray")
door = Rectangle( (40,0) , (55,30),"green")
roof = Triangle( (0,100), (100,100), (50,150), "red")
lock = Circle( (44,12),2,"black")

#demostrate __str__ for different classes
print(basesq)
print(window1)
print(window2)
print(door)
print(roof)
print(lock)

wn = turtle.Screen() # creates a graphics window
pen = turtle.Turtle()
basesq.draw(pen)
window1.draw(pen)
window2.draw(pen)
door.draw(pen)
roof.draw(pen)
lock.draw(pen)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote