I seem to be blind, because I don´t find the logical mistake in this code.
That`s what I want to do:
Several particles (each an instance of the particle-class of traer-physics-library) are on the screen.
I choose one an try to find its next three neighbours.
Of course I could get all distances and sort them, but that`s to much work to get only three elements.
So my solution (not working correctly):
I dont bother about the particle being its own best neighbour - sort out later.
I take the first four elements, put them in an array nb[][] an then loop through the
other particles,if there are better neighbours. Therefor in each loop I have to find the "worst" neighbour under these four.
I only compare this one, if the next particle is "better". Than replace it and find the new maximum. And so on.
This works well sometimes, but sometimes I get an strange behaviour I don`t understand.
There are two "good" neighbours and one "wrong" neighbour. Here is the output I get:
Example with 12 particles:
console-output via prntln:
(the index of the particle and its distance to the choosen one)
nb 0 0: 0 nb 1 0 : 21.209055
nb 0 1: 1 nb 1 1 : 58.19002
nb 0 2: 2 nb 1 2 : 130.75015
nb 0 3: 3 nb 1 3 : 79.68597
maximum value:2
(starting with the first four particles and getting nr. 2 correctly as greatest value)
21.209055 58.19002 46.24385 79.68597
maximum value:3
(replacing correctly nr. 2 with a smaller value, now nr. 3 is maximum)
21.209055 58.19002 46.24385 79.68597
maximum value:3
(next particle more distant than these four, no change)
21.209055 58.19002 46.24385 79.68597
maximum value:3
(next particle more distant than these four, no change)
21.209055 58.19002 46.24385 47.40248
maximum value:1
(replacing correctly nr. 3 with a smaller value)
21.209055 58.19002 46.24385 47.40248
maximum value:1
(next particle more distant than these four, no change)
21.209055 0.0 46.24385 47.40248
maximum value:3
(replacing correctly nr. 1 with the element itself -distance zero)
21.209055 0.0 46.24385 64.40618
maximum value:3
(now thats odd - replacing nr. 3 with a greater value!)
21.209055 0.0 46.24385 64.40618(all 12 particles checked)
Here is my code:
Code:void nachbarn(int a)//------------------------nachbarn (finding the three nearest neighbours of a particle with index a)
{
if(a<0){// no object choosen
return;
}
float abb;//distance of the actually checked particle
int maxi;//index in nb[][] of the element with greatest distance
float[][] nb=new float[2][4];//first index 0 - objectnumber 1 - distance //second index: the four next neighbours including a itself
String dat1;
String titel="nachbarn"+str(a)+".txt";
String[] datlist=new String[5];//preparation for writing the neighbours to a file
for (int i=0;i<4;i++){//starting with the four first objects without regard of their distance
nb[0][i]=i;
nb[1][i]=abst(i,a);//
println("first four nb 0 "+i+": "+nb[0][i] +"temp nb 1 "+i+" : "+nb[1][i]);
}
if(anzahl>4){//anzahl=number of particles
for(int j=4;j<anzahl;j++){//if an object is nearer than the most distant object in the array nb, replace it
maxi=findmax(nb);//find the most distant object in nb[][]
println("maximum value:"+maxi+"");
println("");
abb=abst(j,a);
if(abb<abst(maxi,a)){
nb[0][maxi]=j;
nb[1][maxi]=abb;
}
println(nb[1][0]+" "+nb[1][1]+" "+nb[1][2]+" "+nb[1][3]);
}
//--now the output to file and console
//println("Nachbarn von "+a+": ");
// dat1="Nachbarn von "+str(a)+": ";
// datlist[0]=dat1;
if(int(nb[0][0])!=a){//ignore the particle itself as its own neighbour
//println(int(nb[0][0])+": "+round(nb[1][0]));
// dat1=str(int(nb[0][0]))+": "+str(round(nb[1][0]));
// datlist[1]=dat1;
}
if(int(nb[0][1])!=a){
//println(nb[0][1]+": "+round(nb[1][1]));
// dat1=str(int(nb[0][1]))+": "+str(round(nb[1][1]));
// datlist[2]=dat1;
}
if(int(nb[0][2])!=a){
//println(nb[0][2]+": "+round(nb[1][2]));
// dat1=str(int(nb[0][2]))+": "+str(round(nb[1][2]));
// datlist[3]=dat1;
}
if(int(nb[0][3])!=a){
//println(nb[0][3]+": "+round(nb[1][3]));
// dat1=str(int(nb[0][3]))+": "+str(round(nb[1][3]));
// datlist[4]=dat1;
}
// saveStrings(titel,datlist);
}
}
float abst(int i, int j){//-----------------------------------------------------------------------distance between two particles with index i and j
return dist(particles[i].position().x(),particles[i].position().y(),particles[j].position().x(),particles[j].position().y());
}
int findmax(float[][] nb)//----------------------------------------------------------------------------findmax
{
float maxi;
int ret_ind=0;
if(nb[1][0]<nb[1][1])
{
ret_ind=1;
}
if(nb[1][ret_ind]<nb[1][2])
{
ret_ind=2;
}
if(nb[1][ret_ind]<nb[1][3])
{
ret_ind=3;
}
return ret_ind;
}