Exemplo n.º 1
0
    def flock(self, showing_flockmates):
        # NetLogo allows one to specify the units within the Gui widget.
        # Here we do it explicitly by multiplying by BLOCK_SPACING().
        vision_limit_in_pixels = gui_get('vision') * BLOCK_SPACING()

        flockmates = self.agents_in_radius(vision_limit_in_pixels)

        if len(flockmates) > 0:

            # If showing_flockmates, create links to flockmates if they don't already exist.
            if showing_flockmates:
                for flockmate in flockmates:
                    # Don't make a link if it already exists.
                    if not link_exists(self, flockmate):
                        Link(self, flockmate, color=Color('skyblue3'))

            nearest_neighbor = min(
                flockmates, key=lambda flockmate: self.distance_to(flockmate))

            min_separation = gui_get('minimum separation') * BLOCK_SPACING()
            if self.distance_to(nearest_neighbor) < min_separation:
                self.separate(nearest_neighbor)
            else:
                self.align(flockmates)
                self.cohere(flockmates)
Exemplo n.º 2
0
    def flock(self):
        # NetLogo allows one to specify the units within the Gui widget.
        # Here we do it explicitly by multiplying by BLOCK_SPACING().
        vision_limit_in_pixels = self.get_gui_value('vision') * BLOCK_SPACING()
        flockmates = self.agents_in_radius(vision_limit_in_pixels)
        if len(flockmates) > 0:
            nearest_neighbor = min(
                flockmates, key=lambda flockmate: self.distance_to(flockmate))

            min_separation = self.get_gui_value(
                'minimum separation') * BLOCK_SPACING()
            if self.distance_to(nearest_neighbor) < min_separation:
                self.separate(nearest_neighbor)
            else:
                self.align(flockmates)
                self.cohere(flockmates)
Exemplo n.º 3
0
 def update_velocity(self):
     velocity = self.velocity
     influence_radius = gui_get('Influence radius')
     neighbors = self.agents_in_radius(influence_radius * BLOCK_SPACING())
     for neighbor in neighbors:
         force = Agent.forces_cache.get((neighbor, self), None)
         if force is None:
             force = force_as_dxdy(self.center_pixel, neighbor.center_pixel)
             Agent.forces_cache[(neighbor, self)] = force * (-1)
         velocity = velocity + force
     speed_factor = gui_get('Speed factor')
     self.set_velocity(normalize_dxdy(velocity, 1.5 * speed_factor / 100))
Exemplo n.º 4
0
 def draw(self, shape_name=None):
     super().draw(shape_name=shape_name)
     if self.highlighted:
         radius = round((BLOCK_SPACING() / 2) * self.scale * 1.5)
         circle(gui.SCREEN, Color('red'), self.rect.center, radius, 1)