0

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Josh K
  • 23
  • 4
  • So what, exactly, does *"running into trouble"* mean? – jonrsharpe Oct 11 '14 at 17:30
  • @jonrsharpe The code works properly without error; however, no points are plotted. I realize the code that I have printed now in the def plotPoints section isn't the correct one for what I want to do, I just don't know how to properly do it. – Josh K Oct 11 '14 at 17:39
  • I think your problem is that `p1=Point(random.randint(0,side), 500)`. Your y is out of the range of your window `0 – dreyescat Oct 11 '14 at 17:41
  • @dreyescat if I change the for statement to the following: for i in range(0 – Josh K Oct 11 '14 at 17:45
  • @JoshK if only there was a way you could find out... (but no). – jonrsharpe Oct 11 '14 at 17:47
  • @jonrsharpe What I meant in the last comment was that I tried the change and it didn't work, so I was asking if that was the issue. I used the following code to try a new solution( for i in range(20):print( random.randint(0,10) )). This gives me the value of 10 random points. I now know the ranint code works, but I have no clue how to get it so that these points will show up within my range. – Josh K Oct 11 '14 at 17:57
  • You need to set both the `x` **and `y`** of each `Point` within the bounds (at the moment, you always set `y=500`). – jonrsharpe Oct 11 '14 at 17:58
  • @jonrsharpe Are you saying that line 18 needs to be edited so that I'm using coordinates rather than a boundary? – Josh K Oct 11 '14 at 18:11
  • @jonrsharpe just kidding.. here's the code I'm using. It generates points, however, regardless of what my input is it places the same amount of points...def plotPoints(win, side, pts): for i in range(250-side//2, 250+side//2): pts=Point(random.randint((250-side//2), (250+side//2)), i) pts.draw(win) – Josh K Oct 11 '14 at 19:02

0 Answers0