src/main/java/de/blau/android/util/Geometry.java
/**
     * Calculate the centroid of a List of Coordinates representing a polygon or a line
     * 
     * The polygon or way that is defined by the Nodes should be non-self-intersecting and for polygons the last Node
     * should be equal to the first (OSM semantics)
     * 
     * @param coords array of screen coordinates
     * @param close close way (non-OSM semantics)
     * @return screen coordinates of centroid, null if the way has problems and if the way has length or area zero
     *         return the coordinates of the first node
     */
Calculate the centroid of a List of Coordinates representing a polygon or a line

The polygon or way that is defined by the Nodes should be non-self-intersecting and for polygons the last Node
should be equal to the first (OSM semantics)

@param coords array of screen coordinates
@param close close way (non-OSM semantics)
@return screen coordinates of centroid, null if the way has problems and if the way has length or area zero
return the coordinates of the first node
Calculate the centroid of a List of Coordinates representing a polygon or a line The polygon or way that is defined by the Nodes should be non - self - intersecting and for polygons the last Node should be equal to the first ( OSM semantics ) @param coords array of screen coordinates @param close close way ( non - OSM semantics ) @return screen coordinates of centroid null if the way has problems and if the way has length or area zero return the coordinates of the first node
@Nullable
    public static Coordinates centroidXY(@NonNull final Coordinates[] coords, boolean close) {
        int vs = coords.length;
        double y = 0;
        double x = 0;
        double x1 = coords[0].x;
        double y1 = coords[0].y;
        if (close || coords[0].equals(coords[vs - 1])) { 
            double a = 0;
            for (int i = 0; i < (vs - 1); i++) {
                double x2 = coords[i + 1].x;
                double y2 = coords[i + 1].y;
                double d = x1 * y2 - x2 * y1;
                a += d;
                x += (x1 + x2) * d;
                y += (y1 + y2) * d;
                x1 = x2;
                y1 = y2;
            }
            if (close) {
                double d = x1 * coords[0].y - coords[0].x * y1;
                a += d;
                x += (x1 + coords[0].x) * d;
                y += (y1 + coords[0].y) * d;
            }
            if (Util.notZero(a)) {
                y = y / (3 * a); 
                x = x / (3 * a); 
                return new Coordinates(x, y);
            } else {
                return coords[0];
            }
        } else { 
            double l = 0;
            for (int i = 0; i < (vs - 1); i++) {
                double x2 = coords[i + 1].x;
                double y2 = coords[i + 1].y;
                double len = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
                l += len;
                x += len * (x1 + x2) / 2;
                y += len * (y1 + y2) / 2;
                x1 = x2;
                y1 = y2;
            }
            if (Util.notZero(l)) {
                y = y / l; 
                x = x / l; 
                return new Coordinates(x, y);
            } else {
                return coords[0];
            }
        }
    }
@ Nullable public static Coordinates centroidXY ( @ NonNull final Coordinates [ ] coords , boolean close ) { int vs = coords . length ; double y = 0 ; double x = 0 ; double x1 = coords [ 0 ] . x ; double y1 = coords [ 0 ] . y ; if ( close || coords [ 0 ] . equals ( coords [ vs - 1 ] ) ) { double a = 0 ; for ( int i = 0 ; i < ( vs - 1 ) ; i ++ ) { double x2 = coords [ i + 1 ] . x ; double y2 = coords [ i + 1 ] . y ; double d = x1 * y2 - x2 * y1 ; a += d ; x += ( x1 + x2 ) * d ; y += ( y1 + y2 ) * d ; x1 = x2 ; y1 = y2 ; } if ( close ) { double d = x1 * coords [ 0 ] . y - coords [ 0 ] . x * y1 ; a += d ; x += ( x1 + coords [ 0 ] . x ) * d ; y += ( y1 + coords [ 0 ] . y ) * d ; } if ( Util . notZero ( a ) ) { y = y / ( 3 * a ) ; x = x / ( 3 * a ) ; return new Coordinates ( x , y ) ; } else { return coords [ 0 ] ; } } else { double l = 0 ; for ( int i = 0 ; i < ( vs - 1 ) ; i ++ ) { double x2 = coords [ i + 1 ] . x ; double y2 = coords [ i + 1 ] . y ; double len = Math . sqrt ( ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) ) ; l += len ; x += len * ( x1 + x2 ) / 2 ; y += len * ( y1 + y2 ) / 2 ; x1 = x2 ; y1 = y2 ; } if ( Util . notZero ( l ) ) { y = y / l ; x = x / l ; return new Coordinates ( x , y ) ; } else { return coords [ 0 ] ; } } }
Calculate the centroid of a List of Coordinates representing a polygon or a line
Calculate the centroid of a List of Coordinates representing a polygon or a line
// closed as per OSM definition // see http://paulbourke.net/geometry/polygonmesh/ // NOSONAR nonZero tests for zero // NOSONAR nonZero tests for zero // // NOSONAR nonZero tests for zero // NOSONAR nonZero tests for zero