jeudi 25 février 2016

Don't know how to generating random dots inside my shapes

Inside turtle graphics, I drew a rectangle and a circle side by side. How can I put 10 random dots in each of my shapes? Here is a picture of what it needs to look like:enter image description here

Here are some of the rules I need to fulfill to complete this: enter image description here

Here is the code and algorithm that was given to be used as a template:

-This is for 10 random dots in the rectangle:

import random
x = random.randint(-100, 100)
y = random.randint(-50, 50)

Repeat 10 times:
   x = random x value inside the square
   y = random y value inside the square
   Call drawPoint from UsefulTurtleFunctions
End Repeat

-This is for 10 random dots in the circle:

import random
x = random.randint(-20, 30)
y = random.randint(50, 100)

count_dots_in_circle = 0
While count_dots_in_circle < 10 Do
   x = random x value inside the square enclosing the circle
   y = random y value inside the square enclosing the circle
   If (x,y) is inside the circle Then
      Add one to count_dots_in_circle
      Call drawPoint from UsefulTurtleFunctions to show (x,y) point
   End If

Also this will calculate distance in some way:

def distanceBetween(x1, y1, x2, y2):
   d = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
   return d

Finally, here is the code I have:

import turtle
import math
import randint 

# draw a rectangle at a specific location
def drawRectangle(x = -75, y = 0, width = 100, height = 100): 
    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)    


# Draw a circle at a specific location
def drawCircle(x = 50, y = 0, radius = 50): 
    turtle.penup() # Pull the pen up
    turtle.goto(x, -50)
    turtle.pendown() # Pull the pen down
    turtle.begin_fill() # Begin to fill color in a shape
    turtle.circle(radius) 




Aucun commentaire:

Enregistrer un commentaire