Calculate the area, center, and center of gravity of irregular polygons (calculate the center point of the map fence

Recent projects used: Add an icon to the center point of the irregular polygon. (eg: Blizzard occurs in the xx area, the Blizzard area is a polygon, add a Blizzard icon to the center of the polygon)

The previous design is to calculate the center point of the bounds of the rectangle bounds of the irregular polygon range. This is relatively simple, and is suitable for some circles, rectangles, and convex polygons. But there will be problems when encountering concave polygons, such as a crescent-shaped irregular polygon, the center point of the bounds falls outside the crescent. It's a little hard to accept.

After discussion, it was decided to change the center to the center of gravity.

code below,

Compute the center of an irregular polygon:

[java] view plain copy

public static final double MIN_LAT = -90;
public static final double MAX_LAT = 90;
public static final double MIN_LNG = -180;
public static final double MAX_LNG = 180;

/**

  • Get the geometric center point of an irregular polygon
  • @param mPoints
  • @return */
    public static LatLng getCenterPoint(List<LatLng> mPoints) {
    // 1 自己计算
    // double latitude = (getMinLatitude(mPoints) + getMaxLatitude(mPoints)) / 2;
    // double longitude = (getMinLongitude(mPoints) + getMaxLongitude(mPoints)) / 2;
    // return new LatLng(latitude, longitude);
    // 2 使用Google map API提供的方法(推荐)
    LatLngBounds.Builder boundsBuilder = LatLngBounds.builder();
    for (LatLng ll : mPoints)
    boundsBuilder.include(ll);
    return boundsBuilder.build().getCenter();
    }

// 经度最小值
public static double getMinLongitude(List<LatLng> mPoints) {
double minLongitude = MAX_LNG;
if (mPoints.size() > 0) {
minLongitude = mPoints.get(0).longitude;
for (LatLng latlng : mPoints) {
// 经度最小值
if (latlng.longitude < minLongitude)
minLongitude = latlng.longitude;
}
}
return minLongitude;
}

// 经度最大值
public static double getMaxLongitude(List<LatLng> mPoints) {
double maxLongitude = MIN_LNG;
if (mPoints.size() > 0) {
maxLongitude = mPoints.get(0).longitude;
for (LatLng latlng : mPoints) {
// 经度最大值
if (latlng.longitude > maxLongitude)
maxLongitude = latlng.longitude;
}
}
return maxLongitude;
}

// 纬度最小值
public static double getMinLatitude(List<LatLng> mPoints) {
double minLatitude = MAX_LAT;
if (mPoints.size() > 0) {
minLatitude = mPoints.get(0).latitude;
for (LatLng latlng : mPoints) {
// 纬度最小值
if (latlng.latitude < minLatitude)
minLatitude = latlng.latitude;
}
}
return minLatitude;
}

// 纬度最大值
public static double getMaxLatitude(List<LatLng> mPoints) {
double maxLatitude = MIN_LAT;
if (mPoints.size() > 0) {
maxLatitude = mPoints.get(0).latitude;
for (LatLng latlng : mPoints) {
// 纬度最大值
if (latlng.latitude > maxLatitude)
maxLatitude = latlng.latitude;
}
}
return maxLatitude;
}

Compute the center of gravity of an irregular polygon:

[java] view plain copy

/**

  • Get the center of gravity of an irregular polygon
  • @param mPoints
  • @return */
    public static LatLng getCenterOfGravityPoint(List<LatLng> mPoints) {
    double area = 0.0;//多边形面积
    double Gx = 0.0, Gy = 0.0;// 重心的x、y
    for (int i = 1; i <= mPoints.size(); i++) {
    double iLat = mPoints.get(i % mPoints.size()).latitude;
    double iLng = mPoints.get(i % mPoints.size()).longitude;
    double nextLat = mPoints.get(i - 1).latitude;
    double nextLng = mPoints.get(i - 1).longitude;
    double temp = (iLat * nextLng - iLng * nextLat) / 2.0;
    area += temp;
    Gx += temp * (iLat + nextLat) / 3.0;
    Gy += temp * (iLng + nextLng) / 3.0;
    }
    Gx = Gx / area;
    Gy = Gy / area;
    return new LatLng(Gx, Gy);
    }
    The LatLng class is a simple class that contains latitude and longitude points. You can create a class that contains x, y instead. [java] view plain copy

public final double latitude;
public final double longitude;

Through this picture, you can find the difference between the center and the center of gravity

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325155155&siteId=291194637