c++ - 如何跟踪扩大的圆半径

标签 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只存储圆面积的并集,没有交点信息。

最佳答案

无法声明具有可变大小 (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,并在 main() 中声明 D[]center[]。代码可以使用另一种方法来使用 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};

关于c++ - 如何跟踪扩大的圆半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33844576/

相关文章:

math - 预测球路 - 人工智能

c++ - 检测按键按下和按键释放事件

c++ - 如何使用库分发应用程序?

c++ - 如何要求 stringstream 不要在引号中拆分数据 (C++)

c++ - C动态成员结构

c - 依靠 strtok_r 的内部指针安全吗?

c# - 使用 "greater than or equals"或仅使用 "greater than"

Java:数学线设计

ubuntu - 无法在调试器内调用标准数学函数?

c++ - 使用 g++ 预处理器检测 size_t 是否内置