C# 获取两线段交点

2 篇文章 0 订阅
//获取两直线交点,z 值没有用
    public static Vector3 GetIntersection(Vector3 lineAStart,Vector3 lineAEnd, Vector3 lineBStart,Vector3 lineBEnd)
    {
        float x1 = lineAStart.x, y1 = lineAStart.y;
        float x2 = lineAEnd.x, y2 = lineAEnd.y;

        float x3 = lineBStart.x, y3 = lineBStart.y;
        float x4 = lineBEnd.x, y4 = lineBEnd.y;

        //equations of the form x=c (two vertical lines)
        if (x1 == x2 && x3 == x4 && x1 == x3)
        {
            return Vector3.zero;
        }

        //equations of the form y=c (two horizontal lines)
        if (y1 == y2 && y3 == y4 && y1 == y3)
        {
            return Vector3.zero;
        }

        //equations of the form x=c (two vertical lines)
        if (x1 == x2 && x3 == x4)
        {
            return Vector3.zero;
        }

        //equations of the form y=c (two horizontal lines)
        if (y1 == y2 && y3 == y4)
        {
            return Vector3.zero;
        }
        float x, y;

        if (x1 == x2)
        {
            float m2 = (y4 - y3) / (x4 - x3);
            float c2 = -m2 * x3 + y3;

            x = x1;
            y = c2 + m2 * x1;
        }
        else if (x3 == x4)
        {
            float m1 = (y2 - y1) / (x2 - x1);
            float c1 = -m1 * x1 + y1;

            x = x3;
            y = c1 + m1 * x3;
        }
        else
        {
            //compute slope of line 1 (m1) and c2
            float m1 = (y2 - y1) / (x2 - x1);
            float c1 = -m1 * x1 + y1;

            //compute slope of line 2 (m2) and c2
            float m2 = (y4 - y3) / (x4 - x3);
            float c2 = -m2 * x3 + y3;

            //solving equations (3) & (4) => x = (c1-c2)/(m2-m1)
            //plugging x value in equation (4) => y = c2 + m2 * x
            x = (c1 - c2) / (m2 - m1);
            y = c2 + m2 * x;

            //          if (!(-m1 * x + y == c1
            //              && -m2 * x + y == c2))
            //          {
            //              return Vector3.zero;
            //          }
        }

        if (IsInsideLine(lineAStart,lineAEnd, x, y) &&
            IsInsideLine(lineBStart,lineBEnd, x, y))
        {
            return new Vector3(x,y,0);
        }

        //return default null (no intersection)
        return Vector3.zero;
    }
    private static bool IsInsideLine(Vector3 start, Vector3 end, float x, float y)
    {
        return ((x >= start.x && x <= end.x)
            || (x >= end.x && x <= start.x))
            && ((y >= start.y && y <= end.y)
                || (y >= end.y && y <= start.y));
    }

 

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值