void KMeansClustering::updateRadiusForCluster(size_t cluster_index) { double max_dist = 0; for (size_t p = 0; p < clusters[cluster_index].indices.size(); p++) { double dist = pointToPointDistance(clusters_centroids[cluster_index], (*cloud)[clusters[cluster_index].indices[p]]); if (dist > max_dist) { max_dist = dist; } } clusters_radii[cluster_index] = max_dist; }
void CGame::handleEnemyShots() { for (std::list<std::unique_ptr<CShell>>::iterator shell = enemyShells.begin(); shell != enemyShells.end(); shell++) { if ((*shell)->direction > 0) { //fighter shells are faster if ((*shell)->fast == true) (*shell)->box.x += 10; else (*shell)->box.x += 5; } if ((*shell)->direction < 0) (*shell)->box.x -= 5; //decrease distance needed for exploding shell to blow if ((*shell)->exploding) { (*shell)->distance -= 5; //stop moving if ((*shell)->distance < 0) (*shell)->direction = 0; } //if not exploded already and at distance needed for explosion if ((*shell)->doIExplode() && !(*shell)->exploded) { (*shell)->exploded = true; //explosion timer (*shell)->timer.Start(); int plane_radius = SDL_sqrt((PLANE_HEIGHT*PLANE_HEIGHT + PLANE_WIDTH*PLANE_WIDTH))/2; if (pointToPointDistance(plane.getBox().x + PLANE_WIDTH/2, plane.getBox().y +PLANE_HEIGHT/2, (*shell)->box.x + 2, (*shell)->box.y + 2) < ((*shell)->radius + plane_radius)) { //if distance less than sum of radi, then collision with plane plane.removeLife(); shell->reset(); enemyShells.erase(shell++); continue; } } //if shell goes out of screen if (((*shell)->box.x > SCREEN_WIDTH) || ((*shell)->box.x < 0)) { shell->reset(); enemyShells.erase(shell++); } } }
size_t KMeansClustering::findNearestClusterToPoint(size_t index) { double min_distance = 0.0; size_t nearest_cluster_index = SIZE_T_MAX; for (size_t k = 0; k < clusters.size(); k++) { double distance = pointToPointDistance((*cloud)[index], clusters_centroids[k]); if (k == 0) { min_distance = distance; nearest_cluster_index = 0; } else if (min_distance > distance) { min_distance = distance; nearest_cluster_index = k; } } return nearest_cluster_index; }
void KMeansClustering::filterClusters(double max_distance) { // Run through all the clusters and remove the points that // have a distance > max_distance away from their centroid. for (size_t c = 0; c < clusters.size(); c++) { for (size_t p = 0; p < clusters[c].indices.size(); p++) { double dist = pointToPointDistance(clusters_centroids[c], (*cloud)[clusters[c].indices[p]]); if (dist > max_distance) { // This point is too far away! Remove it from the cluster. size_t the_point = clusters[c].indices[p]; clusters[c].indices.erase(remove(clusters[c].indices.begin(), clusters[c].indices.end(), the_point), clusters[c].indices.end()); } } // Then recalculate the centroid and radius. updateCentroidForCluster(c); updateRadiusForCluster(c); } }