package com.dji.sample.wayline.plane;
|
|
import lombok.Data;
|
|
import java.util.List;
|
|
/**
|
* @Author AIX
|
* @Date 2024/7/11 11:12
|
* @Version 1.0
|
*/
|
@Data
|
public class BoundingRectangle {
|
|
public MapLatLng topLeft;
|
public MapLatLng topRight;
|
public MapLatLng bottomRight;
|
public MapLatLng bottomLeft;
|
|
// 构造器
|
// 构造函数,使用四个角点初始化外接矩形
|
public BoundingRectangle(MapLatLng topLeft, MapLatLng topRight, MapLatLng bottomRight, MapLatLng bottomLeft) {
|
this.topLeft = topLeft;
|
this.topRight = topRight;
|
this.bottomRight = bottomRight;
|
this.bottomLeft = bottomLeft;
|
}
|
|
/**
|
* 静态方法,根据多边形点列表计算外接矩形并返回BoundingRectangle实例
|
* @param polygon
|
* @return
|
*/
|
public static BoundingRectangle fromPolygon(List<double[]> polygon) {
|
double maxLat = Double.NEGATIVE_INFINITY;
|
double minLat = Double.POSITIVE_INFINITY;
|
double maxLon = Double.NEGATIVE_INFINITY;
|
double minLon = Double.POSITIVE_INFINITY;
|
|
// 遍历多边形点以找到边界
|
for (double[] point : polygon) {
|
double lat = point[1]; // 假设每个点的数组是先经度[0]后纬度[1]
|
double lon = point[0];
|
|
if (lat > maxLat) {
|
maxLat = lat;
|
}
|
if (lat < minLat) {
|
minLat = lat;
|
}
|
if (lon > maxLon) {
|
maxLon = lon;
|
}
|
if (lon < minLon) {
|
minLon = lon;
|
}
|
}
|
|
// 创建外接矩形的四个角点
|
MapLatLng topLeft = new MapLatLng(maxLat, minLon);
|
MapLatLng topRight = new MapLatLng(maxLat, maxLon);
|
MapLatLng bottomRight = new MapLatLng(minLat, maxLon);
|
MapLatLng bottomLeft = new MapLatLng(minLat, minLon);
|
|
// 返回BoundingRectangle实例
|
return new BoundingRectangle(topLeft, topRight, bottomRight, bottomLeft);
|
}
|
|
}
|