I have both a 2D array and an arrayList of 25 PVectors each. I am choosing one random item from the arrayList group and comparing the distances thereof to all the PVectors in the 2D array. The aim is to choose from the 2D array group the item that has the shortest Euclidian distance to the chosen PVector in the arrayList. One way would be to loop through and cast a set of 25 distances into an array, sort the array, extract the index thereof, convert the 2D array into a 1D array and match the minimum distance index with that of the 1D array indices to get the PVector I am looking for. It seems a long-winded way to solve the problem. Also, I have difficulties in understanding how to implement the Java compareTo from the Java Compare interface. I will need to create a new class in order to get this to work, which is ok, but I don’t quite get how to extract the min value thereof in the compareTo function.
Max = arrayOfDistances.indexOf(max(arrayOfDistances.toArray()));
What is ‘Max’ in this context? I tried instantiating it as an integer but still had an error; the part after the ‘=’ I get.
Function extract from my code:
void getClostestElement () {
int randIndex = (int)random(num*num); // num = 5
PVector randVec = (PVector) pointList.get(randIndex); // extract random element from arrayList
for (int i = 0; i < num; ++i) {
// loop through the 2D array to get distances between random element and 2D array elements
for (int j = 0; j < num; ++j) {
float distance = PVector.dist(vecPos2D[i][j], randVec);
for (int k = 0; k < arrayOfDistances.length; ++k) {
arrayOfDistances[k] = distance; // doesn't work: adds the same number 25 times
// rather than 25 consecutive numbers; need an if statement to catch 25 elements at a time
// not sure how to implement this unless there is a Java class that I can call on
// eg. Something like: if (distance.number Of Elements $ == 0 …..)
}
}
}
}