Create Random Points Inside Defined Rectangle with

2019-09-06 06:43发布

问题:

The goal is to generate random points inside a rectangle that I created using the following code:

from graphics import *
import random
import math

def plotSquare(win, side):
    rect=Rectangle(Point(500/2-side//2,500/2-side//2), Point(500//2+side//2,500//2+side//2))

    rect.setWidth(5)
    rect.draw(win)

def plotCircle(win, radius, color):
    cir=Circle(Point(250,250), (radius))
    cir.setFill(color)
    cir.draw(win)

def plotPoints(win, side, pts):
    for i in range(250-side//2):
        p1=Point(random.randint(0,side), 500)
        p1.draw(win)

def main ():
    win=GraphWin("My Window", 500, 500)
    win.setCoords(0, 0, 500, 500)
    win.width=500
    win.height=500

    side=eval(input("What is the size of one side of the square (0<n<500): "))
    color=input("What is the color for circle (red/green/blue): ")
    radius=side//2
    pts=eval(input("How many points: "))



   plotSquare(win, side)
    plotCircle(win, radius, color)
    plotPoints(win, side, pts)

    win.getMouse()
    win.close

main()

The plotPoints section is where I'm running into trouble. Any help on finding the range and correct function for generating random points would be great. Thanks.