欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

已知A,B点,和C点,让C点在AB线段上移动

程序员文章站 2022-04-01 18:41:40
...
public static LatLong getBreakPoint_unit(LatLong wgsLatLng_first, LatLong wgsLatLng_last, LatLong break_point, double distance) {
    double A_lat = wgsLatLng_first.getLatitude();//获取A点纬度
    double A_lng = wgsLatLng_first.getLongitude();//获取A点经度
    double B_lat = wgsLatLng_last.getLatitude();//获取B点纬度
    double B_lng = wgsLatLng_last.getLongitude();//获取B点经度
    double C_lat = break_point.getLatitude();//获取C点纬度
    double C_lng = break_point.getLongitude();//获取C点经度

    double yA = MercatorProjection.latitudeToY(A_lat);//将A点纬度转化为平面坐标yA
    double xA = MercatorProjection.longitudeToX(A_lng);//将A点经度转化为平面坐标xA

    double yB = MercatorProjection.latitudeToY(B_lat);//将B点纬度转化为平面坐标yB
    double xB = MercatorProjection.longitudeToX(B_lng);//将B点经度转化为平面坐标xB
    double yC = MercatorProjection.latitudeToY(C_lat);//将C点纬度转化为平面坐标yC
    double xC = MercatorProjection.longitudeToX(C_lng);//将C点经度转化为平面坐标xC
    ArrayList<LatLong> latLongs = new ArrayList<>();
    latLongs.add(wgsLatLng_first);
    latLongs.add(wgsLatLng_last);
    double dis = IMapUtils.calculateLineDistance(wgsLatLng_first, wgsLatLng_last);//求出AB点的距离
    double breakpoint_distance = MercatorProjection.getMercatorDistance(latLongs, dis);//将距离米换算成平面距离
    double v = distance * (breakpoint_distance / dis);//求出单位距离

    Vector2D breakpoint_vector = new Vector2D(xB - xA, yB - yA);//求出AB向量
    Vector2D normalize = breakpoint_vector.normalize();//unit vector,求出AB单位向量
    Vector2D vector2D = normalize.multiply(1).multiply(v);//获取沿着AB平移的向量
    Vector2D c_point_vector = new Vector2D(xC, yC);//获取OC向量
    Vector2D destination_vector = c_point_vector.add(vector2D);//获取平移后的OC'向量
    double longitude = MercatorProjection.xToLongitude(destination_vector.x);//求出C'的经度
    double latitude = MercatorProjection.yToLatitude(destination_vector.y);//求出C'的纬度
    LatLong latLong = new LatLong(latitude, longitude);
    double dis_1 = IMapUtils.calculateLineDistance(wgsLatLng_first, latLong);
    double dis_2 = IMapUtils.calculateLineDistance(latLong, wgsLatLng_last);
    if (dis_1>dis||dis_2>dis){
        return null;//判断是否超出边界
    }
    return latLong;
}