Question: Create the functions: o distance(xa, ya, xb, yb): this function calculates and returns the distance between (xa, ya) and (xb, yb). See Listing 2.9 for

Create the functions: o distance(xa, ya, xb, yb): this function calculates and returns the distance between (xa, ya) and (xb, yb). See Listing 2.9 for the formula for this calculation. o drawDotsInCircle(): This function draws 10 random dots in the circle described in the exercise. Use random.randint(0,100) and random.randint(-50,50) to generate random x and y values. Measure the distance to the center of the circle (using the distance() function above), and if the result is 50 or less, then draw a dot at that (x,y) location using drawPoint function from the UsefulTurtleFunctions.py module (see 6.14 for how to use this module). Use a while loop in which you count the points that are inside the circle. When the program has already counted 10, the loop can end. o drawDotsInSquare(): This function draws 10 random dots in the square described in the exercise. Use random.randint(-125,-25) and random.randint(-50,50) to generate random x and y values. These coordinates are guaranteed to be inside the square so, just draw a dot at the generated (x, y) point using drawPoint function from the UsefulTurtleFunctions.py module (see 6.14 for how to use this module). You can use a for loop because all the generated coordinates will be inside the square. o main(): This function coordinates the actions in the program so, it calls drawDotsInCircle and drawDotsInSquare. Add a call main at the end of theprogram to start its execution (for an example, see Listing 6.13). o You can download the UsefulTurtleFunctions.py module from the "Resources" section of the "Getting Started" panel of the course shell.

import turtle,random

# Draw a line from (x1, y1) to (x2, y2)

def drawLine(x1, y1, x2, y2):

turtle.penup()

turtle.goto(x1, y1)

turtle.pendown()

turtle.goto(x2, y2)

# text at the specified location (x, y)

def writeText(s, x, y):

turtle.penup() # Pull the pen up

turtle.goto(x, y)

turtle.pendown() # Pull the pen down

turtle.write(s) # string

# Draw a point at the specified location (x, y)

def drawPoint(x, y):

turtle.penup() # Pull the pen up

turtle.goto(x, y)

turtle.pendown() # Pull the pen down

turtle.begin_fill() # Begin to fill color in a shape

turtle.circle(3)

turtle.end_fill() # Fill the shape

# Draw a circle at centered at (x, y) with the specified radius

def drawCircle(x, y, radius):

turtle.penup() # Pull the pen up

turtle.goto(x, y - radius)

turtle.pendown() # Pull the pen down

turtle.circle(radius)

# Draw a rectangle at (x, y) with the specified width and height

def drawRectangle(x, y, width, height):

turtle.penup() # Pull the pen up

turtle.goto(x + width / 2, y + height / 2)

turtle.pendown() # Pull the pen down

turtle.right(90)

turtle.forward(height)

turtle.right(90)

turtle.forward(width)

turtle.right(90)

turtle.forward(height)

turtle.right(90)

turtle.forward(width)

def dotsInACircle(x,y,drawPoint):

dots = 0

while dots < 10:

drawPoint(random.randint(0,100),random.randint(-50,50))

dots += 1

return drawPoint

if dotsInACircle < drawCircle:

dotsInACircle

def dotsInARectangle(x,y,drawPoint):

dots = 0

while dots < 10:

drawPoint(random.randint(-125,-25),random.randint(-50,50))

dots +=1

return dotsInARectangle

if dotsInARectangle < drawRectangle:

dotsInARectangle

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!