如何跟踪扩展圆半径

时间:2015-11-21 14:39:30

标签: c++ c math geometry

我正在研究一个数值模拟程序,为了简单起见,我将代码重新创建为在每边界定的域上扩展圆圈。我想跟踪每个圆的半径。如果我有这个代码:

int const N = 10;
int D[N+2][N+2]; //domain bounded on each side
int const nCircle = 4;
int center[nCircle][2] = {{1, 1}, {N, N}, {N, 1}, {1, N}};

void eval(); //function to expand circles

int main() {
    for (int n=0;n<5;n++) {
        eval();
        for (int y=1;y<=N;y++) {
            for (int x=1;x<=N;x++) {
                printf("%d ", D[x][y]);
            }
            printf("\n");
        }
        printf("\n");
    }
}

用于可视化和简化的目的, 将这些添加到全局定义

double radius[nCircle] = {2, 2, 2, 2}; //actually unknown, i want to track this

void eval() {
    double a;
    for (int z=0;z<nCircle;z++) {
        for (int y=1;y<=N;y++) {
            for (int x=1;x<=N;x++) {
                a = pow(x-center[z][0], 2) + pow(y-center[z][1], 2);
                if (a <= pow(radius[z], 2))
                    D[x][y] = 1;
            }
        }
        radius[z] += ((double) rand() / (RAND_MAX));
    }
}

我该怎么做?

修改: 请注意,圆圈可能相互重叠,数组D仅存储圆形区域的并集而没有交叉点的信息。

1 个答案:

答案 0 :(得分:1)

无法声明具有可变大小(VLA)的全局数组。使用编译时常量。

// int const N = 10;
#define N 10
int D[N+2][N+2];
// int const nCircle = 4;
#define nCircle  4
int center[nCircle][2] = {{1, 1}, {N, N}, {N, 1}, {1, N}};

double radius[nCircle] = {2, 2, 2, 2};

或者使用C99,并在D[]内声明center[]main()。代码可以使用其他方法来使用eval()中的数据,例如eval(N, D, nCircle, center, radius)

int main() {
  int const N = 10;
  int D[N+2][N+2] = {0};  // Initialize D
  int const nCircle = 4;
  int center[nCircle][2] = {{1, 1}, {N, N}, {N, 1}, {1, N}};
  double radius[nCircle] = {2, 2, 2, 2};