int poly_p_center(gpc_polygon *p, double *cx, double *cy){ double *x, *y, *a, A=0.0, X=0.0, Y=0.0; int i; size_t s; s = sizeof(double)*p->num_contours; a = (double *)alloca(s); x = (double *)alloca(s); y = (double *)alloca(s); for (i=0; i < p->num_contours; i++) { a[i] = ((p->hole[i]) ? -1.0 : 1.0) * poly_c_area(p->contour+i); if (poly_c_center(p->contour+i, x+i, y+i) != 0) return 1; } for (i=0; i < p->num_contours; i++) A += a[i]; for (i=0; i < p->num_contours; i++) { X += a[i] * x[i]; Y += a[i] * y[i]; } if (A == 0) return 1; *cx = X / A; *cy = Y / A; return 0; }
static PyObject *Polygon_center(Polygon *self, PyObject *args) { int i=INDEF; double cx, cy; if (! PyArg_ParseTuple(args, "|i", &i)) return Polygon_Raise(ERR_ARG); if (i!=INDEF) { if ((i >= 0) && (i < self->p->num_contours)) { if (poly_c_center(self->p->contour+i, &cx, &cy) !=0) return Polygon_Raise(ERR_INV); } else return Polygon_Raise(ERR_IND); } else { if (poly_p_center(self->p, &cx, &cy) != 0) return Polygon_Raise(ERR_INV); } return Py_BuildValue("dd", cx, cy); }