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...
Write a function that plots the following trajectory equations using python turtle graphics. x = v*sin(A)*t...
Write a function that plots the following trajectory equations using python turtle graphics. x = v*sin(A)*t y = v*cos(A)*t -.5*(9.8)*t**2 Loop through t starting at 0 and increment by .1 until y<=0. (hint, while loop) Experiment with values of velocity V (1 to 20) and angle A (0 to 90) that gives you a good plot that does not go off the screen. Position the start of the plot at the left bottom of the screen
Python program please no def, main, functions Given a list of negative integers, write a Python...
Python program please no def, main, functions Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly...
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 <...
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()
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...
language python use a functions and write a python of a computer with three power operations:...
language python use a functions and write a python of a computer with three power operations: power by two, power by three, and power by four. input a number and operation (^2,^3,or^4). output: the result of the power operation
In Python, I have a turtle that is already coded to move in correlation with the...
In Python, I have a turtle that is already coded to move in correlation with the keys I set (I can control the turtle). I need to put a second turtle in that moves WHILE the first turtle is moving and being controlled. The second turtle needs to do the following while the first turtle is still listening and being controlled: -It needs to begin facing a random direction -It needs to move at a speed of .5 the entire...
with python, create a fake logo for a company using turtle graphics
with python, create a fake logo for a company using turtle graphics
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT