求输入线段与其相交管线的所有交点集合代码.

   查询与输入线段相交的所有管线
  



 /**
     * 求与输入线相交管线的交点集合
     * @param wkt
     * @return
     */
    public List getIntersectResByPolyline(String wkt) {
        Polyline polyline = (Polyline) GeometryEngine.geometryFromWkt(wkt, WktExportFlags.wktExportDefaults, Geometry.Type.Polyline);
        List intersectPipelines = completionRateAnalysisMapper.getPipelinesByWkt(wkt);
        List resList=new ArrayList<>();
        for (int i = 0; i < intersectPipelines.size(); i++) {
            Polyline intersectPipeline = (Polyline) GeometryEngine.geometryFromWkt(intersectPipelines.get(i), WktExportFlags.wktExportDefaults, Geometry.Type.Polyline);
            List interPoints= intersectLinesToPoints( polyline.getCoordinates2D(),intersectPipeline.getCoordinates2D());
            resList.addAll(interPoints);
        }
        return resList;
    }

    /**
     *计算两条多线段之间的交点集合
     * @param polyline
     * @param interPolyline
     * @return
     */
    public  List intersectLinesToPoints(Point2D[] polyline, Point2D[] interPolyline) {
       List result=new ArrayList<>();
        for (int j = 0; j < polyline.length - 1; j++) {
            Point2D[] tempLine = {polyline[j], polyline[j + 1]};
            for (int i = 0; i < interPolyline.length - 1; i++) {
                Point2D[] interLine = {interPolyline[i], interPolyline[i + 1]};
                double[] point=getPolyLinesIntersection(tempLine,interLine);
                if(point!=null){
                    result.add(point);
                }
            }
        }
        if (result.size()>0){
            return  result;
        }
        else {
           return  null;
        }
    }


    /**
     * 计算两个线段间的交点
     * @param line 线段1
     * @param interLine 线段2
     * @return
     */
    public double[] getPolyLinesIntersection(Point2D[] line, Point2D[] interLine) {
        double[] result;
        double line_start_X = line[0].x;
        double line_start_Y = line[0].y;
        double line_end_X = line[1].x;
        double line_end_Y = line[1].y;
        double interLine_start_X = interLine[0].x;
        double interLine_start_Y = interLine[0].y;
        double interLine_end_X = interLine[1].x;
        double interLine_end_Y = interLine[1].y;

        /** 1 解线性方程组, 求线段交点. **/
        // 如果分母为0 则平行或共线, 不相交
        double denominator = (line_end_Y - line_start_Y) * (interLine_end_X - interLine_start_X) - (line_start_X - line_end_X) * (interLine_start_Y - interLine_end_Y);
        if (Math.abs(denominator) < 1e-20) {
            return  null;
        }
        // 线段所在直线的交点坐标 (x , y)
        double x = ((line_end_X - line_start_X) * (interLine_end_X - interLine_start_X) * (interLine_start_Y - line_start_Y)
                + (line_end_Y - line_start_Y) * (interLine_end_X - interLine_start_X) * line_start_X
                - (interLine_end_Y - interLine_start_Y) * (line_end_X - line_start_X) * interLine_start_X) / denominator;
        double y = -((line_end_Y - line_start_Y) * (interLine_end_Y - interLine_start_Y) * (interLine_start_X - line_start_X)
                + (line_end_X - line_start_X) * (interLine_end_Y - interLine_start_Y) * line_start_Y
                - (interLine_end_X - interLine_start_X) * (line_end_Y - line_start_Y) * interLine_start_Y) / denominator;
        /** 2 判断交点是否在两条线段上 **/
        double valid_line_x = (x - line_start_X) * (x - line_end_X);
        double valid_line_y = (y - line_start_Y) * (y - line_end_Y);
        double valid_interLine_x = (x - interLine_start_X) * (x - interLine_end_X);
        double valid_interLine_y = (y - interLine_start_Y) * (y - interLine_end_Y);
        if ((valid_line_x <= 0) && (valid_line_y <= 0) && (valid_interLine_x <= 0) && (valid_interLine_y <= 0)) {
            result = new double[]{x, y};
            return result;
        } else {
            return null;
        }
    }

你可能感兴趣的:(windows)