Ejemplo n.º 1
0
 def initialPosition(self, low=2, up=12):
     "Returns List of Objects. This list is a position, i.e, a pattern of circles inside a container"
     initGrid = self.initialGrid  # initial grid, only inside points
     #objects = []
     numObj = random.randint(low, up)
     while (len(self.objects) < numObj) and initGrid:
         p = random.choice(
             initGrid)  # select a point from grid, point is a tuple (x,y)
         idxp = initGrid.index(
             p)  # index of selected point in the list initGrid
         r = random.choice(self.radii)  # Choose randomly an object
         circle = Circle(*p, radius=r)  # Set circle in selected point p
         #if len(self.objects): # if there are at least two cirlcles in the list
         #flag = True
         while any(circle.overlapping(obj) for obj in self.objects):
             #flag = any(circle.overlapping(obj) for obj in self.objects)
             #if flag:
             p = random.choice(initGrid)  # select point
             circle.center = p  # assign another center
             idxp = initGrid.index(p)  # index of selected point
             initGrid.pop(idxp)  # remove from init list
         self.removeOutside()
         #if circle.is_outside(self.grid): # test if circle is outside
         #    circle.radius = min(self.radii) # try with minimum radius for verify. It is a point in the inside frontier
         self.objects.append(circle)
         #pts.append(initGrid.pop(idxp)) # remove from init list and put in list of points
         initGrid = set(initGrid) - set(self.grid.coverage(
             circle))  # forbidden points for next iteration
         initGrid = list(initGrid)  # update grid  points
     for obj, c in zip(self.objects, cycle(Circle.colors)):
         obj.color = c
     return self.objects
Ejemplo n.º 2
0
 def initialGrid(self):
     'Initial grid for selecting random initial points'
     coords_xInside = self.grid.xcoords[
         1:
         -1]  # only coordinates inside container, for this reason remove the first and last element
     coords_yInside = self.grid.ycoords[
         1:-1]  # only coordinates inside container
     Radius = min(self.radii)  # choose a minimum radii
     x, y = coords_xInside[0], coords_yInside[
         0]  # choose the very first point (corner point)
     circle = Circle(x, y, Radius)  # set a circle in a choosen point
     while circle.is_outside(self.grid):  # test if circle is outside
         coords_yInside = coords_yInside[1:-1]  # update list
         coords_xInside = coords_xInside[1:-1]  # update list
         x, y = coords_xInside[0], coords_yInside[
             0]  # choose the first point, this is a corner point
         circle.center = x, y  # set a circle in the new point
     return list(product(coords_xInside, coords_yInside))