Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何计算C语言中多边形的质心_C_Graphics_2d_Centroid - Fatal编程技术网

如何计算C语言中多边形的质心

如何计算C语言中多边形的质心,c,graphics,2d,centroid,C,Graphics,2d,Centroid,我试图围绕质心旋转多边形,但我对公式编码感到困惑 float x[32]; float y[32]; float cx=0; float cy=0; int j,n; printf("How many angles :") scanf("%d" ,&n); //get (x[j],y[j]); for (j=0; j<n; j++){ printf("x%d : ",j+1); scanf("%f" ,&x[j]); printf("y%d : ",j+1); scanf(

我试图围绕质心旋转多边形,但我对公式编码感到困惑

float x[32];
float y[32];
float cx=0;
float cy=0;
int j,n;
printf("How many angles :")
scanf("%d" ,&n);
//get (x[j],y[j]);
for (j=0; j<n; j++){
printf("x%d : ",j+1);
scanf("%f" ,&x[j]);
printf("y%d : ",j+1);
scanf(input, "%f" ,&y[j]);
}
//find a
//find cx
//find cy
printf("The centroid of the polygon is (%f,%f)",cx,cy);
float x[32];
浮动y[32];
浮动cx=0;
浮点数cy=0;
int j,n;
printf(“多少角度:)
scanf(“%d”和“&n”);
//get(x[j],y[j]);
对于(j=0;j,公式如下:

这可能有点误导,因为公式需要一个从(x0,y0)到(xn–1,yn–1)的顶点列表,但在求和中包含xn和yn的值。在那里,您需要做的是将列表的开头环绕;即(xn,yn)≡ (x0,y0)

下面是一些执行这些计算的代码:

float a, cx, cy, t;
int i, i1;

/* First calculate the polygon's signed area A */

a = 0.0;
i1 = 1;
for (i=0; i<n; i++) {
  a += x[i] * y[i1] - x[i1] * y[i];
  i1 = (i1 + 1) % n;
}
a *= 0.5;

/* Now calculate the centroid coordinates Cx and Cy */

cx = cy = 0.0;
i1 = 1;
for (i=0; i<n; i++) {
  t = x[i]*y[i1] - x[i1]*y[i];
  cx += (x[i]+x[i1]) * t;
  cy += (y[i]+y[i1]) * t;
  i1 = (i1 + 1) % n;
}
cx = cx / (6.0 * a);
cy = cy / (6.0 * a);
float a,cx,cy,t;
inti,i1;
/*首先计算多边形的有符号区域A*/
a=0.0;
i1=1;
对于(i=0;iSee)