Create Random Points Inside Defined Rectangle with Python

你说的曾经没有我的故事 提交于 2019-12-25 07:03:27

问题


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.

来源:https://stackoverflow.com/questions/26317291/create-random-points-inside-defined-rectangle-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!