我有一个矩阵,看起来是这样的:

0 0 1
0 0 2
0 2 1.5

这意味着圆的(X,Y,R)(首先是圆的中心,其次是半径)
这个矩阵是动态创建的,所以它可以有你想要的所有大小,只要输入它的一个。
我需要找出这些圆是否相交,我是这样做的:
 for( int i = 0; i < n; i++ ) {
    dis_x = fabs(*(matrix + (i+1) * 3 + 0) - *(matrix + i * 3 + 0));
    dis_y = fabs(*(matrix + (i+1) * 3 + 1) - *(matrix + i * 3 + 1));
    hypotenuse = sqrt( pow(dis_x,2) + pow(dis_y,2) );

    radius_i = *(matrix + i * 3 + 2);
    radius_i_plus_1 = *(matrix + (i+1) * 3 + 2);

    sum_of_radius = radius_i_plus_1 + radius_i;

    if( sum_of_radius >= hypotenuse && hypotenuse != 0) {
        *(array_of_numbers_of_circles + i * 2 + 0) = i + 1;
        *(array_of_numbers_of_circles + i * 2 + 1) = i + 2;
    }

}

如果我举上面的例子,我会看到:
2 3个
但我需要:
1 3
2 3

换句话说,我需要比较所有元素,即使是第一个和最后一个(如果大小是3)以及所有元素:第二个和第四个(如果大小是4+)
提前谢谢你!

最佳答案

count = 0;
for( int i = 0; i < n-1; i++ ) {
  for( int j= i + 1;  j < n ; j++) {
    dis_x = *(matrix + j * 3 + 0) - *(matrix + i * 3 + 0); // no need for fabs
    dis_y = *(matrix + j * 3 + 1) - *(matrix + i * 3 + 1);

    // use squared form, use multiplication rather than pow
    hypSquared = dis_x * dis_x + dis_y * dis_y;

    radius_i = *(matrix + i * 3 + 2);
    radius_j = *(matrix + j * 3 + 2);

    sum_of_radius = radius_i + radius_j;
    diff_of_radius = radius_i - radius_j;
    // compare square of radius to the square of hyp. Eliminates call to sqrt()
    if( sum_of_radius * sum_of_radius >= hypSquared
     && diff_of_radius * diff_of_radius < hypSquared  ) {
        *(array_of_numbers_of_circles + count * 2 + 0) = i + 1;
        *(array_of_numbers_of_circles + count * 2 + 1) = j + 2;
        ++count;
    }

}

09-06 21:24