1,背景:某配送需求,以门店未中心,计算两个订单之间的角度是否在范围之内的,范围之内的可以并单,不在范围之内,不做并单处理。为了模拟角度问题,现将位置以城市做演示,计算三个地方的夹角。
2,位置和代码如下:
可以清晰的看见,以上海为定点,北京和石家庄之间的夹角为14°
以天津为定点,北京和石家庄之间的夹角为63°
3,具体的代码:
private int getDegree(double vertexPointX, double vertexPointY, double point0X, double point0Y, double point1X, double point1Y) {
//向量的点乘
double vector = (point0X - vertexPointX) * (point1X - vertexPointX) + (point0Y - vertexPointY) * (point1Y - vertexPointY);
//向量的模乘
double sqrt = Math.sqrt(
(Math.abs((point0X - vertexPointX) * (point0X - vertexPointX)) + Math.abs((point0Y - vertexPointY) * (point0Y - vertexPointY)))
* (Math.abs((point1X - vertexPointX) * (point1X - vertexPointX)) + Math.abs((point1Y - vertexPointY) * (point1Y - vertexPointY)))
);
//反余弦计算弧度
double radian = Math.acos(vector / sqrt);
//弧度转角度制
return (int) (180 * radian / Math.PI);
}
参考文献:https://www.jianshu.com/p/2bee956fc6fc