博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
判断两条线段是否相交
阅读量:6598 次
发布时间:2019-06-24

本文共 2721 字,大约阅读时间需要 9 分钟。

之前一篇文章里写了一种差乘判断方法:

虽然用3D空间的差乘,但是只适用于2D空间

 

bool IsIntersection(Vector3 a, Vector3 b, Vector3 c, Vector3 d){    var crossA = Mathf.Sign(Vector3.Cross(d - c, a - c).y);    var crossB = Mathf.Sign(Vector3.Cross(d - c, b - c).y);    if (Mathf.Approximately(crossA, crossB)) return false;    var crossC = Mathf.Sign(Vector3.Cross(b - a, c - a).y);    var crossD = Mathf.Sign(Vector3.Cross(b - a, d - a).y);    if (Mathf.Approximately(crossC, crossD)) return false;    return true;}

 

 

后来我找到了另一个封装好的函数,不仅可以判断相交而且能查到是否相交于虚交点,是否平行等等。

原版的编写语言是java,但出处弄丢了

 

以下是我修改的Unity版本:

public static int GetIntersection(Vector3 a, Vector3 b, Vector3 c, Vector3 d, out Vector3 contractPoint){    contractPoint = new Vector3(0, 0);    if (Mathf.Abs(b.z - a.z) + Mathf.Abs(b.x - a.x) + Mathf.Abs(d.z - c.z)            + Mathf.Abs(d.x - c.x) == 0)    {        if ((c.x - a.x) + (c.z - a.z) == 0)        {            //Debug.Log("ABCD是同一个点!");        }        else        {            //Debug.Log("AB是一个点,CD是一个点,且AC不同!");        }        return 0;    }    if (Mathf.Abs(b.z - a.z) + Mathf.Abs(b.x - a.x) == 0)    {        if ((a.x - d.x) * (c.z - d.z) - (a.z - d.z) * (c.x - d.x) == 0)        {            //Debug.Log("A、B是一个点,且在CD线段上!");        }        else        {            //Debug.Log("A、B是一个点,且不在CD线段上!");        }        return 0;    }    if (Mathf.Abs(d.z - c.z) + Mathf.Abs(d.x - c.x) == 0)    {        if ((d.x - b.x) * (a.z - b.z) - (d.z - b.z) * (a.x - b.x) == 0)        {            //Debug.Log("C、D是一个点,且在AB线段上!");        }        else        {            //Debug.Log("C、D是一个点,且不在AB线段上!");        }        return 0;    }    if ((b.z - a.z) * (c.x - d.x) - (b.x - a.x) * (c.z - d.z) == 0)    {        //Debug.Log("线段平行,无交点!");        return 0;    }    contractPoint.x = ((b.x - a.x) * (c.x - d.x) * (c.z - a.z) -            c.x * (b.x - a.x) * (c.z - d.z) + a.x * (b.z - a.z) * (c.x - d.x)) /            ((b.z - a.z) * (c.x - d.x) - (b.x - a.x) * (c.z - d.z));    contractPoint.z = ((b.z - a.z) * (c.z - d.z) * (c.x - a.x) - c.z            * (b.z - a.z) * (c.x - d.x) + a.z * (b.x - a.x) * (c.z - d.z))            / ((b.x - a.x) * (c.z - d.z) - (b.z - a.z) * (c.x - d.x));    if ((contractPoint.x - a.x) * (contractPoint.x - b.x) <= 0            && (contractPoint.x - c.x) * (contractPoint.x - d.x) <= 0            && (contractPoint.z - a.z) * (contractPoint.z - b.z) <= 0            && (contractPoint.z - c.z) * (contractPoint.z - d.z) <= 0)    {        //Debug.Log("线段相交于点(" + contractPoint.x + "," + contractPoint.z + ")!");        return 1; // '相交      }    else    {        //Debug.Log("线段相交于虚交点(" + contractPoint.x + "," + contractPoint.z + ")!");        return -1; // '相交但不在线段上      }}

 

转载于:https://www.cnblogs.com/hont/p/6106043.html

你可能感兴趣的文章
maven 配置文件详解
查看>>
几种OutOfMemoryError
查看>>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>会报错
查看>>
智课雅思词汇---十三、前缀ab-是什么意思
查看>>
同样是搞Java,年薪15W和50W到底差在哪里?
查看>>
legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法)
查看>>
尚学linux课程---9、yum相关操作和知识
查看>>
php回调函数原理和实例
查看>>
Socket 网络编程实践经验
查看>>
7K - find your present (2)
查看>>
MySQL 日期和时间戳的转换 | 以及DATE_FORMAT()用法
查看>>
小鱼家的电费
查看>>
0302 关于就业方面的一些感想
查看>>
回调的经典理解
查看>>
已超过了锁请求超时时段。 (Microsoft SQL Server,错误: 1222)
查看>>
淘宝直通车名词
查看>>
小程序 requestAnimationFrame 死循环
查看>>
Ubuntu11.10 字体修改
查看>>
【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem
查看>>
【51NOD-0】1130 N的阶乘的长度 V2(斯特林近似)
查看>>