Exemple #1
0
static PyObject *Polygon_overlaps(Polygon *self, Polygon *other) {
    double x0, x1, y0, y1, X0, X1, Y0, Y1;
    gpc_polygon * pres;
    int r;
    if (! Polygon_Check(other))
        return Polygon_Raise(ERR_ARG);
    Polygon_getBoundingBox(self,  &x0, &x1, &y0, &y1);
    Polygon_getBoundingBox(other, &X0, &X1, &Y0, &Y1);
    /* first test if bounding box overlaps other boundingbox */
    if ((X0 > x1) || (x0 > X1) || (Y0 > y1) || (y0 > Y1))
        return Py_BuildValue("i",  0);
    /* still there? Let's do the full test... */
    if (! (pres = poly_p_new())) return Polygon_Raise(ERR_MEM);
    gpc_polygon_clip(GPC_INT, other->p, self->p, pres);
    r = pres->num_contours;
    gpc_free_polygon(pres);
    free(pres);
    return Py_BuildValue("i",  ((r > 0) ? 1 : 0));
}
Exemple #2
0
static PyObject *Polygon_flop(Polygon *self, PyObject *args) {
    double y = DNDEF;
    if (! PyArg_ParseTuple(args, "|d", &y))
        return Polygon_Raise(ERR_ARG);
    if (y == DNDEF) {
        double x0, x1, y0, y1;
        Polygon_getBoundingBox(self, &x0, &x1, &y0, &y1);
        y = 0.5 * (y0+y1);
    } else
        self->bbValid = 0;
    poly_p_flop(self->p, y);
    return Py_BuildValue("O", Py_None);
}
Exemple #3
0
static PyObject *Polygon_covers(Polygon *self, Polygon *other) {
    double x0, x1, y0, y1, X0, X1, Y0, Y1;
    gpc_polygon * pres;
    int r;
    if (! Polygon_Check(other))
        return Polygon_Raise(ERR_ARG);
    Polygon_getBoundingBox(self,  &x0, &x1, &y0, &y1);
    Polygon_getBoundingBox(other, &X0, &X1, &Y0, &Y1);
    /* first test if bounding box covers other boundingbox */ 
    if ((X0 < x0) || (X1 > x1) || (Y0 < y0) || (Y1 > y1))
        Py_RETURN_FALSE;
    /* still there? Let's do the full test... */  
    if (! (pres = poly_p_new())) return Polygon_Raise(ERR_MEM);
    gpc_polygon_clip(GPC_DIFF, other->gpc_p, self->gpc_p, pres);
    r = pres->num_contours;
    gpc_free_polygon(pres);
    free(pres);
    if (r > 0)
        Py_RETURN_FALSE;
    else
        Py_RETURN_TRUE;
}
Exemple #4
0
static PyObject *Polygon_flip(Polygon *self, PyObject *args) {
    double x = DNDEF;
    if (! PyArg_ParseTuple(args, "|d", &x))
        return Polygon_Raise(ERR_ARG);
    if (x == DNDEF) {
        double x0, x1, y0, y1;
        Polygon_getBoundingBox(self, &x0, &x1, &y0, &y1);
        x = 0.5 * (x0+x1);
    } else
        self->bbValid = 0;
    poly_p_flip(self->gpc_p, x);
    Py_RETURN_NONE;
}
Exemple #5
0
static PyObject *Polygon_boundingBox(Polygon *self, PyObject *args) {
    int i=INDEF;
    double x0, x1, y0, y1;
    if (! PyArg_ParseTuple(args, "|i", &i))
        return Polygon_Raise(ERR_ARG);
    if (i!=INDEF) {
        if ((i >= 0) && (i < self->p->num_contours))
            poly_c_boundingbox(self->p->contour+i, &x0, &x1, &y0, &y1);
        else
            return Polygon_Raise(ERR_IND);
    } else
        Polygon_getBoundingBox(self, &x0, &x1, &y0, &y1);
    return Py_BuildValue("dddd", x0, x1,y0,y1);
}
Exemple #6
0
static PyObject *Polygon_aspectRatio(Polygon *self, PyObject *args) {
    int i=INDEF;
    double x0, x1, y0, y1;
    if (! PyArg_ParseTuple(args, "|i", &i))
        return Polygon_Raise(ERR_ARG);
    if (i!=INDEF) {
        if ((i >= 0) && (i < self->gpc_p->num_contours))
            poly_c_boundingbox(self->gpc_p->contour+i, &x0, &x1, &y0, &y1);
        else
            return Polygon_Raise(ERR_IND);
    } else
        Polygon_getBoundingBox(self, &x0, &x1, &y0, &y1);
    return Py_BuildValue("d", ((x0 != x1) ? fabs((y1-y0)/(x1-x0)) : 0.0));
}
Exemple #7
0
static PyObject *Polygon_rotate(Polygon *self, PyObject *args) {
    double alpha, xc=DNDEF, yc=DNDEF;
    if (! PyArg_ParseTuple(args, "d|dd", &alpha, &xc, &yc))
        return Polygon_Raise(ERR_ARG);
    if (alpha != 0.0) {
        if (xc == DNDEF) {
            double x0, x1, y0, y1;
            Polygon_getBoundingBox(self, &x0, &x1, &y0, &y1);
            xc = 0.5 * (x0+x1);
            yc = 0.5 * (y0+y1);
        }
        poly_p_rotate(self->p, alpha, xc, yc);
    }
    self->bbValid = 0;
    return Py_BuildValue("O", Py_None);
}
Exemple #8
0
static PyObject *Polygon_scale(Polygon *self, PyObject *args) {
    double xs, ys, xc=DNDEF, yc=DNDEF;
    if (! PyArg_ParseTuple(args, "dd|dd", &xs, &ys, &xc, &yc))
        return Polygon_Raise(ERR_ARG);
    if ((xs != 1.0) || (ys != 1.0)) {
        if (xc == DNDEF) {
            double x0, x1, y0, y1;
            Polygon_getBoundingBox(self, &x0, &x1, &y0, &y1);
            xc = 0.5 * (x0+x1);
            yc = 0.5 * (y0+y1);
        }
        poly_p_scale(self->p, xs, ys, xc, yc);
    }
    self->bbValid = 0;
    return Py_BuildValue("O", Py_None);
}