double reg_calc_area_complex_polygon( regShape* shape ) { regRegion *temp; regShape *copy; double area; fprintf(stderr, "WARNING: Calculating area of a complex polygon "); fprintf(stderr,"using brute force method. This may take a long time.\n"); // Create a new region with just the polygon temp = regCreateRegion(NULL, NULL); copy = shape->copy(shape); // Analytic area calculations always computes the area of the interior // of the shape. copy->include = regInclude; regAddShape(temp, regAND, copy); // Calc the extent of the polygon then trim the bounds to fit within // the original region if available regCalcExtentPolygon(shape, temp->xregbounds, temp->yregbounds); if (shape->region) { reg_trim_extent(temp->xregbounds, temp->yregbounds, shape->region->xregbounds, shape->region->yregbounds, 0); } area = regComputePixellatedArea(temp, temp->xregbounds, temp->yregbounds, 1); // Free and return regFree(temp); return area; }
// Computes the bounding rectangle corners of the region w.r.t. the field // and places the coordinates into xpos and ypos. Returns 1 if the shape is // completely contained within the field. 0 if the coordinates had to be // trimmed. int regExtent(regRegion * region, double *fieldx, double *fieldy, double *xpos, double *ypos) { regShape *atShape; int retval = 1; int start; int cstart; int state = 0; // Shape extents double sxpos[2]; double sypos[2]; // Component extents double cxpos[2]; double cypos[2]; // The null region is taken to be the field if (!region) { xpos[0] = fieldx[0]; xpos[1] = fieldx[1]; ypos[0] = fieldy[0]; ypos[1] = fieldy[1]; return retval; } // Set arrays to 0 and start/cstart to 1 start = reg_zero_bounds(xpos, ypos); cstart = reg_zero_bounds(cxpos, cypos); atShape = region->shape; while (atShape != NULL) { do { reg_extent_shape(atShape, fieldx, fieldy, sxpos, sypos); reg_trim_extent(cxpos, cypos, sxpos, sypos, cstart); state = 1; cstart = 0; if (atShape->next == NULL) { state = 0; } else if (atShape->next->component != atShape->component) { state = 0; } atShape = atShape->next; } while (state); /* End of component */ reg_union_extent(xpos, ypos, cxpos, cypos, start); start = 0; cstart = reg_zero_bounds(cxpos, cypos); } /* Trim to field */ retval = reg_trim_extent(xpos, ypos, fieldx, fieldy, 0); return (retval); }