def randomlyGenerate(self): """ Randomly generates a graph with the number with max number of shop where all shops are connected to their closest shops with no overlapping lines """ self.shopDict = {} orderedShopDict = {} """ First Generate all the shop and make sure the distance between them in large enough to be viewable """ for i in range(self.maxShops): newShop = True while newShop: p = Point([ random.random() * Globals.RESOLUTION[0], random.random() * Globals.RESOLUTION[0] ]) newShop = False #Make sure intersections are distenced for shop in self.shopDict: if abs(shop.getX() - p.getX()) < Globals.RESOLUTION[ 0] / self.maxShops / 2 or abs(shop.getY() - p.getY( )) < Globals.RESOLUTION[0] / self.maxShops / 2: newShop = True self.shopDict[Shop(p)] = [] """ Then order all the points in order of distance """ for shop in self.shopDict: orderedShopDict[shop] = self.orderDistance(shop, self.shopDict) """ Then connect the points to their closest points without overlapping lines and store these connections """ shouldBreak = False while not shouldBreak: shouldBreak = True for shop in orderedShopDict: if orderedShopDict[shop]: otherShop = orderedShopDict[shop][0] if otherShop not in self.shopDict[ shop] and shop not in self.shopDict[otherShop]: if self.notIntersect( [shop.getPos(), otherShop.getPos()]): self.shopDict[shop].append(otherShop) self.shopDict[otherShop].append(shop) shouldBreak = False del orderedShopDict[shop][0] """ Then order those points into order of distance for better searching """ for shop in self.shopDict: self.shopDict[shop] = self.orderDistance(shop, self.shopDict[shop], take=0) """ Finally generate the roads based on those connections """ self.roads = [] alreadyDone = [] for shop in self.shopDict: for otherShop in self.shopDict[shop]: if (otherShop, shop) not in alreadyDone: self.roads.append( Road(start=shop.getPos(), end=otherShop.getPos())) alreadyDone.append((shop, otherShop))