我是靠谱客的博主 整齐洋葱,最近开发中收集的这篇文章主要介绍根据四个点坐标求两个直线的交叉点坐标(Java&JavaScript)JavaJavaScript,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Java

	//计算两条直线的交点
    //输入四个点,前两个点确定线段1,后两个点确定线段2
    //输出交点坐标
    public static void main(String args[]){
        double[] point1= new double[]{0,0};
        double[] point2 = new double[]{2,2};

        double[] point3 = new double[]{0,2};
        double[] point4 = new double[]{2,0};
        double[] node = cross(point1,point2,point3,point4);
        if(node==null)System.out.println(false);
        else System.out.println(node[0]+","+node[1]);
        HashSet<String> set = new HashSet<>();
        set.add(node[0]+":"+node[1]);
        for(String item:set){
            System.out.println(item);
        }
    }

    public static double[] cross(double[] point1,double[] point2,double[] point3,double[] point4){
        if((point2[1]-point1[1])*(point4[0]-point3[0])==(point4[1]-point3[1])*(point2[0]-point1[0]))return null;
        if(point2[0]-point1[0]==0){
            double k2 = (point4[1]-point3[1])/(point4[0]-point3[0]);
            double b2 = point3[1]-point3[0]*k2;
            double cross_x = point1[0];
            double cross_y = k2*cross_x+b2;
            return new double[]{cross_x,cross_y};
        }
        if(point4[0]-point3[0]==0){
            double k1 = (point2[1]-point1[1])/(point2[0]-point1[0]);
            double b1 = point1[1]-point1[0]*k1;
            double cross_x = point3[0];
            double cross_y = k1*cross_x+b1;
            return new double[]{cross_x,cross_y};
        }
        double k1 = (point2[1]-point1[1])/(point2[0]-point1[0]);
        double b1 = point1[1]-point1[0]*k1;
        double k2 = (point4[1]-point3[1])/(point4[0]-point3[0]);
        double b2 = point3[1]-point3[0]*k2;
        double cross_x = (b1-b2)/(k2-k1);
        double cross_y = k1*cross_x+b1;
        return new double[]{cross_x,cross_y};
    }

JavaScript

//四个坐标 两条直线 求交叉点
function segmentsIntr(a, b, c, d){
    /** 1 解线性方程组, 求线段交点. **/
// 如果分母为0 则平行或共线, 不相交
    var denominator = (b[1] - a[1])*(d[0] - c[0]) - (a[0] - b[0])*(c[1] - d[1]);
    if (denominator==0) {
        return false;
    }
// 线段所在直线的交点坐标 (x , y)
    var x = ( (b[0] - a[0]) * (d[0] - c[0]) * (c[1] - a[1])
        + (b[1] - a[1]) * (d[0] - c[0]) * a[0]
        - (d[1] - c[1]) * (b[0] - a[0]) * c[0] ) / denominator ;
    var y = -( (b[1] - a[1]) * (d[1] - c[1]) * (c[0] - a[0])
        + (b[0] - a[0]) * (d[1] - c[1]) * a[1]
        - (d[0] - c[0]) * (b[1] - a[1]) * c[1] ) / denominator;
    /** 2 判断交点是否在两条线段上 **/
    if (
        // 交点在线段1上
        (x - a[0]) * (x - b[0]) <= 0 && (y - a[1]) * (y - b[1]) <= 0
        // 且交点也在线段2上
        && (x - c[0]) * (x - d[0]) <= 0 && (y - c[1]) * (y - d[1]) <= 0
    ){
        // 返回交点p
        return [x,y]
    }
    //否则不相交
    return false
}

最后

以上就是整齐洋葱为你收集整理的根据四个点坐标求两个直线的交叉点坐标(Java&JavaScript)JavaJavaScript的全部内容,希望文章能够帮你解决根据四个点坐标求两个直线的交叉点坐标(Java&JavaScript)JavaJavaScript所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部