Tuesday, 15 February 2011

Create Random Points Inside Defined Rectangle with Python -



Create Random Points Inside Defined Rectangle with Python -

the goal generate random points within rectangle created using next 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): 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 size of 1 side of square (0<n<500): ")) color=input("what color 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 i'm running trouble. help on finding range , right function generating random points great. thanks.

python random points

No comments:

Post a Comment