Question

In: Computer Science

I need to write these three functions in Python using turtle module. def line(t, coord1, coord2)...

I need to write these three functions in Python using turtle module.

def line(t, coord1, coord2)

  • t is a turtle object passed in
  • coord1 and coord2 are 2-element lists that represent x, y coordinates
  • draws a line from coord1 to cord2

def poly(t, *coords)

  • t is a turtle object passed in
  • *coords is any number of x, y coordinates each as a 2-element list
  • draws a polygon based on coords (will close off polygon by drawing a line from last coord to first)

def rectangle(t, coord, width, height, color)

  • t is a turtle object passed in
  • coord is upper left corner of rectangle
  • assume width and height are ints
  • color is the color to fill with
  • (use t.color('red'), t.begin_fill(), and t.end_fill())

Solutions

Expert Solution

Function 1: line

Code:-

import turtle

def line(t, coord1, coord2):
        t.penup()
        t.goto((coord1[0], coord1[1]))
        t.pendown()
        t.goto((coord2[0], coord2[1]))

line(turtle, [50, 100], [100, 50])
turtle.hideturtle()
turtle.exitonclick()

Screenshot:-

Output:-

Function 2: Polygon

Code:-

import turtle

def poly(t, *coords):
        t.penup()
        t.goto(coords[0])
        t.pendown()
        i = 0
        while i < len(coords)-1:
                t.goto(coords[i+1])
                i += 1

        t.goto(coords[0])

poly(turtle, [50, 100], [100, 50], [0, 50], [0, 200], [200, 100])
turtle.hideturtle()
turtle.exitonclick()

Screenshot:-

Output:-

Function 3: Rectangle

Code:-

import turtle

def rectangle(t, coord, width, height, color):
        t.fillcolor(color)
        t.pencolor(color)
        t.begin_fill()

        t.penup()
        t.goto(coord)
        t.pendown()
        t.goto([coord[0]+width, coord[1]]) #Goes to the co ordinate width pixels to the right
        t.goto([coord[0]+width, coord[1]+height]) #Goes to the point width pixels to the right and height pixels down
        t.goto([coord[0], coord[1]+height]) #Goes to the point height pixels down
        t.goto(coord)#Back to start

        t.end_fill()

rectangle(turtle, [100, 50], 100, 50, "red")
turtle.hideturtle()
turtle.exitonclick()

Screenshot:-

Output:-

I hope this resolved your doubts. If you have further doubts do let me know.


Related Solutions

Write a function that draw your initials. (Using python 3 turtle) (T, S)
Write a function that draw your initials. (Using python 3 turtle) (T, S)
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
PYTHON: Write a script that imports the functions in the module below and uses all of...
PYTHON: Write a script that imports the functions in the module below and uses all of the functions. import math def _main():     print("#" * 28, "\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="")     f = -200     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     f = 125     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     c = 0     print("{:.2f} C is {:.2f} F".format(c, c2f(c)))     c = -200     print("{:.2f} C is {:.2f} F".format(c, c2f(c))) def f2c(temp):     #Converts Fahrenheit tempature to Celcius     if temp <...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
I need to draw my first name using Turtle. I am struggling with "P" and "A"...
I need to draw my first name using Turtle. I am struggling with "P" and "A" this is what I have import turtle turtle.setup(800, 700) window = turtle.Screen() window.reset() window.bgcolor('cornsilk') t = turtle.Turtle() t.color('blue') t.pencolor('red') t.turtlesize(6) t.speed(2) t.up() t.setposition(-50, 0) t.pendown()#Drawing letter T t.forward(40) t.back(20) t.right(90) t.forward(50) t.left(90) t.penup() t.forward(70) t.left(90) t.forward(25) t.pendown() t.circle(25)# Drawing letter O t.penup() t.left(180) t.forward(25) t.left(90) t.forward(10) t.pendown()#Drawing letter N t.left(90) t.forward(50) t.right(150) t.forward(60) t.left(150) t.forward(53) t.back(53) t.right(90) turtle.done()
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and use a function named highest() that takes three inputs and returns the highest number. After you have got it working, try calling the function with inputs ‘hat’, ‘cat’, ‘rat’.
Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.
Modules Assignment pythonWrite a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.Write a separate Python program (in a separate file from you Python module) that imports your above module and uses several of its functions in your program. Make use of the input() and print() functions in this program file. Try to make your program as a real-world application.
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line is replacing all the '1' values across my .csv file and is throwing off my data aggregation. How would I implement 'iloc' so that this line of code only changes the '1's integers in my 'RIC' column? patient_data_df= patient_data_df.replace(to_replace=[1], value ="Stroke") Thank you:)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT