1 | initial version |
Thank you very much!sorry I didn't get back you both but I'd forgotten I'd posted on here. Here is the answer I came up with. I for the life of me can't figure out why my formatting isn't working properly. So if anyone can help me out I'd appreciate it. The code works well, but I ended up making some case specific adjustments to make it more effective.
vector<vector<int>> RAPC(vector<rotatedrect> minRect, float radius) {
vector<vector<int>> clusters; // vector that will contain identify who belongs to which cluster vector<bool> touched(minRect.size(), false); // initializing vector that will identify whether or not a rectangle has been assigned to a cluster
// for every rect for (int i = 0; i < minRect.size()-1; i++) { // compare to every other rect for (int n = (i + 1); n < minRect.size(); n++) { // distance from rect i to rect n float distance = sqrt(pow((minRect[i].center.x - minRect[n].center.x), 2) + pow((minRect[i].center.y - minRect[n].center.y), 2));
//criteria to determine whether the two rectangles belong in the same cluster
if ( distance < radius)
{
// if neither rectangle has been assigned to a cluster yet
if (touched[i] == false && touched[n] == false) {
// create new cluster in clusters with i and n as members
clusters.emplace_back(initializer_list<int>{i, n});
touched[i] = true; // make note of i being placed
touched[n] = true; // make note of n being placed
}
// if i has already been assigned to a cluster
else if (touched[i] == true && touched[n] == false) {
int loc = findLoc(i, clusters); // find i in clusters
clusters[loc].emplace_back(n); // place n in that cluster
touched[n] = true; // make note of n being placed
}
// if n has already been assigned to a cluster
else if (touched[n] == true && touched[i] == false) {
int loc = findLoc(n, clusters); // find n in clusters
clusters[loc].emplace_back(i); // place n in that cluster
touched[i] = true; // make note of i being placed
}
// if both rectangles have already been assigned
else (touched[i] == true && touched[n] == true); {
int locI = findLoc(i, clusters); // search for location of i
int locN = findLoc(n, clusters); // search for location of n
// if both rectangles are already part of the same cluster, ignore
if (locI == locN) {
break;
}
// concatenate vector n with vector i
clusters[locI].insert(clusters[locI].end(), clusters[locN].begin(), clusters[locN].end());
// erase original location of vector n
clusters[locN].erase(clusters[locN].begin(), clusters[locN].end());
}
}
}
} return clusters;
// function used by RAPC mainly just to make code more concise
int findLoc(int numSearchedFor, vector<vector<int>> clusters) { bool searchComplete = false; int ans; // initializing function output ie the cluster ID where the value is found for (int i = 0; i < clusters.size(); i++) { for (int n = 0; n < clusters[i].size(); n++) { if (clusters[i][n] == numSearchedFor) { ans = i; searchComplete = true; break; } } if (searchComplete == true) { break; } } return ans; }