java 计算线的方位角和两条直线的交点

1、计算两点方位角或者是直线的方位角

    public static double GetAzimuth(double dN1, double dE1, double dN2, double dE2) {
        double dAzimuth = 0;

        dAzimuth = Math.atan2(dE2 - dE1, dN2 - dN1) * 180 / Math.PI;
        if (dAzimuth < 0)
            dAzimuth += 360;

        return dAzimuth;
    }

2、计算两条直线的交点

    //交叉
    private static double xmult(double xa, double ya, double xb, double yb,
                                double x0, double y0) {
        return (xa - x0) * (yb - y0) - (xb - x0) * (ya - y0);
    }

    public static boolean GetPointInLine(double xa, double ya, double xb, double yb,
                                         double xc, double yc) {
        if (Math.abs(xmult(xa, ya, xb, yb, xc, yc)) < 1E-4 &&
                (xa - xc) * (xb - xc) < 1E-4 && (ya - yc) * (yb - yc) < 1E-4) {
            return true;
        }
        return false;
    }

    /// 计算两直线的交点
    public static boolean GetLineJoinPoint(double xa, double ya, double xb, double yb,
                                           double xc, double yc, double xd, double yd,
                                           double[] x, double[] y, boolean bBeeline) {
        //两线平行
        if (Math.abs((xa - xb) * (yc - yd) - (xc - xd) * (ya - yb)) < 1E-4) {
            if (Math.abs(xa - xc) + Math.abs(ya - yc) < 1E-4 ||
                    Math.abs(xa - xd) + Math.abs(ya - yd) < 1E-4) {
                x[0] = xa;
                y[0] = ya;
                return true;
            } else if (Math.abs(xb - xc) + Math.abs(yb - yc) < 1E-4 ||
                    Math.abs(xb - xd) + Math.abs(yb - yd) < 1E-4) {
                x[0] = xb;
                y[0] = yb;
                return true;
            }
            return false;
        }

        //线段无交点(不在同一侧)
        if (!bBeeline &&
                (xmult(xa, ya, xb, yb, xc, yc) * xmult(xa, ya, xb, yb, xd, yd) > 1E-4 ||
                        xmult(xc, yc, xd, yd, xa, ya) * xmult(xc, yc, xd, yd, xb, yb) > 1E-4)) {
            return false;
        }

        y[0] = ((xa - xb) * (yc - yd) * ya - (ya - yb) * (yc - yd) * xa - (xc - xd) * (ya - yb) * yc + (ya - yb) * (yc - yd) * xc) / ((xa - xb) * (yc - yd) - (xc - xd) * (ya - yb));

        if (Math.abs(ya - yb) < 1E-4) {
            x[0] = xc + (y[0] - yc) * (xc - xd) / (yc - yd);
        } else {
            x[0] = xa + (y[0] - ya) * (xa - xb) / (ya - yb);
        }

        return true;
    }

 

你可能感兴趣的:(gis(java))