目标是使用以下代码在我创建的矩形内生成随机点:
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()
plotPoints
部分是我遇到麻烦的地方。任何帮助找到范围和正确的函数来生成随机点都会很棒。感谢。