[英]How to track expanding circle radius
I'm working on a numerical simulation program, for simplicity I recreated the code into expanding circles on a domain bounded on each side. 我正在开发一个数值模拟程序,为简单起见,我将代码重新创建为在每边有界的域上扩展圆。 I want to track each circle's radius.
我想跟踪每个圆的半径。 If I have this code:
如果我有此代码:
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");
}
}
for visualization and simplicity purpose, add these into global definition 为了可视化和简化目的,将它们添加到全局定义中
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));
}
}
How do I do that? 我怎么做?
edit : note that circles might overlap each other, array D only store the union of circle area without information of the intersections. 编辑 :请注意,圆可能会彼此重叠,数组D仅存储圆区域的并集,而没有交点的信息。
Cannot declare a global array with a variable size (VLA). 无法声明具有可变大小(VLA)的全局数组。 Use a compile time constant.
使用编译时间常数。
// 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};
Alternatively use C99, and declare D[]
and center[]
inside main()
. 或者使用C99,并在
main()
声明D[]
和center[]
。 Code could use another method to use data in eval()
like eval(N, D, nCircle, center, radius)
代码可以使用另一种方法在
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};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.