📄 centroid.c
字号:
/* * ANSI C code from the article * "Centroid of a Polygon" * by Gerard Bashein and Paul R. Detmer, (gb@locke.hs.washington.edu, pdetmer@u.washington.edu) * in "Graphics Gems IV", Academic Press, 1994 *//*********************************************************************polyCentroid: Calculates the centroid (xCentroid, yCentroid) and areaof a polygon, given its vertices (x[0], y[0]) ... (x[n-1], y[n-1]). Itis assumed that the contour is closed, i.e., that the vertex following(x[n-1], y[n-1]) is (x[0], y[0]). The algebraic sign of the area ispositive for counterclockwise ordering of vertices in x-y plane;otherwise negative.Returned values: 0 for normal execution; 1 if the polygon isdegenerate (number of vertices < 3); and 2 if area = 0 (and thecentroid is undefined).**********************************************************************/int polyCentroid(double x[], double y[], int n, double *xCentroid, double *yCentroid, double *area) { register int i, j; double ai, atmp = 0, xtmp = 0, ytmp = 0; if (n < 3) return 1; for (i = n-1, j = 0; j < n; i = j, j++) { ai = x[i] * y[j] - x[j] * y[i]; atmp += ai; xtmp += (x[j] + x[i]) * ai; ytmp += (y[j] + y[i]) * ai; } *area = atmp / 2; if (atmp != 0) { *xCentroid = xtmp / (3 * atmp); *yCentroid = ytmp / (3 * atmp); return 0; } return 2; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -