龙芯爱好者的日常!
返回

计算(x,y,z)点到线段的距离,适用于计算一个 rgb 颜色与双色键的距离

2020-03-02 170 0

float pointToLineSegment3(vec3 p1, vec3 p2, vec3 p)

{

    if ( p1 == p2 )

        return distance(p, p1);

    float a = distance(p1, p2);

    float b = distance(p1, p);

    float c = distance(p2, p);

    if ( c + b <= a)

        return 0.0;

    else if ( c * c >= a * a + b * b )

        return b;

    else if ( b * b >= a * a + c * c )

        return c;

    float d = ( a + b + c ) * 0.5;

    float k = d * (d - a) * (d - b) * (d - c);

    float s = sqrt(k);

    return 2.0 * s / a;

}



顶部