java - 将由两点创建的区域扩大其距离的百分比
问题描述
我有两个创建一个盒子的地理点(每个点都是一个角)。我想创建一个稍大的盒子。以下代码段是创建框的代码。
private Box createRouteBox(DirectionsRoute route){
Box box;
Bounds bounds = route.bounds;
LatLng northeast = bounds.northeast;
LatLng southwest = bounds.southwest;
Point northeastPoint = new Point(northeast.lng, northeast.lat);
Point southwestPoint = new Point(southwest.lng, southwest.lat);
box = new Box(northeastPoint, southwestPoint);
return box;
}
你能给我一个如何前进的建议吗?
解决方案
尝试这样的事情
private Box expandedBox(Point northeastPoint, Point southwestPoint){
// percentage of distance to set as padding to the box
Double offset = 0.1;
Double distance = pointDistance(northeastPoint, southwestPoint);
Double padding = (distance * offset);
// get current x, y
Double neX = northeastPoint.getX();
Double neY = northeastPoint.getY();
Double swX = southwestPoint.getX();
Double swY = southwestPoint.getY();
// init new x, y
Double neX2, neY2, swX2, swY2;
if(neX > swX){
neX2 = neX + padding;
swX2 = swX - padding;
} else {
neX2 = neX - padding;
swX2 = swX + padding;
}
if(neY > swY){
neY2 = neY + padding;
swY2 = swY - padding;
} else {
neY2 = neY - padding;
swY2 = swY + padding;
}
northeastPoint = new Point(neX2, neY2);
southwestPoint = new Point(swX2, swY2);
return new Box(northeastPoint, southwestPoint);
}
private Double pointDistance(Point p1, Point p2) {
return Math.sqrt((p2.getY() - p1.getY()) * (p2.getY() - p1.getY()) + (p2.getX() - p1.getX()) * (p2.getX() - p1.getX()));
}
您可以将偏移更改为所需距离的百分比。
推荐阅读
- reactjs - React 路由器 V5 可选路径
- json - jq:解析错误:第 1 行第 14 列的数字文字无效
- sql - 从同一表 SQL Server 中的日期列更新月份列
- javascript - 如何在动画后将元素移回原点
- javascript - 期望字符串(用于内置组件)或类/函数(用于复合组件)但未定义
- python - 为什么在使用 pandas 读取 csv 时会自动删除前面的引号?
- c# - 我可以异步运行同步代码以获得性能吗?
- php - 将响应 curl 函数从字符串转换为对象或数组 PHP
- ios - 麦克风和扬声器播放时按钮无响应
- python - Python 和 Matlab 的结果略有不同