Unity中计算点到线的距离

private double pointToLine(Vector2 point1, Vector2 point2, Vector2 position)//point1和point2为线的两个端点
    {
        double space = 0;
        double a, b, c;
        a = Vector2.Distance(point1,point2);// 线段的长度      
        b = Vector2.Distance(point1, position);// position到点point1的距离      
        c = Vector2.Distance(point2, position);// position到point2点的距离 
        if (c <= 0.000001 || b <= 0.000001)
        {
            space = 0;
            return space;
        }
        if (a <= 0.000001)
        {
            space = b;
            return space;
        }
        if (c * c >= a * a + b * b)
        {
            space = b;
            return space;
        }
        if (b * b >= a * a + c * c)
        {
            space = c;
            return space;
        }
        double p = (a + b + c) / 2;// 半周长      
        double s = Math.Sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积      
        space = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)      
        return space;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值