I'm trying to create non-overlapping rectangle flashcards and place them at random locations in Zelle Graphics. xMin and yMin is the coordinate of the upper left corner, and xMax and yMax is of the lower right corner of the rectangle. I tried generating random (xCenter,yCenter) coordinate to make a new rectangle, and make a check if the new rectangle overlaps any existing rectangles. If it's overlapped then generate new random points until it's not overlapped anymore.
I got the function for checking overlap, but I'm having trouble with the while loop afterwards. I'm a beginner at Python, so I'd very appreciate your help!
from graphics import *
from random import randrange *
def checkLocation(xMin,xMax,yMin,yMax,xMinList,xMaxList,yMinList,yMaxList):
for x in range (xMin,xMax):
for y in range (yMin,yMax):
for i in range(len(xMinList)):
if xMinList[i] < x < xMaxList[i] and yMinList[i] < y < yMaxList[i]:
return False
#if the new rectangle isn't overlapping, append its 4 corner into the list for future comparison:
xMinList.append(xMin)
xMaxList.append(xMax)
yMinList.append(yMin)
yMaxList.append(yMax)
return xMinList,xMaxList,yMinList,yMaxList
def main():
win = GraphWin("Flash Card", 800,800)
xCenter, yCenter = randrange (200,600), randrange (200,600) #display the words from the text in randomly generated locations
xMin = xCenter - 50
xMax = xCenter + 50
yMin = yCenter - 50
yMax = yCenter + 50
xMinList = [300,500,200,100,600] #I hard coded these 4 lists for the purpose of testing
xMaxList = [350,580,220,140,650]
yMinList = [100,500,300,600,400]
yMaxList = [160,540,325,680,450]
#while checkLocation is False (location overlapping), check again until it's True (not overlapping)
while not checkLocation(xMin,xMax,yMin,yMax,xMinList,xMaxList,yMinList,yMaxList):
checkLocation(xMin,xMax,yMin,yMax,xMinList,xMaxList,yMinList,yMaxList)
xMinList, xMaxList,yMinList,yMaxList = checkLocation(xMin,xMax,yMin,yMax,xMinList,xMaxList,yMinList,yMaxList)
rect = Rectangle (Point(xMin,yMin),Point(xMax,yMax))
rect.draw(win)
main()
Most of your issues stem from your definition and use of the function checkLocation()
: sometimes it returns a boolean, sometimes a tuple of lists; you don't seem to be aware that it updates the lists in question so they don't need to be returned and reassigned; one time you seem to call it for no reason at all.
I've reworked your code below to draw ten flash cards that don't overlap -- it's mostly the same code just put into a more logical order:
from graphics import *
from random import randrange
def checkLocation(xMin, xMax, yMin, yMax, xMinList, xMaxList, yMinList, yMaxList):
for x in range(xMin, xMax):
for y in range(yMin, yMax):
for i in range(len(xMinList)):
if xMinList[i] < x < xMaxList[i] and yMinList[i] < y < yMaxList[i]:
return False
# if the new rectangle isn't overlapping, append its 4 corner into the list for future comparison:
xMinList.append(xMin)
xMaxList.append(xMax)
yMinList.append(yMin)
yMaxList.append(yMax)
return True
def main():
win = GraphWin("Flash Card", 800, 800)
xMinList = []
xMaxList = []
yMinList = []
yMaxList = []
for _ in range(10):
xCenter, yCenter = randrange(200, 600), randrange(200, 600)
xMin = xCenter - 50
xMax = xCenter + 50
yMin = yCenter - 50
yMax = yCenter + 50
# while checkLocation is False (location overlapping), check again until it's True (not overlapping)
while not checkLocation(xMin, xMax, yMin, yMax, xMinList, xMaxList, yMinList, yMaxList):
# display the words from the text in randomly generated locations
xCenter, yCenter = randrange(200, 600), randrange(200, 600)
xMin = xCenter - 50
xMax = xCenter + 50
yMin = yCenter - 50
yMax = yCenter + 50
rect = Rectangle(Point(xMin, yMin), Point(xMax, yMax))
rect.draw(win)
win.getMouse() # Pause to view result
win.close() # Close window when done
main()
Initially, your code didn't run at all due to this incorrect import
:
from random import randrange *