Example #1
0
def pickSubtractiveClusterCenters(imageMatrix, noClusters, height, width):
    clusterCentersPositions = []
    potentialMatrix = np.full((height, width), 0.)
    for i in range(height):
        for j in range(width):
            for k in range(height):
                for l in range(width):
                    potentialMatrix[i][j] += exp(
                        -1 * 4 * distanceBetweenPoints(
                            imageMatrix[i][j], imageMatrix[k][l], True) /
                        (15**2))
    for k in range(noClusters):
        maxPotentialHeight, maxPotentialWidth = np.unravel_index(
            potentialMatrix.argmax(), potentialMatrix.shape)
        maxPotential = potentialMatrix[maxPotentialHeight][maxPotentialWidth]
        clusterCentersPositions.append([maxPotentialHeight, maxPotentialWidth])
        potentialMatrix[maxPotentialHeight][maxPotentialWidth] = 0.
        for i in range(height):
            for j in range(width):
                if not [i, j] in clusterCentersPositions:
                    potentialMatrix[i][j] -= maxPotential * exp(
                        -1 * 4 * distanceBetweenPoints(
                            imageMatrix[i][j], imageMatrix[maxPotentialHeight]
                            [maxPotentialWidth], True) / (21**2))
    clusterCenters = []
    for i in range(len(clusterCentersPositions)):
        clusterCenters.append(imageMatrix[clusterCentersPositions[i][0]][
            clusterCentersPositions[i][1]])
    return clusterCenters
Example #2
0
    def flyToCoords(self, drone_loc, drone_alt, target_loc, target_alt):
        if not drone_loc['latitude']:
            self.drone.stop()
            return False

        distance = utils.distanceBetweenPoints(drone_loc, target_loc)
        altitude = target_alt - drone_alt
        if distance <= 3 and abs(altitude) <= 1:
            # Reached destination
            self.drone.stop()
            return True

        # Turn to correct bearing
        current_bearing = self.drone.NavData["demo"][2][2]
        current_bearing = (current_bearing + 360) % 360
        target_bearing = utils.bearingBetweenPoints(drone_loc, target_loc)
        angle = target_bearing - current_bearing
        if angle > 180:
            angle -= 360
        elif angle < -180:
            angle += 360

        if abs(angle) > 5:
            self.drone.turnAngle(angle, 0.5, 0.1)
            turn_speed = 0.0
        else:
            turn_speed = angle / 10.0

        # Move forward and up at proportional speed
        forward_speed = max(min(distance / 50.0, 0.1), 0.05)  # 0.05 < x < 0.25
        up_speed = max(min(altitude / 10.0, 0.5), -0.5)  # -0.5 < x < 0.5
        self.drone.move(0.0, forward_speed, up_speed, turn_speed)

        return False
Example #3
0
 def getFoodInside(self, food_machine):
     ball_position = self.getPosition()
     radius = self.getRadius()
     i = 0
     while i < len(food_machine.pieces_of_food):
         piece_position = [utils.calculatePygameValue(food_machine.pieces_of_food[i].position[0]), utils.calculatePygameValue(food_machine.pieces_of_food[i].position[1])]
         if piece_position[0] > 145 and utils.distanceBetweenPoints(ball_position, piece_position) < radius:
             self.counter = self.counter + 1
             self.has_food_inside = True
             food_machine.removePieceAt(i)
         else:
             i = i + 1
Example #4
0
    def getDistance(self):
        drone_loc = self.drone_state.state['location']
        app_loc = self.control_state.state['location']

        if not drone_loc['latitude'] or not app_loc['latitude']:
            return

        distance = {
            'distance': round(utils.distanceBetweenPoints(drone_loc, app_loc),
                              2)
        }
        self.drone_state.update_state(distance)
Example #5
0
def calculateclusterMatrix(imageMatrix, clusterCenters, noClusters, height,
                           width):
    clusterMatrix = np.full((height, width), -1)
    for i in range(height):
        for j in range(width):
            tempDistances = []
            for k in range(noClusters):
                tempDistances.append(
                    distanceBetweenPoints(imageMatrix[i][j], clusterCenters[k],
                                          True))
            clusterMatrix[i][j] = tempDistances.index(min(tempDistances))
    return clusterMatrix
Example #6
0
def pickFarthestClusterCenters(imageMatrix, noClusters, height, width):
    clusterCenters = []
    distanceMatrix = np.full((height, width), 0.)
    randomHeight = randint(0, height)
    randomWidth = randint(0, width)
    clusterCenters.append(imageMatrix[randomHeight][randomWidth])
    for i in range(height):
        for j in range(width):
            distanceMatrix[i][j] = distanceBetweenPoints(
                imageMatrix[i][j], imageMatrix[randomHeight][randomWidth],
                True)
    for k in range(noClusters - 1):
        maxDistanceHeight, maxDistanceWidth = np.unravel_index(
            distanceMatrix.argmax(), distanceMatrix.shape)
        clusterCenters.append(imageMatrix[maxDistanceHeight][maxDistanceWidth])
        for i in range(height):
            for j in range(width):
                distanceMatrix[i][j] = min(
                    distanceMatrix[i, j],
                    distanceBetweenPoints(
                        imageMatrix[i][j],
                        imageMatrix[maxDistanceHeight][maxDistanceWidth],
                        True))
    return clusterCenters
Example #7
0
        balloons[-1].myFixture = balloons[-1].body.CreateFixture(balloons[-1].fixtureDef)
        balloons[-1].body.active = True
        balloons[-1].getFoodInside(food_machine)
    elif is_balloon_growing and mouse_keys[0]:
        balloons[-1].shape.radius += utils.calculateBox2DValue(4)

    world_for_bubbles.Step(timeStep, 6, 2)
    world_for_bubbles.ClearForces()
    world_for_food.Step(timeStep, 6, 2)
    world_for_food.ClearForces()

    #balloons shrinking
    if mouse_keys[2] and not is_balloon_growing and not is_balloon_shrinking:
        shrinking_balloon_coordinates = pygame.mouse.get_pos()
        for i in range(len(balloons)):
            if utils.distanceBetweenPoints(balloons[i].getPosition(), shrinking_balloon_coordinates) < balloons[i].getRadius():
                shrinking_balloon = balloons[i]
                is_balloon_shrinking = True
    if mouse_keys[2] and is_balloon_shrinking and shrinking_balloon != None:
        shrinking_balloon.shape.radius -= utils.calculateBox2DValue(4)
        shrinking_balloon.reloadFixture()
        if(shrinking_balloon.getRadius() <= 1):
            shrinking_balloon.releaseAshes(food_machine)
            shrinking_balloon.destroyBody(world_for_bubbles)
            balloons.remove(shrinking_balloon)
            is_balloon_shrinking = False
            pop_sound.play()
    else:
        shrinking_balloon = None
    if not mouse_keys[2]:
        is_balloon_shrinking = False