示例#1
0
文件: PolyUtil.c 项目: npinto/Polygon
int poly_c_center(gpc_vertex_list *vl, double *cx, double *cy){
  gpc_vertex *v;
  double x=0.0, y=0.0, a;
  int i;
  for (i=0; i < vl->num_vertices-1; i++) {
    v = vl->vertex + i;
    a = v->x * (v+1)->y - (v+1)->x * v->y;
    x += (v->x + (v+1)->x) * a;
    y += (v->y + (v+1)->y) * a;
  }
  if (! poly_c_is_closed(vl)) {
    /* close the loop */
    v = vl->vertex + (vl->num_vertices-1);
    a = v->x * vl->vertex->y - vl->vertex->x * v->y;
    x += (v->x + vl->vertex->x) * a;
    y += (v->y + vl->vertex->y) * a;
  }
  /* this algorithm needs ccw ordered points,
     so we have to calculate the order and multiply,
     what a waste of cpu power... 
     If you know a better way, let me know... */
  a = 6.0*poly_c_area(vl)*poly_c_orientation(vl);
  if (a == 0)
      return 1;
  *cx = x / a;
  *cy = y / a;
  return 0;
}
示例#2
0
static PyObject *Polygon_orientation(Polygon *self, PyObject *args) {
    int i=INDEF;
    if (! PyArg_ParseTuple(args, "|i", &i))
        return Polygon_Raise(ERR_ARG);
    if (i!=INDEF) {
        if ((i >= 0) && (i < self->p->num_contours))
            return Py_BuildValue("i", poly_c_orientation(self->p->contour+i));
        else
            return Polygon_Raise(ERR_IND);
    } else {
        PyObject *OL;
        OL = PyTuple_New(self->p->num_contours);
        for (i = 0; i < self->p->num_contours; i++)
            PyTuple_SetItem(OL, i, PyFloat_FromDouble(poly_c_orientation(self->p->contour+i)));
        return Py_BuildValue("O", OL);
    }
}