增加多边形中心,重心点计算

This commit is contained in:
wanggeng 2022-11-15 20:55:56 +08:00
parent 18424c2fd3
commit e4a0a497dc

View File

@ -19,6 +19,11 @@ public class PointUtil {
private static final double RADIAN = Math.PI / 180.00D;
private static final double HALF = 0.5D;
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;
/**
* 点在多边形内
*
@ -301,4 +306,97 @@ public class PointUtil {
return Math.abs(area);
}
/**
* 计算中心点
*
* @param mPoints
* @return
*/
public static Point getCenterPoint(List<Point> mPoints) {
double latitude = (getMinLatitude(mPoints) + getMaxLatitude(mPoints)) / 2;
double longitude = (getMinLongitude(mPoints) + getMaxLongitude(mPoints)) / 2;
return new Point(latitude, longitude);
}
/**
* 获取不规则多边形重心点
*
* @param mPoints
* @return
*/
public static Point getCenterOfGravityPoint(List<Point> mPoints) {
double area = 0.0;//多边形面积
double Gx = 0.0, Gy = 0.0;// 重心的xy
for (int i = 1; i <= mPoints.size(); i++) {
double iLat = mPoints.get(i % mPoints.size()).getX();
double iLng = mPoints.get(i % mPoints.size()).getY();
double nextLat = mPoints.get(i - 1).getX();
double nextLng = mPoints.get(i - 1).getY();
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 Point(Gx, Gy);
}
// 经度最小值
public static double getMinLongitude(List<Point> mPoints) {
double minLongitude = MAX_LNG;
if (mPoints.size() > 0) {
minLongitude = mPoints.get(0).getY();
for (Point latlng : mPoints) {
// 经度最小值
if (latlng.getY() < minLongitude)
minLongitude = latlng.getY();
}
}
return minLongitude;
}
// 经度最大值
public static double getMaxLongitude(List<Point> mPoints) {
double maxLongitude = MIN_LNG;
if (mPoints.size() > 0) {
maxLongitude = mPoints.get(0).getY();
for (Point latlng : mPoints) {
// 经度最大值
if (latlng.getY() > maxLongitude)
maxLongitude = latlng.getY();
}
}
return maxLongitude;
}
// 纬度最小值
public static double getMinLatitude(List<Point> mPoints) {
double minLatitude = MAX_LAT;
if (mPoints.size() > 0) {
minLatitude = mPoints.get(0).getX();
for (Point latlng : mPoints) {
// 纬度最小值
if (latlng.getX() < minLatitude)
minLatitude = latlng.getX();
}
}
return minLatitude;
}
// 纬度最大值
public static double getMaxLatitude(List<Point> mPoints) {
double maxLatitude = MIN_LAT;
if (mPoints.size() > 0) {
maxLatitude = mPoints.get(0).getX();
for (Point latlng : mPoints) {
// 纬度最大值
if (latlng.getX() > maxLatitude)
maxLatitude = latlng.getX();
}
}
return maxLatitude;
}
}