jeudi 3 décembre 2015

Comparing array members with nest loop,counting how many times the condition(if) was met

I'm comparing three different arrays , in my case , my program is checking with how many other circles one particular circle intersects. My function that does that :

void Intersection(int n, int A[], int B[], int C[]) 
{
ofstream out(rez);
   for (int i = 0; i < n; i++)
     {
       times_condition_met=0;
         for (int j = 0; j < n; j++)
         {
             if (Distance(X, Y, i, j) < (Radius[i] + Radius[j]))
             {
                 times_condition_met++;
             } 
         }
     }
}

I've my coordinates of circles centers and radii in three different arrays.

 X[] Y[] and R[]

This is the condition that has to be met

if (Distance(X, Y, i, j) < (Radius[i] + Radius[j]))
            times_condition_met++;

The function Distance finds the distance between two points (between centers of circles).

The problem I've met when counting with how many other circles does one particular circle intersect with is that when my function is checking if the condition is met e.g. if Circle1 intersects with Circle2 times_condition_met++ and then some time later loop checks if Circle2 intersects with Circle1 it does times_condition_met++ again. So what kind of if I should use so Circle1 intersects with Circle2 and Circle2 intersects with Circle1 would be counted as the condition was met only one time instead of two?

Aucun commentaire:

Enregistrer un commentaire